Last active
December 6, 2016 17:43
-
-
Save ejallday/660e1325def134c31f35489226d1603f to your computer and use it in GitHub Desktop.
node-schedule-bug-workaround
This file contains hidden or 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
// email-blast-cron.spec.js | |
'use strict'; | |
const emailBlastCron = require('../lib/email-blast-cron'); | |
const chai = require('chai'); | |
const expect = chai.expect; | |
const request = require('request-promise'); | |
const sinon = require('sinon'); | |
describe('EmailBlastCron.startAllJobs()', function() { | |
let postRequestStub; | |
let clock; | |
let now = Date.now(); | |
before(function(){ | |
clock = sinon.useFakeTimers(); | |
clock.tick(now); | |
postRequestStub = sinon.stub(request, 'post').returns(Promise.resolve(true)); | |
}); | |
after(function(){ | |
clock.restore(); | |
request.post.restore(); | |
}); | |
it('does not fire off twice on Sunday', function(done){ | |
emailBlastCron.startAllJobs(); | |
clock.tick(60 * 60 * 24 * 7 * 1000); | |
// This test passes. Called exactly 7 times. | |
expect(postRequestStub).to.have.callCount(7); | |
done(); | |
}); | |
}); | |
// email-blast-cron.js | |
'use strict'; | |
require('dotenv').load(); | |
const schedule = require('node-schedule'); | |
const rp = require('request-promise'); | |
const EmailBlastCron = { | |
// note - All cron times are in UTC to match our EC2 instances | |
startAllJobs: function() { | |
this.SundayJob(); | |
this.MondayJob(); | |
this.TuesdayJob(); | |
this.WednesdayJob(); | |
this.ThursdayJob(); | |
this.FridayJob(); | |
this.SaturdayJob(); | |
}, | |
SundayJob: function() { | |
schedule.scheduleJob({hour: 14, minute: 0, dayOfWeek: 0}, ()=> { | |
console.log('Sunday starting'); | |
this.jobCallback(); | |
}); | |
}, | |
MondayJob: function() { | |
schedule.scheduleJob({hour: 14, minute: 0, dayOfWeek: 1}, ()=> { | |
console.log('Monday starting'); | |
this.jobCallback(); | |
}); | |
}, | |
TuesdayJob: function() { | |
schedule.scheduleJob({hour: 14, minute: 0, dayOfWeek: 2}, ()=> { | |
console.log('Tuesday starting'); | |
this.jobCallback(); | |
}); | |
}, | |
WednesdayJob: function() { | |
schedule.scheduleJob({hour: 14, minute: 0, dayOfWeek: 3}, ()=> { | |
console.log('Wednesday starting'); | |
this.jobCallback(); | |
}); | |
}, | |
ThursdayJob: function() { | |
schedule.scheduleJob({hour: 23, minute: 0, dayOfWeek: 4}, ()=> { | |
console.log('Thursday starting'); | |
this.jobCallback(); | |
}); | |
}, | |
FridayJob: function() { | |
schedule.scheduleJob({hour: 23, minute: 0, dayOfWeek: 5}, ()=> { | |
console.log('Friday starting'); | |
this.jobCallback(); | |
}); | |
}, | |
SaturdayJob: function() { | |
schedule.scheduleJob({hour: 14, minute: 0, dayOfWeek: 6}, ()=> { | |
console.log('Saturday starting'); | |
this.jobCallback(); | |
}); | |
}, | |
jobCallback: function() { | |
var options = { | |
uri: process.env.EMAIL_BLAST_URI, | |
headers: { 'email-token': process.env.EMAIL_BLAST_TOKEN } | |
}; | |
console.log('-----------------------EMAIL BLAST--------------------') | |
console.log('Firing off email blast'); | |
console.log('Date: ', new Date()); | |
console.log('------------------------------------------------------') | |
return rp.post(options) | |
.then(function (body) { | |
console.log('Cron EMAIL SUCCESS!', body, new Date()); | |
}) | |
.catch(function (err) { | |
console.log('Cron EMAIL FAILURE', err, new Date()); | |
}); | |
} | |
} | |
module.exports = EmailBlastCron; | |
// index.js | |
"use strict"; | |
const emailBlastCron = require('./lib/email-blast-cron') | |
process.stdin.resume(); | |
emailBlastCron.startAllJobs(); | |
process.on('SIGINT', function() { | |
console.log('Thanks for CTRL+Cing, exiting...'); | |
process.exit(2); | |
}); | |
process.on('exit', function() { | |
console.log('EXITING!!!!'); | |
}); | |
process.on('uncaughtException', function(e) { | |
console.log('uncaughtException', e); | |
process.exit(99); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment