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