Skip to main content
The Worker class is responsible for executing scheduled tasks at their defined intervals. It manages the lifecycle of scheduled jobs and handles the actual execution of commands and callbacks.

Constructor

ApplicationService
required
The AdonisJS application service instance
Example

Properties

tasks

An array of active cron scheduled tasks. These are managed internally by the Worker.

booted

Indicates whether the worker has been booted. The worker boots automatically when start() is called for the first time.

Methods

start()

Starts the worker and begins executing scheduled tasks for a specific tag.
string
default:"'default'"
The tag to filter schedules by. Only schedules with this tag will be executed
This method:
  1. Boots the worker if not already booted
  2. Loads the scheduler from the container
  3. Calls onStarting callback if registered
  4. Schedules all tasks matching the specified tag
  5. Calls onStarted callback if registered
  6. Logs that the worker has started successfully
Example

stop()

Stops all running scheduled tasks.
This method stops all active cron tasks, effectively shutting down the scheduler. Example

boot()

Initializes the worker by loading the scheduler and command loaders. This method is called automatically by start() and typically doesn’t need to be called manually.
This method:
  1. Returns early if already booted
  2. Loads the scheduler from the container
  3. Calls scheduler.boot() to initialize schedules
  4. Creates command loaders for executing commands
  5. Sets booted to true

Usage Example

Here’s a complete example of using the Worker class in a custom script:

Multiple Workers

You can run multiple workers with different tags simultaneously:

Integration with AdonisJS

In most cases, you won’t create Worker instances directly. Instead, use the CLI commands provided by the package:
The scheduler:run command creates a Worker instance internally and manages its lifecycle automatically.