1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Xervice\RabbitMQ\Communication\Console\Command; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
use Xervice\Console\Business\Model\Command\AbstractCommand; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @method \Xervice\RabbitMQ\Business\RabbitMQFacade getFacade() |
15
|
|
|
*/ |
16
|
|
|
class WorkerCommand extends AbstractCommand |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
20
|
|
|
*/ |
21
|
|
|
protected function configure(): void |
22
|
|
|
{ |
23
|
|
|
$this |
24
|
|
|
->setName('queue:worker:run') |
25
|
|
|
->addOption('loop', 'l', InputOption::VALUE_NONE, 'Endless loop') |
26
|
|
|
->addOption('runtime', 't', InputOption::VALUE_REQUIRED, 'How long running this command',60); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
31
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
32
|
|
|
* |
33
|
|
|
* @return int|void |
34
|
|
|
*/ |
35
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
36
|
|
|
{ |
37
|
|
|
$loop = $input->getOption('loop'); |
38
|
|
|
$loopTime = $input->getOption('runtime'); |
39
|
|
|
|
40
|
|
|
$time = time(); |
41
|
|
|
$timeEnd = $time; |
42
|
|
|
$timeLeft = $loopTime - ($timeEnd - $time); |
43
|
|
|
|
44
|
|
|
if ($output->isDebug()) { |
45
|
|
|
$output->writeln('Start at ' . date('H:i:s', $time)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
while ($loop || $timeLeft > 0) { |
49
|
|
|
$this->runQueueWorker($output); |
50
|
|
|
|
51
|
|
|
$timeEnd = time(); |
52
|
|
|
$timeLeft = $loopTime - ($timeEnd - $time); |
53
|
|
|
|
54
|
|
|
if ($output->isDebug() && !$loop) { |
55
|
|
|
$output->writeln('<comment>Time left: ' . $timeLeft . ' seconds!</comment>'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
} |
59
|
|
|
if ($output->isDebug()) { |
60
|
|
|
$output->writeln(''); |
61
|
|
|
$output->writeln('Finished at ' . date('H:i:s', $timeEnd)); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
67
|
|
|
*/ |
68
|
|
|
protected function runQueueWorker(OutputInterface $output): void |
69
|
|
|
{ |
70
|
|
|
$this->getFacade()->runWorker($output); |
71
|
|
|
} |
72
|
|
|
} |