1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Puzzle\AMQP\Commands\Worker; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Command\Command; |
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Puzzle\AMQP\Workers\ProcessorInterfaceAdapter; |
10
|
|
|
use Puzzle\AMQP\Client; |
11
|
|
|
use Puzzle\AMQP\Workers\WorkerProvider; |
12
|
|
|
use Puzzle\AMQP\Workers\WorkerContext; |
13
|
|
|
use Puzzle\Pieces\OutputInterfaceAware; |
14
|
|
|
use Puzzle\Pieces\EventDispatcher\EventDispatcherAware; |
15
|
|
|
use Puzzle\Pieces\EventDispatcher\NullEventDispatcher; |
16
|
|
|
use Puzzle\AMQP\Workers\MessageAdapterFactory; |
17
|
|
|
|
18
|
|
|
class Run extends Command |
19
|
|
|
{ |
20
|
|
|
use EventDispatcherAware; |
21
|
|
|
|
22
|
|
|
private |
23
|
|
|
$client, |
24
|
|
|
$workerProvider, |
25
|
|
|
$messageAdapterFactory, |
26
|
|
|
$outputInterfaceAware; |
27
|
|
|
|
28
|
|
View Code Duplication |
public function __construct(Client $client, WorkerProvider $workerProvider, OutputInterfaceAware $outputInterfaceAware) |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
parent::__construct(); |
31
|
|
|
|
32
|
|
|
$this->client = $client; |
33
|
|
|
$this->workerProvider = $workerProvider; |
34
|
|
|
$this->messageAdapterFactory = null; |
35
|
|
|
$this->outputInterfaceAware = $outputInterfaceAware; |
36
|
|
|
$this->eventDispatcher = new NullEventDispatcher(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function setMessageAdapterFactory(MessageAdapterFactory $factory) |
40
|
|
|
{ |
41
|
|
|
$this->messageAdapterFactory = $factory; |
42
|
|
|
|
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected function configure() |
47
|
|
|
{ |
48
|
|
|
$this->setName('run') |
49
|
|
|
->setDescription('Launch AMQP worker') |
50
|
|
|
->addArgument('task', InputArgument::REQUIRED, 'worker name to run'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
54
|
|
|
{ |
55
|
|
|
$this->outputInterfaceAware->register($output); |
56
|
|
|
|
57
|
|
|
$workerName = $input->getArgument('task'); |
58
|
|
|
$workerContext = $this->workerProvider->getWorker($workerName); |
59
|
|
|
|
60
|
|
|
if($workerContext instanceof WorkerContext) |
61
|
|
|
{ |
62
|
|
|
$output->writeln("Launching <info>$workerName</info>"); |
63
|
|
|
|
64
|
|
|
$processor = $this->createProcessor($workerContext); |
65
|
|
|
|
66
|
|
|
$this->eventDispatcher->dispatch('worker.run'); |
67
|
|
|
|
68
|
|
|
return $workerContext->getConsumer()->consume($processor, $this->client, $workerContext); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$output->writeln("<error>Worker $workerName not found</error>"); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function createProcessor(WorkerContext $workerContext) |
75
|
|
|
{ |
76
|
|
|
$processor = new ProcessorInterfaceAdapter($workerContext, $this->messageAdapterFactory); |
77
|
|
|
$processor->setEventDispatcher($this->eventDispatcher); |
78
|
|
|
|
79
|
|
|
$processor->setMessageProcessors( |
80
|
|
|
$this->workerProvider->getMessageProcessors() |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
return $processor; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
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.