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