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
Properties
tasks
booted
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
- Boots the worker if not already booted
- Loads the scheduler from the container
- Calls
onStartingcallback if registered - Schedules all tasks matching the specified tag
- Calls
onStartedcallback if registered - Logs that the worker has started successfully
stop()
Stops all running scheduled tasks.boot()
Initializes the worker by loading the scheduler and command loaders. This method is called automatically bystart() and typically doesn’t need to be called manually.
- Returns early if already booted
- Loads the scheduler from the container
- Calls
scheduler.boot()to initialize schedules - Creates command loaders for executing commands
- Sets
bootedtotrue
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:scheduler:run command creates a Worker instance internally and manages its lifecycle automatically.