mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-17 23:44:39 -05:00
Updated example of scheduled on off job using date
This commit is contained in:
parent
eb0eb2d744
commit
30c9112bb5
2 changed files with 23 additions and 27 deletions
|
@ -1,36 +1,24 @@
|
|||
/* eslint-disable no-console */
|
||||
|
||||
const path = require('path');
|
||||
const pWaitFor = require('p-wait-for');
|
||||
// const path = require('path');
|
||||
const setTimeoutPromise = require('util').promisify(setTimeout);
|
||||
const addSeconds = require('date-fns/addSeconds');
|
||||
const JobManager = require('../../lib/job-manager');
|
||||
|
||||
const jobManager = new JobManager({
|
||||
info: console.log,
|
||||
warn: console.log,
|
||||
error: console.log
|
||||
});
|
||||
const jobManager = new JobManager(console);
|
||||
|
||||
process.on('SIGINT', () => {
|
||||
shutdown('SIGINT');
|
||||
});
|
||||
|
||||
async function shutdown(signal) {
|
||||
console.log(`shutting down via: ${signal}`);
|
||||
|
||||
await jobManager.shutdown();
|
||||
}
|
||||
const isJobQueueEmpty = (bree) => {
|
||||
return (Object.keys(bree.workers).length === 0)
|
||||
&& (Object.keys(bree.intervals).length === 0)
|
||||
&& (Object.keys(bree.timeouts).length === 0);
|
||||
};
|
||||
|
||||
(async () => {
|
||||
jobManager.scheduleJob('in 10 seconds', () => {
|
||||
return require('../jobs/timed-job')();
|
||||
}, {
|
||||
const dateInTenSeconds = addSeconds(new Date(), 10);
|
||||
|
||||
jobManager.scheduleJob(dateInTenSeconds, path.resolve(__dirname, '../jobs/timed-job.js'), {
|
||||
ms: 2000
|
||||
}, 'one-off-scheduled-job');
|
||||
|
||||
await setTimeoutPromise(100); // allow job to get scheduled
|
||||
|
||||
await pWaitFor(() => (Object.keys(jobManager.bree.workers).length === 0) && (Object.keys(jobManager.bree.intervals).length === 0));
|
||||
await pWaitFor(() => (isJobQueueEmpty(jobManager.bree)));
|
||||
|
||||
process.exit(0);
|
||||
})();
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
const {isMainThread, parentPort, workerData} = require('bthreads');
|
||||
const util = require('util');
|
||||
const setTimeoutPromise = util.promisify(setTimeout);
|
||||
|
||||
|
@ -7,8 +8,15 @@ const passTime = async (ms) => {
|
|||
} else {
|
||||
await setTimeoutPromise(ms.ms);
|
||||
}
|
||||
|
||||
return 'done';
|
||||
};
|
||||
|
||||
module.exports = passTime;
|
||||
if (isMainThread) {
|
||||
module.exports = passTime;
|
||||
} else {
|
||||
(async () => {
|
||||
await passTime(workerData.ms);
|
||||
parentPort.postMessage('done');
|
||||
// alternative way to signal "finished" work (not recommended)
|
||||
// process.exit();
|
||||
})();
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue