Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 sleep(90) 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).

like image 581
jreikes Avatar asked Sep 19 '16 22:09

jreikes


People also ask

Are laravel jobs asynchronous?

php - Laravel Jobs are not asynchronous - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.

What is the difference between job and queue in laravel?

Jobs and QueuesThe line itself is the Queue, and each customer in the line is a Job. In order to process Jobs in the Queue you need command line processes or daemons. Think of launching a queue daemon on the command line as adding a new bank teller to the pool of available bank tellers.

What is laravel queue sync?

Sync, or synchronous, is the default queue driver which runs a queued job within your existing process. With this driver enabled, you effectively have no queue as the queued job runs immediately.

What is queue job in laravel?

Laravel queues provide a unified queueing API across a variety of different queue backends, such as Amazon SQS, Redis, or even a relational database. Laravel's queue configuration options are stored in your application's config/queue.php configuration file.


1 Answers

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.

like image 177
Can Celik Avatar answered Sep 21 '22 09:09

Can Celik