Last active
January 6, 2017 13:50
-
-
Save Quaggie/9165fdc2b6ed9b1acee1eb988dfa2e6a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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