|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tarantool\JobQueue\Handler; |
|
4
|
|
|
|
|
5
|
|
|
use Tarantool\JobQueue\Handler\RetryStrategy\LimitedRetryStrategy; |
|
6
|
|
|
use Tarantool\JobQueue\Handler\RetryStrategy\RetryStrategyFactory; |
|
7
|
|
|
use Tarantool\JobQueue\JobBuilder\JobOptions; |
|
8
|
|
|
use Tarantool\JobQueue\JobBuilder\RetryStrategies; |
|
9
|
|
|
use Tarantool\Queue\Options; |
|
10
|
|
|
use Tarantool\Queue\Queue; |
|
11
|
|
|
use Tarantool\Queue\Task; |
|
12
|
|
|
|
|
13
|
|
|
class RetryHandler implements Handler |
|
14
|
|
|
{ |
|
15
|
|
|
private $handler; |
|
16
|
|
|
private $retryStrategyFactory; |
|
17
|
|
|
|
|
18
|
|
|
private static $defaults = [ |
|
19
|
|
|
JobOptions::RETRY_LIMIT => 2, |
|
20
|
|
|
JobOptions::RETRY_ATTEMPT => 1, |
|
21
|
|
|
JobOptions::RETRY_STRATEGY => RetryStrategies::LINEAR, |
|
22
|
|
|
]; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(Handler $handler, RetryStrategyFactory $retryStrategyFactory) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->handler = $handler; |
|
27
|
|
|
$this->retryStrategyFactory = $retryStrategyFactory; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function handle(Task $task, Queue $queue): void |
|
31
|
|
|
{ |
|
32
|
|
|
$data = $task->getData() + self::$defaults; |
|
33
|
|
|
$attempt = $data[JobOptions::RETRY_ATTEMPT]; |
|
34
|
|
|
|
|
35
|
|
|
$strategy = $this->retryStrategyFactory->create($data[JobOptions::RETRY_STRATEGY]); |
|
36
|
|
|
$strategy = new LimitedRetryStrategy($strategy, $data[JobOptions::RETRY_LIMIT]); |
|
37
|
|
|
|
|
38
|
|
|
if (null === $delay = $strategy->getDelay($attempt)) { |
|
39
|
|
|
$this->handler->handle($task, $queue); |
|
40
|
|
|
|
|
41
|
|
|
return; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// TODO replace these 2 calls with an atomic one |
|
45
|
|
|
$queue->put([JobOptions::RETRY_ATTEMPT => $attempt + 1] + $data, [Options::DELAY => $delay]); |
|
46
|
|
|
$queue->delete($task->getId()); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|