Skip to main content
Version: v1

Scheduling Jobs

You can schedule jobs using shell scripts and the node-schedule library.

Example

scripts/fetch-metrics.ts

export function main(args: any) {
// Do some stuff
}

scripts/schedule-jobs.ts

// 3p
import { scheduleJob } from 'node-schedule';
import { main as fetchMetrics } from './fetch-metrics';

export async function main(args: any) {
console.log('Scheduling the job...');

// Run the fetch-metrics script every day at 10:00 AM.
scheduleJob(
{ hour: 10, minute: 0 },
() => fetchMetrics(args)
);

console.log('Job scheduled!');
}

Schedule the job(s):

npm run build:scripts
foal run schedule-jobs arg1=value1

Background Jobs with pm2

While the above command works, it does not run the scheduler and the jobs in the background. To do this, you can use pm2, a popular process manager for Node.js.

First you need to install locally the Foal CLI:

npm install @foal/cli

Then you can run the scheduler like this:

pm2 start ./node_modules/.bin/foal --name scheduler -- run schedule-jobs arg1=value1

If everything works fine, you should see your scheduler running with this command:

pm2 ls

To display the logs of the scheduler and the jobs, use this one:

pm2 logs scheduler

Eventually, to stop the scheduler and the jobs, you can use this command:

pm2 delete scheduler