1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TreeHouse\WorkerBundle\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Command\Command; |
6
|
|
|
use Symfony\Component\Console\Helper\Table; |
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 TreeHouse\WorkerBundle\QueueManager; |
12
|
|
|
|
13
|
|
|
class PeekCommand extends Command |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var QueueManager |
17
|
|
|
*/ |
18
|
|
|
protected $manager; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param QueueManager $queueManager |
22
|
|
|
*/ |
23
|
|
|
public function __construct(QueueManager $queueManager) |
24
|
|
|
{ |
25
|
|
|
$this->manager = $queueManager; |
26
|
|
|
|
27
|
|
|
parent::__construct(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @inheritdoc |
32
|
|
|
*/ |
33
|
|
View Code Duplication |
protected function configure() |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$this |
36
|
|
|
->setName('worker:peek') |
37
|
|
|
->addArgument('action', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Selects which actions to peek for. Defaults to all actions') |
38
|
|
|
->addOption('state', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The state of job to peek for. Valid options are <comment>ready</comment>, <comment>delayed</comment> and <comment>buried</comment>.', ['ready']) |
39
|
|
|
->setDescription('Peeks the next job from the queue') |
40
|
|
|
->setHelp('Inspects the next job from the queue for one or more actions. Note that this does not reserve the job, so it will still be given to a worker if/once it\'s ready.') |
41
|
|
|
; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritdoc |
46
|
|
|
*/ |
47
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
48
|
|
|
{ |
49
|
|
|
$actions = $input->getArgument('action'); |
50
|
|
|
if (empty($actions)) { |
51
|
|
|
$actions = array_keys($this->manager->getExecutors()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$states = $input->getOption('state'); |
55
|
|
|
|
56
|
|
|
foreach ($actions as $action) { |
57
|
|
|
$output->writeln(sprintf('Action: <info>%s</info>', $action)); |
58
|
|
|
|
59
|
|
|
$table = new Table($output); |
60
|
|
|
$table->setHeaders(['state', 'id', 'data']); |
61
|
|
|
foreach ($states as $state) { |
62
|
|
|
$job = $this->manager->peek($action, $state); |
63
|
|
|
|
64
|
|
|
if (is_null($job)) { |
65
|
|
|
$table->addRow([$state, '-', '-']); |
66
|
|
|
} else { |
67
|
|
|
$table->addRow([$state, $job->getId(), $job->getData()]); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$table->render(); |
72
|
|
|
$output->writeln(''); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.