-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice_communication.php
More file actions
45 lines (39 loc) · 1.16 KB
/
service_communication.php
File metadata and controls
45 lines (39 loc) · 1.16 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
<?php
// php -d extension=modules/kislayphp_queue.so service_communication.php
if (!extension_loaded('kislayphp_queue')) {
fwrite(STDERR, "kislayphp_queue extension is not loaded\n");
exit(1);
}
$queue = new Kislay\Queue\Queue();
$traceId = bin2hex(random_bytes(8));
$requestId = bin2hex(random_bytes(8));
$queue->enqueue('svc.inventory.commands', [
'traceId' => $traceId,
'requestId' => $requestId,
'source' => 'orders',
'target' => 'inventory',
'type' => 'ReserveStock',
'payload' => [
'sku' => 'ABC-1',
'qty' => 2,
],
'ts' => time(),
]);
// Worker side consumption simulation.
$command = $queue->dequeue('svc.inventory.commands');
$ok = is_array($command) && ($command['type'] ?? '') === 'ReserveStock';
$queue->enqueue('svc.orders.replies', [
'traceId' => $traceId,
'requestId' => $requestId,
'source' => 'inventory',
'target' => 'orders',
'status' => $ok ? 'OK' : 'ERROR',
'payload' => ['reserved' => $ok],
'ts' => time(),
]);
$reply = $queue->dequeue('svc.orders.replies');
echo json_encode([
'ok' => $ok,
'command' => $command,
'reply' => $reply,
], JSON_PRETTY_PRINT) . PHP_EOL;