Cron/Scheduler
Cron is a crucial process for running jobs at regular intervals, and Fortjs makes it remarkably easy to create and manage cron jobs.
Create Cron Job
Let's create a cron job that increments a counter every second. To create the job, follow these steps:
- Create a class that extends the
ScheduleTask
from Fortjs. - Implement the
execute
method and include your task inside it. In our case, the task is to increment thecounter
value.
import { ScheduleTask } from "fortjs";
export class CounterScheduler extends ScheduleTask {
counter = 0;
async execute() {
this.counter++;
}
}
Use Cron Job
In the previous section, we defined the cron job, but now we need to instruct Fortjs to execute this cron job based on a cron expression.
In your bootstrap file (index.ts
), add the following code:
import { CounterScheduler } from "./crons/counter";
Fort.scheduler.add({
expression: "*/1 * * * * *", // run every 1 second
task: CounterScheduler,
name: "Counter"
});
// Start all cron jobs
Fort.scheduler.startAll();
Start Cron Job
The Fortjs scheduler provides two methods to initiate the cron job:
- startAll
- start
startAll
startAll
initiates all registered cron jobs.
// start all cron job
Fort.scheduler.startAll();
start
start
takes the cron name and starts the specified cron.
Fort.scheduler.start("Counter");
Stop cron job
The Fortjs scheduler provides two methods to halt the execution of cron jobs:
- stopAll
- stop
stopAll
stopAll
stops all registered cron jobs.
// stop all cron job
Fort.scheduler.stopAll();
stop
stop
takes the cron name and stops the specified cron.
Fort.scheduler.stop("Counter");
Fortjs utilizes the cron package under the hood to execute cron tasks. However, it can be configured to use any alternative package. For more details, refer to the documentation - Cron Scheduler.