Just Lucky Really
Just Lucky Really

Reputation: 1401

Laravel - Run scheduled task only if same previous task has finished

I was wondering if Laravel has a built in function to only run a scheduled task if the previous has finished.

I have a need to run a specific task every few seconds, but the task relies on querying another host that may at some point lose connection.

There are a couple of ways I thought about doing this, if there isn't:

  1. Having the task just put the job into the queue
  2. Having a flag saved (Either as a file or in the db), that is set at the start of the task, and unset at the finish.

Just hoping the task scheduler has something built in before I go down either of those routes ...

Upvotes: 2

Views: 2998

Answers (1)

Just Lucky Really
Just Lucky Really

Reputation: 1401

Found the answer in the Docs :D

https://laravel.com/docs/5.3/scheduling

excerpt:

Preventing Task Overlaps

By default, scheduled tasks will be run even if the previous instance of the task is still running. To prevent this, you may use the withoutOverlapping method:

$schedule->command('emails:send')->withoutOverlapping();

In this example, the emails:send Artisan command will be run every minute if it is not already running. The withoutOverlapping method is especially useful if you have tasks that vary drastically in their execution time, preventing you from predicting exactly how long a given task will take.

Upvotes: 6

Related Questions