1 | <?php |
||
13 | class RetryProcessor implements ProcessorInterface |
||
14 | { |
||
15 | const PROPERTY_KEY = 'attempt'; |
||
16 | |||
17 | /** |
||
18 | * @var ProcessorInterface |
||
19 | */ |
||
20 | protected $processor; |
||
21 | |||
22 | /** |
||
23 | * @var RetryStrategyInterface |
||
24 | */ |
||
25 | protected $strategy; |
||
26 | |||
27 | /** |
||
28 | * @var LoggerInterface |
||
29 | */ |
||
30 | protected $logger; |
||
31 | |||
32 | /** |
||
33 | * @var int |
||
34 | */ |
||
35 | protected $maxAttempts = 2; |
||
36 | |||
37 | /** |
||
38 | * @param ProcessorInterface $processor |
||
39 | * @param RetryStrategyInterface $strategy |
||
40 | * @param LoggerInterface $logger |
||
41 | */ |
||
42 | 5 | public function __construct(ProcessorInterface $processor, RetryStrategyInterface $strategy, LoggerInterface $logger = null) |
|
43 | { |
||
44 | 5 | $this->processor = $processor; |
|
45 | 5 | $this->strategy = $strategy; |
|
46 | 5 | $this->logger = $logger; |
|
47 | 5 | } |
|
48 | |||
49 | /** |
||
50 | * @param ProcessorInterface $processor |
||
51 | */ |
||
52 | 1 | public function setProcessor($processor) |
|
53 | { |
||
54 | 1 | $this->processor = $processor; |
|
55 | 1 | } |
|
56 | |||
57 | /** |
||
58 | * @return ProcessorInterface |
||
59 | */ |
||
60 | 1 | public function getProcessor() |
|
61 | { |
||
62 | 1 | return $this->processor; |
|
63 | } |
||
64 | |||
65 | /** |
||
66 | * @param int $maxAttempts |
||
67 | */ |
||
68 | 2 | public function setMaxAttempts($maxAttempts) |
|
69 | { |
||
70 | 2 | $this->maxAttempts = $maxAttempts; |
|
71 | 2 | } |
|
72 | |||
73 | /** |
||
74 | * @return int |
||
75 | */ |
||
76 | 1 | public function getMaxAttempts() |
|
77 | { |
||
78 | 1 | return $this->maxAttempts; |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * @inheritdoc |
||
83 | */ |
||
84 | 3 | public function process(EnvelopeInterface $envelope) |
|
85 | { |
||
86 | try { |
||
87 | 3 | $result = $this->processor->process($envelope); |
|
88 | 2 | } catch (\Exception $exception) { |
|
89 | 2 | if ($this->logger) { |
|
90 | $this->logger->error($exception->getMessage(), ['message' => $envelope->getDeliveryTag()]); |
||
91 | } |
||
92 | |||
93 | 2 | $result = $this->retryMessage($envelope, $exception); |
|
94 | } |
||
95 | |||
96 | 2 | return $result; |
|
97 | } |
||
98 | |||
99 | /** |
||
100 | * @param EnvelopeInterface $envelope |
||
101 | * @param \Exception $exception |
||
102 | * |
||
103 | * @throws ProcessExhaustedException |
||
104 | * |
||
105 | * @return bool |
||
106 | */ |
||
107 | 2 | protected function retryMessage(EnvelopeInterface $envelope, \Exception $exception = null) |
|
120 | |||
121 | /** |
||
122 | * @param EnvelopeInterface $envelope |
||
123 | * |
||
124 | * @throws \LogicException |
||
125 | * |
||
126 | * @return int |
||
127 | */ |
||
128 | 2 | protected function getAttemptValue(EnvelopeInterface $envelope) |
|
144 | } |
||
145 |