Run Laravel queue with specified delay between each job
On Laravel (8) App, i have to send Http post requests to an external API that throttles to 1 request per 2 seconds and we have usually send the requests in batches of hundreds.
So is there a way i can send one request per 3 seconds (using jobdelay perhaps?)
I believe rate limit "blocks" the next job after the limit has been reached. What I am trying to implement is where every job executes and has a delay of fixed seconds before the next job kicks in. I have done this and it seems to work:
$start = Carbon::now();
$employees = StagedEmployees::all();
foreach ($employees as $emp) {
$job = new PostEmployeeToMinistryOfLabor($emp);
$job->delay($start->addSeconds(3));
dispatch($job);
}
[The fact that the changes on Carbon instance ( addSeconds() ) is mutable kinda helps.] So the jobs get scheduled with delay but it only works well if the queue is working currently and it ignores the time taken by the job to finish. I am wondering if there is a recommended way to achieve this.