1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Puzzle\AMQP\Workers; |
4
|
|
|
|
5
|
|
|
use Swarrot\Processor\ProcessorInterface; |
6
|
|
|
use Puzzle\Pieces\EventDispatcher\NullEventDispatcher; |
7
|
|
|
use Puzzle\Pieces\EventDispatcher\EventDispatcherAware; |
8
|
|
|
use Puzzle\AMQP\Clients\Processors\MessageProcessorAware; |
9
|
|
|
|
10
|
|
|
class ProcessorInterfaceAdapter implements ProcessorInterface |
11
|
|
|
{ |
12
|
|
|
use |
13
|
|
|
EventDispatcherAware, |
14
|
|
|
MessageAdapterFactoryAware, |
15
|
|
|
MessageProcessorAware; |
16
|
|
|
|
17
|
|
|
private |
18
|
|
|
$workerContext; |
19
|
|
|
|
20
|
5 |
|
public function __construct(WorkerContext $workerContext) |
21
|
|
|
{ |
22
|
5 |
|
$this->workerContext = $workerContext; |
23
|
5 |
|
$this->eventDispatcher = new NullEventDispatcher(); |
24
|
5 |
|
$this->messageAdapterFactory = null; |
25
|
5 |
|
} |
26
|
|
|
|
27
|
5 |
|
public function process(\Swarrot\Broker\Message $message, array $options) |
28
|
|
|
{ |
29
|
5 |
|
$message = $this->createMessageAdapter($message); |
30
|
5 |
|
$message = $this->onConsume($message); |
31
|
|
|
|
32
|
5 |
|
$this->workerContext->getLogger()->debug((string) $message); |
33
|
|
|
|
34
|
5 |
|
$this->onWorkerProcess(); |
35
|
|
|
|
36
|
|
|
try |
37
|
|
|
{ |
38
|
5 |
|
$processResult = $this->workerContext->getWorker()->process($message); |
39
|
|
|
} |
40
|
2 |
|
catch(\Throwable $exception) |
41
|
|
|
{ |
42
|
2 |
|
$this->onWorkerProcessed(); |
43
|
|
|
|
44
|
2 |
|
if($exception instanceof \Error) |
45
|
|
|
{ |
46
|
1 |
|
$exception = new \ErrorException($exception->getMessage(), $exception->getCode(), E_ERROR, $exception->getFile(), $exception->getLine(), $exception); |
47
|
|
|
} |
48
|
|
|
|
49
|
2 |
|
throw $exception; |
50
|
|
|
} |
51
|
|
|
|
52
|
3 |
|
$this->onWorkerProcessed(); |
53
|
|
|
|
54
|
3 |
|
return $processResult; |
55
|
|
|
} |
56
|
|
|
|
57
|
5 |
|
private function onWorkerProcess() |
58
|
|
|
{ |
59
|
5 |
|
$this->eventDispatcher->dispatch('worker.process'); |
60
|
5 |
|
} |
61
|
|
|
|
62
|
5 |
|
private function onWorkerProcessed() |
63
|
|
|
{ |
64
|
5 |
|
$this->eventDispatcher->dispatch('worker.processed'); |
65
|
5 |
|
} |
66
|
|
|
} |
67
|
|
|
|