Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for blocking channel->wait #414

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Console/ConsumeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class ConsumeCommand extends WorkCommand
{--consumer-tag}
{--prefetch-size=0}
{--prefetch-count=1000}
{--non-blocking=true}
';

protected $description = 'Consume messages';
Expand All @@ -41,6 +42,7 @@ public function handle(): void
$consumer->setConsumerTag($this->consumerTag());
$consumer->setPrefetchSize((int) $this->option('prefetch-size'));
$consumer->setPrefetchCount((int) $this->option('prefetch-count'));
$consumer->setNonblocking((bool) $this->option('non-blocking') == 'false' ? false : true);

parent::handle();
}
Expand Down
12 changes: 11 additions & 1 deletion src/Consumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Queue\WorkerOptions;
use PhpAmqpLib\Channel\AMQPChannel;
use PhpAmqpLib\Exception\AMQPRuntimeException;
use PhpAmqpLib\Exception\AMQPTimeoutException;
use PhpAmqpLib\Message\AMQPMessage;
use Throwable;
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\RabbitMQQueue;
Expand All @@ -26,6 +27,9 @@ class Consumer extends Worker
/** @var int */
protected $prefetchCount;

/** @var bool */
protected $nonblocking = true;

/** @var AMQPChannel */
protected $channel;

Expand All @@ -52,6 +56,11 @@ public function setPrefetchCount(int $value): void
$this->prefetchCount = $value;
}

public function setNonblocking(bool $value): void
{
$this->nonblocking = $value;
}

/**
* Listen to the given queue in a loop.
*
Expand Down Expand Up @@ -127,7 +136,8 @@ function (AMQPMessage $message) use ($connection, $options, $connectionName, $qu

// If the daemon should run (not in maintenance mode, etc.), then we can wait for a job.
try {
$this->channel->wait(null, true, (int) $options->timeout);
$this->channel->wait(null, $this->nonblocking, (int) $options->timeout);
} catch (AMQPTimeoutException $exception) {
} catch (AMQPRuntimeException $exception) {
$this->exceptions->report($exception);

Expand Down