|
2 | 2 |
|
3 | 3 | namespace Kiboko\Component\Flow\RabbitMQ; |
4 | 4 |
|
5 | | -use Bunny\Channel; |
6 | 5 | use Bunny\Client; |
7 | 6 | use Kiboko\Contract\Pipeline\StateInterface; |
8 | 7 |
|
9 | 8 | final class State implements StateInterface |
10 | 9 | { |
11 | | - private Channel $channel; |
12 | | - private array $metrics = []; |
| 10 | + private int $acceptMetric = 0; |
| 11 | + private int $rejectMetric = 0; |
| 12 | + private int $errorMetric = 0; |
13 | 13 |
|
14 | 14 | public function __construct( |
15 | | - private Client $connection, |
16 | | - private string $pipelineId, |
| 15 | + private StateManager $manager, |
17 | 16 | private string $stepCode, |
18 | 17 | private string $stepLabel, |
19 | | - private string $topic, |
20 | | - private ?int $messageLimit = 1, |
21 | | - private ?string $exchange = null, |
22 | 18 | ) { |
23 | | - $this->channel = $this->connection->channel(); |
24 | | - } |
25 | | - |
26 | | - public static function withoutAuthentication( |
27 | | - string $pipelineId, |
28 | | - string $stepCode, |
29 | | - string $stepLabel, |
30 | | - string $host, |
31 | | - string $vhost, |
32 | | - string $topic, |
33 | | - ?int $messageLimit = 1, |
34 | | - ?string $exchange = null, |
35 | | - ?int $port = null, |
36 | | - ): self { |
37 | | - $connection = new Client([ |
38 | | - 'host' => $host, |
39 | | - 'port' => $port, |
40 | | - 'vhost' => $vhost, |
41 | | - 'user' => 'guest', |
42 | | - 'password' => 'guest', |
43 | | - ]); |
44 | | - $connection->connect(); |
45 | | - |
46 | | - return new self($connection, pipelineId: $pipelineId, stepCode: $stepCode, stepLabel: $stepLabel, topic: $topic, messageLimit: $messageLimit, exchange: $exchange); |
47 | | - } |
48 | | - |
49 | | - public static function withAuthentication( |
50 | | - string $pipelineId, |
51 | | - string $stepCode, |
52 | | - string $stepLabel, |
53 | | - string $host, |
54 | | - string $vhost, |
55 | | - string $topic, |
56 | | - ?string $user, |
57 | | - ?string $password, |
58 | | - ?int $messageLimit = 1, |
59 | | - ?string $exchange = null, |
60 | | - ?int $port = null, |
61 | | - ): self { |
62 | | - $connection = new Client([ |
63 | | - 'host' => $host, |
64 | | - 'port' => $port, |
65 | | - 'vhost' => $vhost, |
66 | | - 'user' => $user, |
67 | | - 'password' => $password, |
68 | | - ]); |
69 | | - $connection->connect(); |
70 | | - |
71 | | - return new self($connection, pipelineId: $pipelineId, stepCode: $stepCode, stepLabel: $stepLabel, topic: $topic, messageLimit: $messageLimit, exchange: $exchange); |
72 | 19 | } |
73 | 20 |
|
74 | 21 | public function initialize(): void |
75 | 22 | { |
76 | | - $this->metrics = [ |
77 | | - 'accept' => 0, |
78 | | - 'reject' => 0, |
79 | | - 'error' => 0, |
80 | | - ]; |
81 | | - |
82 | | - $this->channel->queueDeclare( |
83 | | - queue: $this->topic, |
84 | | - passive: false, |
85 | | - durable: true, |
86 | | - exclusive: false, |
87 | | - autoDelete: true, |
88 | | - ); |
| 23 | + $this->acceptMetric = 0; |
| 24 | + $this->rejectMetric = 0; |
| 25 | + $this->errorMetric = 0; |
89 | 26 | } |
90 | 27 |
|
91 | 28 | public function accept(int $step = 1): void |
92 | 29 | { |
93 | | - $this->metrics['accept'] += $step; |
| 30 | + $this->acceptMetric += $step; |
94 | 31 |
|
95 | | - if ($this->metrics['accept'] === $this->messageLimit) { |
96 | | - $this->sendUpdate(); |
97 | | - |
98 | | - $this->metrics['accept'] = 0; |
99 | | - $this->metrics['reject'] = 0; |
100 | | - $this->metrics['error'] = 0; |
101 | | - } |
| 32 | + $this->manager->trySend($step); |
102 | 33 | } |
103 | 34 |
|
104 | 35 | public function reject(int $step = 1): void |
105 | 36 | { |
106 | | - $this->metrics['reject'] += $step; |
| 37 | + $this->rejectMetric += $step; |
| 38 | + |
| 39 | + $this->manager->trySend($step); |
107 | 40 | } |
108 | 41 |
|
109 | 42 | public function error(int $step = 1): void |
110 | 43 | { |
111 | | - $this->metrics['error'] += $step; |
| 44 | + $this->errorMetric += $step; |
| 45 | + |
| 46 | + $this->manager->trySend($step); |
112 | 47 | } |
113 | 48 |
|
114 | 49 | public function teardown(): void |
115 | 50 | { |
116 | | - $this->sendUpdate(); |
117 | | - |
118 | | - $this->channel->close(); |
119 | | - $this->connection->stop(); |
| 51 | + $this->manager->teardown($this); |
120 | 52 | } |
121 | 53 |
|
122 | | - private function sendUpdate(): void |
| 54 | + public function toArray(): array |
123 | 55 | { |
124 | | - $date = new \DateTime(); |
| 56 | + return [ |
| 57 | + 'code' => $this->stepCode, |
| 58 | + 'label' => $this->stepLabel ?: $this->stepCode, |
| 59 | + 'metrics' => iterator_to_array($this->walkMetrics()), |
| 60 | + ]; |
| 61 | + } |
125 | 62 |
|
126 | | - $this->channel->publish( |
127 | | - \json_encode([ |
128 | | - 'id' => $this->pipelineId, |
129 | | - 'date' => ['date' => $date->format('c'), 'tz' => $date->getTimezone()->getName()], |
130 | | - 'stepsUpdates' => [ |
131 | | - [ |
132 | | - 'code' => $this->stepCode, |
133 | | - 'label' => $this->stepLabel ?: $this->stepCode, |
134 | | - 'metrics' => [ |
135 | | - [ |
136 | | - 'code' => 'accept', |
137 | | - 'value' => $this->metrics['accept'] |
138 | | - ], |
139 | | - [ |
140 | | - 'code' => 'reject', |
141 | | - 'value' => $this->metrics['reject'] |
142 | | - ], |
143 | | - [ |
144 | | - 'code' => 'error', |
145 | | - 'value' => $this->metrics['error'] |
146 | | - ] |
147 | | - ] |
148 | | - ] |
149 | | - ] |
150 | | - ]), |
151 | | - [ |
152 | | - 'content-type' => 'application/json', |
153 | | - ], |
154 | | - $this->exchange, |
155 | | - $this->topic |
156 | | - ); |
| 63 | + private function walkMetrics(): \Generator |
| 64 | + { |
| 65 | + if ($this->acceptMetric > 0) { |
| 66 | + yield [ |
| 67 | + 'code' => 'accept', |
| 68 | + 'value' => $this->acceptMetric, |
| 69 | + ]; |
| 70 | + $this->acceptMetric = 0; |
| 71 | + } |
| 72 | + if ($this->rejectMetric > 0) { |
| 73 | + yield [ |
| 74 | + 'code' => 'reject', |
| 75 | + 'value' => $this->rejectMetric, |
| 76 | + ]; |
| 77 | + $this->rejectMetric = 0; |
| 78 | + } |
| 79 | + if ($this->errorMetric > 0) { |
| 80 | + yield [ |
| 81 | + 'code' => 'error', |
| 82 | + 'value' => $this->errorMetric, |
| 83 | + ]; |
| 84 | + $this->errorMetric = 0; |
| 85 | + } |
157 | 86 | } |
158 | 87 | } |
0 commit comments