node-cron does not work properly?

回眸只為那壹抹淺笑 提交于 2021-01-28 10:29:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!