jreikes jreikes - 1 year ago 112
PHP Question

How do I time delay a job in Laravel 5.2?

Let's say my server sends an identical request to 5 client devices at 12:00:05. I want to wait 90 seconds (until 12:01:35) and then check which clients have responded appropriately to the request and do some other stuff. What's the best way to accomplish something like this?

Should I queue up a job and use

at the beginning? The problem is this type of job will always take at least 90 seconds to complete and the server is set by default to time out after 60 seconds. I suppose I can change the server setting, but my other jobs should still be considered to have timed out if they get past 60 seconds.

Should I queue up a scheduled task instead? The problem here is I think Laravel and cron only give you scheduling precision to the nearest minute (12:01 or 12:02, but not 12:01:35).

Answer Source

You can use delayed dispatching for your queues in Laravel . https://laravel.com/docs/master/queues#delayed-dispatching

$job = (new YourEvent($coolEvent))->delay(Carbon::now()->addSeconds(90));

This will run the task 90 seconds after it's added to queue.