Skip to content

Instantly share code, notes, and snippets.

@Quaggie
Last active January 6, 2017 13:50
Show Gist options
  • Save Quaggie/9165fdc2b6ed9b1acee1eb988dfa2e6a to your computer and use it in GitHub Desktop.
Save Quaggie/9165fdc2b6ed9b1acee1eb988dfa2e6a to your computer and use it in GitHub Desktop.
'use strict';
const moment = require('moment');
const { CronJob } = require('cron');
function callback (job) {
}
exports.callback = function () {
/*
Seconds: 0-59
Minutes: 0-59
Hours: 0-23
Day of Month: 1-31
Months: 0-11
Day of Week: 0-6
*/
const sixHours = moment().add(6, 'hours').toDate();
const fiveSeconds = moment().add(5, 'seconds').toDate();
const time = process.env.NODE_ENV === 'production' ? sixHours : fiveSeconds;
runJob(time, callback);
};
// date:
function runJob (date, cb, killJobOnComplete) {
const job = new CronJob({
cronTime: date,
onTick: function () {
cb().then( () => job.stop())
},
onComplete: () => {
console.log('COMPLETED ~~~~');
if (!killJobOnComplete) {
const sixHours = moment().add(6, 'hours').toDate();
const fiveMinutes = moment().add(5, 'minutes').toDate();
const time = process.env.NODE_ENV === 'production' ? sixHours : fiveMinutes;
runJob(time, cb)
}
},
start: false,
timezone: 'America/Sao_Paulo',
context: null,
runOnInit: false
});
job.start();
}
exports.runJob = runJob;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment