1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dtc\QueueBundle\Command; |
4
|
|
|
|
5
|
|
|
use Dtc\QueueBundle\Model\Job; |
6
|
|
|
use Dtc\QueueBundle\Run\Loop; |
7
|
|
|
use Dtc\QueueBundle\Util\Util; |
8
|
|
|
use Psr\Log\LoggerInterface; |
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Input\InputOption; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
|
15
|
|
|
class RunCommand extends ContainerAwareCommand |
16
|
|
|
{ |
17
|
1 |
|
protected function configure() |
18
|
|
|
{ |
19
|
|
|
$this |
20
|
1 |
|
->setName('dtc:queue:run') |
21
|
1 |
|
->setDefinition( |
22
|
|
|
array( |
23
|
1 |
|
new InputArgument('worker_name', InputArgument::OPTIONAL, 'Name of worker', null), |
24
|
1 |
|
new InputArgument('method', InputArgument::OPTIONAL, 'DI method of worker', null), |
25
|
1 |
|
new InputOption( |
26
|
1 |
|
'id', |
27
|
1 |
|
'i', |
28
|
1 |
|
InputOption::VALUE_REQUIRED, |
29
|
1 |
|
'Id of Job to run', |
30
|
1 |
|
null |
31
|
|
|
), |
32
|
1 |
|
new InputOption( |
33
|
1 |
|
'max_count', |
34
|
1 |
|
'm', |
35
|
1 |
|
InputOption::VALUE_REQUIRED, |
36
|
1 |
|
'Maximum number of jobs to work on before exiting', |
37
|
1 |
|
null |
38
|
|
|
), |
39
|
1 |
|
new InputOption( |
40
|
1 |
|
'duration', |
41
|
1 |
|
'd', |
42
|
1 |
|
InputOption::VALUE_REQUIRED, |
43
|
1 |
|
'Duration to run for in seconds', |
44
|
1 |
|
null |
45
|
|
|
), |
46
|
1 |
|
new InputOption( |
47
|
1 |
|
'timeout', |
48
|
1 |
|
't', |
49
|
1 |
|
InputOption::VALUE_REQUIRED, |
50
|
1 |
|
'Process timeout in seconds (hard exit of process regardless)', |
51
|
1 |
|
3600 |
52
|
|
|
), |
53
|
1 |
|
new InputOption( |
54
|
1 |
|
'nano_sleep', |
55
|
1 |
|
's', |
56
|
1 |
|
InputOption::VALUE_REQUIRED, |
57
|
1 |
|
'If using duration, this is the time to sleep when there\'s no jobs in nanoseconds', |
58
|
1 |
|
500000000 |
59
|
|
|
), |
60
|
1 |
|
new InputOption( |
61
|
1 |
|
'logger', |
62
|
1 |
|
'l', |
63
|
1 |
|
InputOption::VALUE_REQUIRED, |
64
|
1 |
|
'Log using the logger service specified, or output to console if null (or an invalid logger service id) is passed in' |
65
|
|
|
), |
66
|
|
|
) |
67
|
|
|
) |
68
|
1 |
|
->setDescription('Start up a job in queue'); |
69
|
1 |
|
} |
70
|
|
|
|
71
|
1 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
72
|
|
|
{ |
73
|
1 |
|
$start = microtime(true); |
74
|
1 |
|
$container = $this->getContainer(); |
75
|
1 |
|
$loop = $container->get('dtc_queue.run.loop'); |
76
|
1 |
|
$loop->setOutput($output); |
77
|
1 |
|
$workerName = $input->getArgument('worker_name'); |
78
|
1 |
|
$methodName = $input->getArgument('method'); |
79
|
1 |
|
$maxCount = $input->getOption('max_count'); |
80
|
1 |
|
$duration = $input->getOption('duration'); |
81
|
1 |
|
$processTimeout = $input->getOption('timeout'); |
82
|
1 |
|
$nanoSleep = $input->getOption('nano_sleep'); |
83
|
1 |
|
$loggerService = $input->getOption('logger'); |
84
|
|
|
|
85
|
1 |
|
$this->setLoggerService($loop, $loggerService); |
86
|
|
|
|
87
|
1 |
|
$maxCount = Util::validateIntNull('max_count', $maxCount, 32); |
88
|
1 |
|
$duration = Util::validateIntNull('duration', $duration, 32); |
89
|
1 |
|
$nanoSleep = Util::validateIntNull('nano_sleep', $nanoSleep, 63); |
90
|
1 |
|
$processTimeout = Util::validateIntNull('timeout', $processTimeout, 32); |
91
|
1 |
|
$loop->checkMaxCountDuration($maxCount, $duration, $processTimeout); |
92
|
|
|
|
93
|
|
|
// Check to see if there are other instances |
94
|
1 |
|
set_time_limit($processTimeout); // Set timeout on the process |
95
|
|
|
|
96
|
1 |
|
if ($jobId = $input->getOption('id')) { |
97
|
|
|
return $loop->runJobById($start, $jobId); // Run a single job |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
return $loop->runLoop($start, $workerName, $methodName, $maxCount, $duration, $nanoSleep); |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
protected function setLoggerService(Loop $loop, $loggerService) |
104
|
|
|
{ |
105
|
1 |
|
if (!$loggerService) { |
106
|
1 |
|
return; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$container = $this->getContainer(); |
110
|
|
|
if (!$container->has($loggerService)) { |
111
|
|
|
return; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$logger = $container->get($loggerService); |
115
|
|
|
if (!$logger instanceof LoggerInterface) { |
116
|
|
|
throw new \Exception("$loggerService must be instance of Psr\\Log\\LoggerInterface"); |
117
|
|
|
} |
118
|
|
|
$loop->setLogger($logger); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|