1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gendoria\CommandQueueBundle\Worker; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Gendoria\CommandQueue\Command\CommandInterface; |
7
|
|
|
use Gendoria\CommandQueue\CommandProcessor\CommandProcessorInterface; |
8
|
|
|
use Gendoria\CommandQueue\ProcessorFactory\ProcessorFactoryInterface; |
9
|
|
|
use Gendoria\CommandQueue\Serializer\SerializerInterface; |
10
|
|
|
use Gendoria\CommandQueue\Worker\BaseWorker; |
11
|
|
|
use Gendoria\CommandQueueBundle\Event\QueueBeforeGetProcessorEvent; |
12
|
|
|
use Gendoria\CommandQueueBundle\Event\QueueBeforeTranslateEvent; |
13
|
|
|
use Gendoria\CommandQueueBundle\Event\QueueEvents; |
14
|
|
|
use Gendoria\CommandQueueBundle\Event\QueueProcessErrorEvent; |
15
|
|
|
use Gendoria\CommandQueueBundle\Event\QueueProcessEvent; |
16
|
|
|
use Monolog\Handler\FingersCrossedHandler; |
17
|
|
|
use Monolog\Logger; |
18
|
|
|
use Psr\Log\LoggerInterface; |
19
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Base for Symfony based queue workers. |
23
|
|
|
* |
24
|
|
|
* This class takes care of correctly handling long-running process logs, |
25
|
|
|
* as well as sending some events on Symfony event bus. |
26
|
|
|
* |
27
|
|
|
* @author Tomasz Struczyński <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
abstract class BaseSymfonyWorker extends BaseWorker |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Symfony event dispatcher. |
33
|
|
|
* |
34
|
|
|
* @var EventDispatcherInterface |
35
|
|
|
*/ |
36
|
|
|
private $eventDispatcher; |
37
|
|
|
|
38
|
2 |
|
public function __construct(ProcessorFactoryInterface $processorFactory, SerializerInterface $serializer, EventDispatcherInterface $eventDispatcher, LoggerInterface $logger = null) |
39
|
|
|
{ |
40
|
2 |
|
parent::__construct($processorFactory, $serializer, $logger); |
41
|
2 |
|
$this->eventDispatcher = $eventDispatcher; |
42
|
2 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get subsystem name. |
46
|
|
|
* |
47
|
|
|
* This name may be used further to identify worker subsystem in logs or events. |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
abstract public function getSubsystemName(); |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
2 |
|
protected function beforeTranslateHook(&$commandData) |
57
|
|
|
{ |
58
|
2 |
|
parent::beforeTranslateHook($commandData); |
59
|
|
|
|
60
|
2 |
|
$this->eventDispatcher->dispatch(QueueEvents::WORKER_RUN_BEFORE_TRANSLATE, new QueueBeforeTranslateEvent($this, $commandData, $this->getSubsystemName())); |
61
|
2 |
|
$this->clearLogs(); |
62
|
2 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
2 |
|
protected function beforeGetProcessorHook(CommandInterface $command) |
68
|
|
|
{ |
69
|
2 |
|
parent::beforeGetProcessorHook($command); |
70
|
2 |
|
$this->eventDispatcher->dispatch(QueueEvents::WORKER_RUN_BEFORE_GET_PROCESSOR, new QueueBeforeGetProcessorEvent($this, $command, $this->getSubsystemName())); |
71
|
2 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
2 |
|
protected function beforeProcessHook(CommandInterface $command, CommandProcessorInterface $processor) |
77
|
|
|
{ |
78
|
2 |
|
parent::beforeProcessHook($command, $processor); |
79
|
2 |
|
$this->eventDispatcher->dispatch(QueueEvents::WORKER_RUN_BEFORE_PROCESS, new QueueProcessEvent($this, $command, $processor, $this->getSubsystemName())); |
80
|
2 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
1 |
|
protected function afterProcessHook(CommandInterface $command, CommandProcessorInterface $processor) |
86
|
|
|
{ |
87
|
1 |
|
parent::afterProcessHook($command, $processor); |
88
|
1 |
|
$this->eventDispatcher->dispatch(QueueEvents::WORKER_RUN_AFTER_PROCESS, new QueueProcessEvent($this, $command, $processor, $this->getSubsystemName())); |
89
|
1 |
|
$this->clearLogs(); |
90
|
1 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
1 |
|
protected function processorErrorHook(CommandInterface $command, CommandProcessorInterface $processor, Exception $e) |
96
|
|
|
{ |
97
|
1 |
|
parent::processorErrorHook($command, $processor, $e); |
98
|
1 |
|
$this->eventDispatcher->dispatch(QueueEvents::WORKER_RUN_PROCESSOR_ERROR, new QueueProcessErrorEvent($this, $command, $processor, $e, $this->getSubsystemName())); |
99
|
1 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Clear logs. |
103
|
|
|
* |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
2 |
|
private function clearLogs() |
107
|
|
|
{ |
108
|
2 |
|
if (!$this->logger instanceof Logger) { |
109
|
1 |
|
return; |
110
|
|
|
} |
111
|
|
|
/* @var $logger Logger */ |
112
|
1 |
|
$logger = $this->logger; |
113
|
|
|
|
114
|
1 |
|
foreach ($logger->getHandlers() as $handler) { |
115
|
1 |
|
if ($handler instanceof FingersCrossedHandler) { |
116
|
1 |
|
$handler->clear(); |
117
|
1 |
|
} |
118
|
1 |
|
} |
119
|
1 |
|
} |
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
|