问题
I deployed an app on Heroku: myapp
and I wrote some cron code in it:
var time = require('time');
var CronJob = require('cron').CronJob;
var job = new CronJob({
cronTime: '00 45 12 * * *',
onTick: clearReserve,
start: true,
timeZone: 'Asia/Shanghai'
});
My purpose is call the function named 'clearReserve' everyday in the specific time. but it only work in the first day I upload my code to heroku, and never do this cron job again.
PS: this "clearReserve" function will manipulate my database, I use MongoLab URI which I created in MongoLab, not the Heroku add-on;
回答1:
Your cron job is never ran again because Heroku dyno went to sleep. https://devcenter.heroku.com/articles/dynos#dyno-sleeping
In order to have your cron jobs executed you need to keep Heroku app awake 24/7. To make sure your Heroku app does not go to sleep, install the NewRelic Heroku Addon you can set up availability monitoring. You provide a URL which NewRelic pings every 30 seconds, therefore keeping your app awake. The intended purpose of this feature is to alert you if your site goes down for business and performance reasons, but it has the added benefit of preventing your app from idling.
Hope it helps!
来源:https://stackoverflow.com/questions/27289269/node-cron-does-not-work-properly