简体   繁体   中英

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);


}
}

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.

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

Php sleep

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM