@fastify/schedule
Version:
Fastify plugin for scheduling periodic jobs
43 lines (31 loc) • 1.73 kB
Markdown
[](https://github.com/fastify/fastify-schedule/actions/workflows/ci.yml)
[](https://www.npmjs.com/package/@fastify/schedule)
[](https://www.npmjs.com/package/@fastify/schedule)
[](https://github.com/neostandard/neostandard)
Fastify plugin for scheduling periodic jobs. Provides an instance of [toad-scheduler](https://github.com/kibertoad/toad-scheduler) on fastify instance.
Jobs are stopped automatically when the fastify instance is stopped.
First install the package:
```bash
npm i @fastify/schedule toad-scheduler
```
Next, set up the plugin:
```js
const fastify = require('fastify')();
const { fastifySchedule } = require('@fastify/schedule');
const { SimpleIntervalJob, AsyncTask } = require('toad-scheduler');
const task = new AsyncTask(
'simple task',
() => { return db.pollForSomeData().then((result) => { /* continue the promise chain */ }) },
(err) => { /* handle errors here */ }
)
const job = new SimpleIntervalJob({ seconds: 20, }, task)
fastify.register(fastifySchedulePlugin);
// `fastify.scheduler` becomes available after initialization.
// Therefore, you need to call `ready` method.
fastify.ready().then(() => {
fastify.scheduler.addSimpleIntervalJob(job)
})
```
For more detailed instructions, see the [documentation](https://github.com/kibertoad/toad-scheduler) of `toad-scheduler`.