Skip to main content
The AdonisJS Scheduler provides lifecycle hooks that allow you to execute code at different stages of task execution. You can attach hooks to individual schedules or listen to global scheduler events.

Task-level hooks

You can attach before and after hooks to individual scheduled tasks using the .before() and .after() methods.

Before hooks

The .before() method registers a callback that executes before the scheduled task runs:
You can attach multiple before hooks to a single schedule. They execute in the order they were registered:

After hooks

The .after() method registers a callback that executes after the scheduled task completes:
Multiple after hooks execute sequentially after the task completes:

Complete example

Here’s a practical example combining both hooks:

Global scheduler hooks

The scheduler provides global hooks that execute when the scheduler worker starts.

onStarting hook

The onStarting callback executes when the scheduler worker is initializing, before any tasks are scheduled:
This hook receives a context object with:
  • tag: The tag filter being used (e.g., 'default', 'reports')

onStarted hook

The onStarted callback executes after all scheduled tasks have been registered and the worker is fully operational:

onBoot hook

The onBoot callback executes during the scheduler’s boot phase, before commands are loaded:

Practical use cases

Logging and monitoring

Health checks and notifications

Error tracking

Task-level hooks (before and after) execute for each task execution, while global hooks (onStarting, onStarted, onBoot) execute once when the scheduler worker starts.