-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathExampleMessageHandler.php
More file actions
67 lines (61 loc) · 2.13 KB
/
ExampleMessageHandler.php
File metadata and controls
67 lines (61 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
declare(strict_types=1);
namespace Queue\App\Message;
use Dot\DependencyInjection\Attribute\Inject;
use Dot\Log\Logger;
use Symfony\Component\Messenger\Exception\ExceptionInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Stamp\DelayStamp;
class ExampleMessageHandler
{
#[Inject(
MessageBusInterface::class,
'dot-log.queue-log',
'config',
)]
public function __construct(
protected MessageBusInterface $bus,
protected Logger $logger,
protected array $config,
) {
}
public function __invoke(ExampleMessage $message): void
{
try {
// Throwing an exception to satisfy PHPStan (replace with own code)
throw new \Exception("Failed to execute");
} catch (\Throwable $exception) {
$payload = $message->getPayload();
$this->logger->error($payload['foo'] . ' failed with message: '
. $exception->getMessage() . ' after ' . ($payload['retry'] ?? 0) . ' retries');
$this->retry($payload);
}
}
/**
* @throws ExceptionInterface
*/
public function retry(array $payload): void
{
if (! isset($payload['retry'])) {
$this->bus->dispatch(new ExampleMessage(["foo" => $payload['foo'], 'retry' => 1]), [
new DelayStamp($this->config['fail-safe']['first_retry']),
]);
} else {
$retry = $payload['retry'];
switch ($retry) {
case 1:
$delay = $this->config['fail-safe']['second_retry'];
$this->bus->dispatch(new ExampleMessage(["foo" => $payload['foo'], 'retry' => ++$retry]), [
new DelayStamp($delay),
]);
break;
case 2:
$delay = $this->config['fail-safe']['third_retry'];
$this->bus->dispatch(new ExampleMessage(["foo" => $payload['foo'], 'retry' => ++$retry]), [
new DelayStamp($delay),
]);
break;
}
}
}
}