0.0.3 • Published 4 years ago
@sprdv/hapi-cron v0.0.3
hapi-cron
Hapi plugin to setup cron jobs, based on the node-cron module.
Installation
hapi-cron can be installed using npm
or yarn
.
npm install @sprdv/hapi-cron
Usage
This plugin can be registered like any other:
'use strict';
const Hapi = require('@hapi/hapi');
const init = async () => {
const server = Hapi.server();
await server.register({
plugin: require('@sprdv/hapi-cron'),
options: {
timezone: 'Europe/Zurich'
}
});
await server.start();
console.log('Server running on %s', server.info.uri);
};
init();
Options
The following options are available:
- timezone: a valid timezone.
Documentation
Registering jobs
Pre-registered jobs will be started along with the server:
server.jobs.add({
name: 'job-name',
time: '* * * * * *',
onTick: () => console.log('This job rocks!')
});
The following options are available:
- name: a unique job name (required).
- time: a valid cron pattern (required).
- onTick: a function to fire at the specified time (required).
- onComplete: a function to fire when the job is stopped.
Starting jobs
If you add a job when the server is already running, you will need to start it manually:
server.jobs.get('job-name').start();
Stopping jobs
When the server stops, all running jobs will be stopped automatically. However, you can stop any job whenever you want:
server.jobs.get('job-name').stop();