-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathRunCommand.php
More file actions
122 lines (105 loc) · 2.68 KB
/
RunCommand.php
File metadata and controls
122 lines (105 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
declare(strict_types=1);
namespace Queue\Command;
use Cake\Command\Command;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use Cake\Core\ContainerInterface;
use Cake\Log\Log;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Queue\Console\Io;
use Queue\Queue\Processor;
/**
* Main execution of queued jobs.
*/
class RunCommand extends Command {
/**
* @return string
*/
public static function getDescription(): string {
return 'Run queue worker to process jobs.';
}
/**
* @var \Cake\Core\ContainerInterface
*/
protected ContainerInterface $container;
/**
* @param \Cake\Core\ContainerInterface $container
*/
public function __construct(ContainerInterface $container) {
$this->container = $container;
}
/**
* @inheritDoc
*/
public static function defaultName(): string {
return 'queue run';
}
/**
* @return \Cake\Console\ConsoleOptionParser
*/
public function getOptionParser(): ConsoleOptionParser {
$parser = parent::getOptionParser();
$parser->addOption('config', [
'default' => 'default',
'help' => 'Name of a queue config to use',
'short' => 'c',
]);
$parser->addOption('logger', [
'help' => 'Name of a configured logger',
'default' => 'stdout',
'short' => 'l',
]);
$parser->addOption('max-runtime', [
'help' => 'Seconds for max runtime',
'default' => null,
'short' => 'r',
]);
$parser->addOption('group', [
'short' => 'g',
'help' => 'Group (comma separated list possible)',
'default' => null,
]);
$parser->addOption('type', [
'short' => 't',
'help' => 'Type (comma separated list possible)',
'default' => null,
]);
$parser->setDescription(
'Simple and minimalistic job queue (or deferred-task) system.'
. PHP_EOL
. 'This command runs a queue worker.',
);
return $parser;
}
/**
* @param \Cake\Console\Arguments $args Arguments
*
* @return \Psr\Log\LoggerInterface
*/
protected function getLogger(Arguments $args): LoggerInterface {
$logger = null;
if (!$args->getOption('quiet')) {
$logger = Log::engine((string)$args->getOption('logger'));
}
return $logger ?? new NullLogger();
}
/**
* Run a QueueWorker loop.
* Runs a Queue Worker process which will try to find unassigned jobs in the queue
* which it may run and try to fetch and execute them.
*
* @param \Cake\Console\Arguments $args Arguments
* @param \Cake\Console\ConsoleIo $io ConsoleIo
*
* @return int
*/
public function execute(Arguments $args, ConsoleIo $io): int {
$logger = $this->getLogger($args);
$io = new Io($io);
$processor = new Processor($io, $logger, $this->container);
return $processor->run($args->getOptions());
}
}