-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumerCallback.php
155 lines (135 loc) · 5.6 KB
/
consumerCallback.php
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
declare(strict_types=1);
use PhpAmqpLib\Message\AMQPMessage;
use Psr\Container\ContainerInterface;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use Doctrine\DBAL\Exception\DriverException;
use PhpAmqpLib\Exception\AMQPRuntimeException;
use PhpAmqpLib\Exception\AMQPProtocolException;
use Astrotech\Core\Base\Adapter\Contracts\LogSystem;
use Astrotech\Core\Base\Exception\ValidationException;
use Astrotech\Core\Base\Infra\ConsumerBase;
use PhpAmqpLib\Exception\AMQPConnectionClosedException;
function processMessage(AMQPMessage $message, ContainerInterface $container): void
{
/** @var LogSystem $logSystem */
$logSystem = $container->get(LogSystem::class);
$consumerRoutes = require ROUTES_PATH . '/consumers.php';
$messageBody = json_decode($message->getBody(), true);
$traceId = uniqid();
foreach ($consumerRoutes as $routingKey => $handlersList) {
$logSystem->info(
json_encode([
'messageRoutingKey' => $message->getRoutingKey(),
'consumerRoutingKey' => $routingKey
], JSON_PRETTY_PRINT),
['category' => $traceId]
);
if ($message->getRoutingKey() !== $routingKey) {
continue;
}
$logSystem->info(
json_encode(
[
'message' => "Message Routing Key '{$message->getRoutingKey()}' found in consumers routes"
],
JSON_PRETTY_PRINT
),
['category' => $traceId]
);
foreach ($handlersList as $routingKeyAction => $handlers) {
$actionName = trim($messageBody['action']);
$logSystem->info(
json_encode(['messageAction' => $actionName, 'routeActions' => $routingKeyAction], JSON_PRETTY_PRINT),
['category' => $traceId]
);
if ($routingKeyAction !== $actionName) {
continue;
}
if (!APP_IS_PRODUCTION && isset($messageBody['data']['automatedTests'])) {
$data = json_encode($messageBody, JSON_PRETTY_PRINT);
file_put_contents(RUNTIME_PATH . "/automatedTests/{$messageBody['eventId']}.log", $data);
continue;
}
$logSystem->info(
json_encode(
['message' => "'{$actionName}' action matched with '{$routingKeyAction}' Routing Key!"],
JSON_PRETTY_PRINT
),
['category' => $traceId]
);
try {
foreach ($handlers as $handlerClassName) {
$errorHandler = function (
Throwable $e,
array $details = []
) use (
$logSystem,
$handlerClassName,
$messageBody,
$traceId
): void {
$data = [
'date' => (new DateTimeImmutable())->format('Y-m-d H:i:s'),
'handler' => $handlerClassName,
'type' => get_class($e),
'message' => "{$e->getMessage()}",
'file' => "{$e->getFile()}:{$e->getLine()}",
'stackTrace' => $e->getTrace(),
'queueMessage' => json_encode($messageBody),
...$details
];
$jsonPayload = json_encode($data, JSON_PRETTY_PRINT);
if ($jsonPayload !== false) {
$logSystem->error($jsonPayload, ['category' => $traceId]);
return;
}
$output = sprintf(
"[%s] %s (%s:%s)" . PHP_EOL . "%s",
$handlerClassName,
$e->getMessage(),
$e->getFile(),
$e->getLine(),
$e->getTraceAsString()
);
$logSystem->error($output, ['category' => $traceId]);
};
/** @var ConsumerBase $handler */
$handler = new $handlerClassName($message, $container, $traceId);
$handler->execute();
}
$message->ack();
} catch (ValidationException $e) {
$errorHandler($e);
$message->ack();
} catch (
RequestException
| ConnectException
| AMQPRuntimeException
| AMQPProtocolException
| AMQPConnectionClosedException
$e
) {
$errorHandler($e);
$message->nack(true);
} catch (DriverException $e) {
$errorHandler($e, ['query' => $e->getQuery()->getSQL(), 'values' => $e->getQuery()->getParams()]);
$message->nack(true);
} catch (Throwable $e) {
$errorHandler($e);
$message->nack(true);
} finally {
$message->getChannel()->close();
}
$logSystem->info(
json_encode(['message' => "The '{$actionName}' handlers finished."], JSON_PRETTY_PRINT),
['category' => $traceId]
);
}
}
$logSystem->info(json_encode(
['message' => "All handlers executed!"],
JSON_PRETTY_PRINT
), ['category' => $traceId]);
}