1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Queue; |
6
|
|
|
|
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use Yiisoft\Queue\Adapter\AdapterInterface; |
9
|
|
|
use Yiisoft\Queue\Cli\LoopInterface; |
10
|
|
|
use Yiisoft\Queue\Enum\JobStatus; |
11
|
|
|
use Yiisoft\Queue\Exception\AdapterConfiguration\AdapterNotConfiguredException; |
12
|
|
|
use Yiisoft\Queue\Message\MessageInterface; |
13
|
|
|
use Yiisoft\Queue\Middleware\AdapterHandler; |
14
|
|
|
use Yiisoft\Queue\Middleware\MessageHandlerInterface; |
15
|
|
|
use Yiisoft\Queue\Middleware\MiddlewareInterface; |
16
|
|
|
use Yiisoft\Queue\Middleware\MiddlewareDispatcher; |
17
|
|
|
use Yiisoft\Queue\Middleware\Request; |
18
|
|
|
use Yiisoft\Queue\Worker\WorkerInterface; |
19
|
|
|
use Yiisoft\Queue\Message\IdEnvelope; |
20
|
|
|
|
21
|
|
|
final class Queue implements QueueInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var array|array[]|callable[]|MiddlewareInterface[]|string[] |
25
|
|
|
*/ |
26
|
|
|
private array $middlewareDefinitions; |
27
|
|
|
private AdapterHandler $adapterHandler; |
28
|
|
|
|
29
|
18 |
|
public function __construct( |
30
|
|
|
private WorkerInterface $worker, |
31
|
|
|
private LoopInterface $loop, |
32
|
|
|
private LoggerInterface $logger, |
33
|
|
|
private MiddlewareDispatcher $pushMiddlewareDispatcher, |
34
|
|
|
private ?AdapterInterface $adapter = null, |
35
|
|
|
private string $channelName = QueueFactoryInterface::DEFAULT_CHANNEL_NAME, |
36
|
|
|
MiddlewareInterface|callable|array|string ...$middlewareDefinitions |
37
|
|
|
) { |
38
|
18 |
|
$this->middlewareDefinitions = $middlewareDefinitions; |
39
|
18 |
|
$this->adapterHandler = new AdapterHandler(); |
40
|
|
|
} |
41
|
|
|
|
42
|
2 |
|
public function getChannelName(): string |
43
|
|
|
{ |
44
|
2 |
|
return $this->channelName; |
45
|
|
|
} |
46
|
|
|
|
47
|
9 |
|
public function push( |
48
|
|
|
MessageInterface $message, |
49
|
|
|
MiddlewareInterface|callable|array|string ...$middlewareDefinitions |
50
|
|
|
): MessageInterface { |
51
|
9 |
|
$this->logger->debug( |
52
|
9 |
|
'Preparing to push message with handler name "{handlerName}".', |
53
|
9 |
|
['handlerName' => $message->getHandlerName()] |
54
|
9 |
|
); |
55
|
|
|
|
56
|
9 |
|
$request = new Request($message, $this->adapter); |
57
|
9 |
|
$message = $this->pushMiddlewareDispatcher |
58
|
9 |
|
->dispatch($request, $this->createHandler($middlewareDefinitions)) |
59
|
9 |
|
->getMessage(); |
60
|
|
|
|
61
|
8 |
|
$messageId = $message->getMetadata()[IdEnvelope::MESSAGE_ID_KEY] ?? 'null'; |
62
|
8 |
|
$this->logger->info( |
63
|
8 |
|
'Pushed message with handler name "{handlerName}" to the queue. Assigned ID #{id}.', |
64
|
8 |
|
['handlerName' => $message->getHandlerName(), 'id' => $messageId] |
65
|
8 |
|
); |
66
|
|
|
|
67
|
8 |
|
return $message; |
68
|
|
|
} |
69
|
|
|
|
70
|
5 |
|
public function run(int $max = 0): void |
71
|
|
|
{ |
72
|
5 |
|
$this->checkAdapter(); |
73
|
|
|
|
74
|
4 |
|
$this->logger->debug('Start processing queue messages.'); |
75
|
4 |
|
$count = 0; |
76
|
|
|
|
77
|
4 |
|
$handlerCallback = function (MessageInterface $message) use (&$max, &$count): bool { |
78
|
4 |
|
if (($max > 0 && $max <= $count) || !$this->handle($message)) { |
79
|
1 |
|
return false; |
80
|
|
|
} |
81
|
4 |
|
$count++; |
82
|
|
|
|
83
|
4 |
|
return true; |
84
|
4 |
|
}; |
85
|
|
|
|
86
|
|
|
/** @psalm-suppress PossiblyNullReference */ |
87
|
4 |
|
$this->adapter->runExisting($handlerCallback); |
|
|
|
|
88
|
|
|
|
89
|
4 |
|
$this->logger->info( |
90
|
4 |
|
'Processed {count} queue messages.', |
91
|
4 |
|
['count' => $count] |
92
|
4 |
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
public function listen(): void |
96
|
|
|
{ |
97
|
1 |
|
$this->checkAdapter(); |
98
|
|
|
|
99
|
1 |
|
$this->logger->info('Start listening to the queue.'); |
100
|
|
|
/** @psalm-suppress PossiblyNullReference */ |
101
|
1 |
|
$this->adapter->subscribe(fn (MessageInterface $message) => $this->handle($message)); |
102
|
1 |
|
$this->logger->info('Finish listening to the queue.'); |
103
|
|
|
} |
104
|
|
|
|
105
|
2 |
|
public function status(string|int $id): JobStatus |
106
|
|
|
{ |
107
|
2 |
|
$this->checkAdapter(); |
108
|
|
|
|
109
|
|
|
/** @psalm-suppress PossiblyNullReference */ |
110
|
2 |
|
return $this->adapter->status($id); |
111
|
|
|
} |
112
|
|
|
|
113
|
9 |
|
public function withAdapter(AdapterInterface $adapter): self |
114
|
|
|
{ |
115
|
9 |
|
$new = clone $this; |
116
|
9 |
|
$new->adapter = $adapter; |
117
|
|
|
|
118
|
9 |
|
return $new; |
119
|
|
|
} |
120
|
|
|
|
121
|
6 |
|
public function getAdapter(): ?AdapterInterface |
122
|
|
|
{ |
123
|
6 |
|
return $this->adapter; |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
public function withMiddlewares(MiddlewareInterface|callable|array|string ...$middlewareDefinitions): self |
127
|
|
|
{ |
128
|
1 |
|
$instance = clone $this; |
129
|
1 |
|
$instance->middlewareDefinitions = $middlewareDefinitions; |
130
|
|
|
|
131
|
1 |
|
return $instance; |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
public function withMiddlewaresAdded(MiddlewareInterface|callable|array|string ...$middlewareDefinitions): self |
135
|
|
|
{ |
136
|
1 |
|
$instance = clone $this; |
137
|
1 |
|
$instance->middlewareDefinitions = [...array_values($instance->middlewareDefinitions), ...array_values($middlewareDefinitions)]; |
138
|
|
|
|
139
|
1 |
|
return $instance; |
140
|
|
|
} |
141
|
|
|
|
142
|
2 |
|
public function withChannelName(string $channel): self |
143
|
|
|
{ |
144
|
2 |
|
$instance = clone $this; |
145
|
2 |
|
$instance->channelName = $channel; |
146
|
|
|
|
147
|
2 |
|
return $instance; |
148
|
|
|
} |
149
|
|
|
|
150
|
5 |
|
private function handle(MessageInterface $message): bool |
151
|
|
|
{ |
152
|
5 |
|
$this->worker->process($message, $this); |
153
|
|
|
|
154
|
5 |
|
return $this->loop->canContinue(); |
155
|
|
|
} |
156
|
|
|
|
157
|
7 |
|
private function checkAdapter(): void |
158
|
|
|
{ |
159
|
7 |
|
if ($this->adapter === null) { |
160
|
1 |
|
throw new AdapterNotConfiguredException(); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
9 |
|
private function createHandler(array $middlewares): MessageHandlerInterface |
165
|
|
|
{ |
166
|
9 |
|
return new class ( |
167
|
9 |
|
$this->adapterHandler, |
168
|
9 |
|
$this->pushMiddlewareDispatcher, |
169
|
9 |
|
array_merge($this->middlewareDefinitions, $middlewares) |
170
|
9 |
|
) implements MessageHandlerInterface { |
171
|
|
|
public function __construct( |
172
|
|
|
private AdapterHandler $adapterHandler, |
173
|
|
|
private MiddlewareDispatcher $dispatcher, |
174
|
|
|
private array $middlewares, |
175
|
|
|
) { |
176
|
9 |
|
} |
177
|
|
|
|
178
|
|
|
public function handle(Request $request): Request |
179
|
|
|
{ |
180
|
9 |
|
return $this->dispatcher |
181
|
9 |
|
->withMiddlewares($this->middlewares) |
182
|
9 |
|
->dispatch($request, $this->adapterHandler); |
183
|
|
|
} |
184
|
9 |
|
}; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.