-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathSyncQueueProducer.php
More file actions
58 lines (50 loc) · 1.98 KB
/
Copy pathSyncQueueProducer.php
File metadata and controls
58 lines (50 loc) · 1.98 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
<?php
declare(strict_types=1);
namespace Yiisoft\Queue;
use BackedEnum;
use Psr\Log\LoggerInterface;
use Yiisoft\Queue\Message\MessageInterface;
use Yiisoft\Queue\Middleware\Push\PushMiddlewareConfig;
use Yiisoft\Queue\Middleware\Push\PushMiddlewareDispatcher;
use Yiisoft\Queue\Middleware\Push\SynchronousPushHandler;
use Yiisoft\Queue\Worker\WorkerInterface;
/**
* Producer that runs each message synchronously, in the same process as the caller.
*/
final class SyncQueueProducer implements QueueProducerInterface
{
private string $queueName;
private PushMiddlewareDispatcher $dispatcher;
/**
* @param mixed[] $middlewareDefinitions Queue-specific push middleware definitions.
*/
public function __construct(
private readonly LoggerInterface $logger,
PushMiddlewareConfig $middlewareConfig,
WorkerInterface $worker,
string|BackedEnum $queueName = DefaultQueue::NAME,
array $middlewareDefinitions = [],
) {
$this->queueName = StringNormalizer::normalize($queueName);
$this->dispatcher = new PushMiddlewareDispatcher(
middlewareFactory: $middlewareConfig->middlewareFactory,
middlewareDefinitions: [...$middlewareConfig->commonMiddlewareDefinitions, ...$middlewareDefinitions],
finishHandler: new SynchronousPushHandler($worker, $this),
);
}
public function getQueueName(): string
{
return $this->queueName;
}
public function push(MessageInterface $message): MessageInterface
{
$this->logger->debug('Preparing to push message with message type "{messageType}".', ['messageType' => $message->getType()]);
$message = $this->dispatcher->dispatch($message);
$this->logger->info('Processed message with message type "{messageType}" synchronously.', ['messageType' => $message->getType()]);
return $message;
}
public function status(string|int $id): MessageStatus
{
return MessageStatus::NOT_FOUND;
}
}