This package is available in the npm registry.
npm i @stouder-io/adonis-scheduler
Next, configure the package by running the following command.
node ace configure @stouder-io/adonis-scheduler
The scheduler will start with your Adonis server, but no task is loaded.
To create a task, run the following command.
node ace make:task MyFirstTask
This will create the following file under app/Tasks
directory:
import { TaskContract } from '@ioc:StouderIO/Scheduler'
export default class MyFirstTask implements TaskContract {
public readonly name: string = '{{ name }}'
public readonly cron: string = '* * * * *'
public async run(): Promise<void> {
}
}
The run
method is called when the scheduler run the task according to the cron expression you configure in the cron
field. name
is just for internal use, but it must be unique.
Please note that running CPU-intensive operations in the task has the potential to block your full web application as Node.js is single-threaded.
If you need to run high-intensive CPU task, you could run an Ace command using execa
, for example:
import { TaskContract } from '@ioc:StouderIO/Scheduler'
import execa from 'execa'
export default class MyFirstTask implements TaskContract {
public readonly name: string = 'test-task'
public readonly cron: string = '* * * * *'
public async run(): Promise<void> {
// will execute `node ace intensive` every minute
execa.node('ace', ['intensive'], { stdio: 'inherit' })
}
}
Also, if you run multiple instances of your Adonis application, the tasks will be ran on each instance.