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