Ghost/ghost/job-manager/test/examples/graceful-shutdown.js
Naz 34411cfcc1 Improved documentation and job examples
refs  #122

- As job signature has changed in 3bca4f63b8 and documentation needed updates accordingly
2021-01-07 15:17:38 +13:00

36 lines
879 B
JavaScript

/* eslint-disable no-console */
const pWaitFor = require('p-wait-for');
const path = require('path');
const setTimeoutPromise = require('util').promisify(setTimeout);
const JobManager = require('../../lib/job-manager');
const jobManager = new JobManager({
info: console.log,
warn: console.log,
error: console.log
});
process.on('SIGINT', () => {
shutdown('SIGINT');
});
async function shutdown(signal) {
console.log(`shutting down via: ${signal}`);
await jobManager.shutdown();
}
(async () => {
jobManager.addJob({
at: 'every 10 seconds',
job: path.resolve(__dirname, '../jobs/graceful.js')
});
await setTimeoutPromise(100); // allow job to get scheduled
await pWaitFor(() => (Object.keys(jobManager.bree.workers).length === 0) && (Object.keys(jobManager.bree.intervals).length === 0));
process.exit(0);
})();