helloworld
helloworld

Reputation: 195

Laravel Queue - Pause between jobs

I want to create a queue (AMAZON SQS) that only runs jobs every X sec. So if suddenly 50 jobs are submitted, the end up in the queue. The queue listener then pulls a job, does something and waits X sec. After that, the next job is pulled. Another X sec pause. Etc etc

For the queue listener, the sleep option option only determines how long the worker will "sleep" if there are no new jobs available. So it will only sleep if there is nothing in the queue.

Or should I just put in a pause(x) in my PHP code?

[edit] I just tested the sleep method with a FIFO and standard AWS SQS queue and this messes up the whole queue. Suddenly jobs are (sucesssfully) resubmitted 3 times after which the go into failed state. Moreover, the delay that is given in my code (3-4 min) was ignored, instead a one minute was taken

<?php

namespace App\Jobs;

use App\City;

class RetrieveStations extends Job
{
protected $cities;

/**
 * Create a new job instance.
 *
 * @return void
 */
public function __construct ($cities)
{
    $this->cities = $cities;
}

/**
 * Execute the job.
 *
 * @return void
 */
public function handle()
{



    // code here
      doSomething()
      sleep(X);


}
}

Upvotes: 2

Views: 3046

Answers (3)

Bojan S.
Bojan S.

Reputation: 323

You can add --rest to your queue:work command. This will pause for 10 seconds between each job: php artisan queue:work --rest=10

https://github.com/laravel/framework/blob/100ed71fe11a25908539c57368a048ec80c6123d/src/Illuminate/Queue/WorkerOptions.php#L47

Upvotes: 0

Sander Kruger
Sander Kruger

Reputation: 11

I have the exact same problem to solve. I'm using Laravel 5.8 and I don't see how I can get the queue worker to wait a fixed period between jobs.

I'm now thinking of using a scheduled task to handle this. I can schedule a task to run, say, every 5 minutes and run the following artisan command:

$schedule->command('queue:work --queue=emails --once')->everyFiveMinutes();

This will take one job from the queue and run it. Unfortunately, there's not much more granular control over how often a job is processed.

Upvotes: 1

Stefano Maglione
Stefano Maglione

Reputation: 4180

Exactly, you need to set asleep your php code, there is no other way.

Php sleep

Upvotes: 0

Related Questions