Final Class Yiisoft\Queue\QueueConsumer
| Inheritance | Yiisoft\ |
|---|---|
| Implements | Yiisoft\ |
Consumes messages for one logical queue.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\ |
|
| listen() | Yiisoft\ |
|
| run() | Yiisoft\ |
Method Details
| public mixed __construct ( Yiisoft\ | ||
| $worker | Yiisoft\ |
|
| $loop | Yiisoft\ |
|
| $logger | \ |
|
| $adapter | ?\ |
|
| $queueName | string|\ |
|
public function __construct(
private readonly WorkerInterface $worker,
private readonly LoopInterface $loop,
private readonly LoggerInterface $logger,
private readonly ?AdapterInterface $adapter = null,
string|BackedEnum $queueName = DefaultQueue::NAME,
) {
$this->queueName = StringNormalizer::normalize($queueName);
}
| public void listen ( ) |
public function listen(): void
{
if ($this->adapter === null) {
$this->logger->info('Cannot listen without an adapter. Queue is in synchronous mode.');
return;
}
$this->logger->info('Start listening to the queue.');
$this->adapter->subscribe(fn(MessageInterface $message): bool => $this->handle($message));
$this->logger->info('Finish listening to the queue.');
}
| public integer run ( integer $max = 0 ) | ||
| $max | integer | |
public function run(int $max = 0): int
{
if ($this->adapter === null) {
$this->logger->debug('Queue is in synchronous mode (no adapter). Messages are processed on push. run() does nothing.');
return 0;
}
$this->logger->debug('Start processing queue messages.');
$count = 0;
$this->adapter->runExisting(function (MessageInterface $message) use (&$count, $max): bool {
if (($max > 0 && $count >= $max) || !$this->handle($message)) {
return false;
}
$count++;
return true;
});
$this->logger->info('Processed {count} queue messages.', ['count' => $count]);
return $count;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.