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

Implement worker to sleep when no job available #1

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 8 additions & 0 deletions src/Consumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class Consumer extends Worker
/** @var AMQPChannel */
protected $channel;

/** @var boolean */
protected $gotJob = false;

public function setContainer(Container $value): void
{
$this->container = $value;
Expand Down Expand Up @@ -79,6 +82,7 @@ public function daemon($connectionName, $queue, WorkerOptions $options): void
false,
false,
function (AMQPMessage $message) use ($connection, $options, $connectionName, $queue): void {
$this->gotJob = true;
$job = new RabbitMQJob(
$this->container,
$connection,
Expand Down Expand Up @@ -108,7 +112,11 @@ function (AMQPMessage $message) use ($connection, $options, $connectionName, $qu
// fire off this job for processing. Otherwise, we will need to sleep the
// worker so no more jobs are processed until they should be processed.
try {
$this->gotJob = false;
$this->channel->wait(null, true, (int) $options->timeout);
if ($this->gotJob == false) {
$this->sleep($options->sleep > 0 ? $options->sleep : 1);
}
} catch (AMQPRuntimeException $exception) {
$this->exceptions->report($exception);

Expand Down