Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper way to catch up on Laravel tasks missed during maintenance / downtime?

When a Laravel 5 application goes down (planned maintenance or otherwise), but then it's brought back up a few minutes later, does Laravel automatically know to catch up and run those tasks that were missed?

If not, what is the preferred way to handle catchup for potentially missed tasks?

like image 323
Phillip Elm Avatar asked Sep 01 '25 22:09

Phillip Elm


1 Answers

It appears that the task scheduling system is a bit too simple to ensure that a task runs after downtime (say, if it runs daily or at specific times); as advertised, it literally just runs tasks on the given schedule.

In essence, downtime / maintenance makes it possible to "miss the bus".

To ensure that critical tasks run:

  • Add a model / mechanism for storing information on when critical tasks were last run
  • Instead of running critical tasks directly from the schedule system, wrap them in task(s) that run frequently to check when the actual task was last executed and then dispatches them when appropriate
    • The basic logic here being:
      • if (time() > ($task->lastRunTime + $task->intervalTime)) dispatch(...) or;
      • if (time() >= $task->nextRunTime) dispatch(...)

Based on your application, the specific implementation of this will vary; the nice thing about using a model for this is that you can easily adapt it to your application's needs, such as tracking metrics.

like image 90
Phillip Elm Avatar answered Sep 03 '25 13:09

Phillip Elm