1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of HeriJobQueueBundle. |
5
|
|
|
* |
6
|
|
|
* (c) Alexandre Mogère |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Heri\Bundle\JobQueueBundle\Command; |
13
|
|
|
|
14
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
15
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
17
|
|
|
use Symfony\Component\Console\Input\InputOption; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
|
20
|
|
|
class QueueListenCommand extends ContainerAwareCommand |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @param bool |
24
|
|
|
*/ |
25
|
|
|
protected $work; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
protected function configure() |
31
|
|
|
{ |
32
|
|
|
$this |
33
|
|
|
->setName('jobqueue:listen') |
34
|
|
|
->setDescription('Initialize JobQueue manager') |
35
|
|
|
->addArgument('queue-name', InputArgument::OPTIONAL, 'Listen a specific queue') |
36
|
|
|
->addOption( |
37
|
|
|
'sleep', |
38
|
|
|
'0.2', |
39
|
|
|
InputOption::VALUE_OPTIONAL, |
40
|
|
|
'Number of seconds to wait before polling for new job', |
41
|
|
|
1 |
42
|
|
|
) |
43
|
|
|
; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
50
|
|
|
{ |
51
|
|
|
$queue = $this->getContainer()->get('jobqueue'); |
52
|
|
|
$queue->setUp($this->getContainer()->getParameter('jobqueue.config')); |
53
|
|
|
$queue->setOutput($output); |
54
|
|
|
|
55
|
|
|
if (!$queue->isEnabled()) { |
56
|
|
|
$output->writeLn('<comment>JobQueue manager deactivated</comment>'); |
57
|
|
|
} else { |
58
|
|
|
if ($this->work) { |
59
|
|
|
$output->writeLn('<comment>Handling the first job on the queue...</comment>'); |
60
|
|
|
} else { |
61
|
|
|
$output->writeLn('<comment>JobQueue running... press ctrl-c to stop.</comment>'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$queue->listen( |
65
|
|
|
$input->getArgument('queue-name'), |
66
|
|
|
$input->getOption('sleep'), |
67
|
|
|
$this->work |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|