loopback cron code example

Example: loopback cron job

npm install --save @loopback/cron
...
// Start by importing the component class:
import {CronComponent} from '@loopback/cron';

// In the constructor, add the component to your application:
this.component(CronComponent);
...
import {CronJob, asCronJob} from '@loopback/cron';

// Create an cron job
const job = new CronJob({
	cronTime: '*/1 * * * * *', // Every one second
    onTick: () => {
		// Do the work
	},
	start: true, // Start the job immediately
});

// Bind the cron job as an extension for the scheduler
app.bind('cron.jobs.job1').to(job).apply(asCronJob);

Tags:

Misc Example