Delay the job: https://laravel.com/docs/5.2/queues#delayed-jobs or just use the pause() function of PHP.
Delay between processes in Job Queue
Hi,
I'm working on a project that use the OpenWeather API. On OpenWeather is possible to obtain an API key for free if there are no more than 60 request per minute.
For my project, I need to retrieve the current weather data for 2'000 cities each hour. So I've thought I could work with the Queue function of Laravel, which run the requests in background and retry to run the task in case of errors.
Here the question, is it possible to limit the Job Queue in order to run only one process each second (and not 3 or 4 per second)? If yes, how could I do that?
Thank you very much for the help!
Davide
I had the very same problem but even sleep wasn't enough for me because if 10 users did say update at the same time then sleep will sleep them at the same time and the delay start at the same time therefore the execution happens at the same time. i.e too much request to the API.
What i did is this.
public function update(Request $request, $id)
{
$delay = \DB::table('jobs')->count()*10;
$queue = new Update($request->all(), $id, \Auth::user()->email);
$this->dispatch($queue->delay($delay));
}
in this case if the queue is empty then delay is 0. If the queue has Y items then next item will be delayed by (Y*10). 10 sec from the last item.
Please or to participate in this conversation.