node-cron无法正常工作?

时间:2022-05-10 08:04:48

I deployed an app on Heroku: myapp

我在Heroku上部署了一个应用程序:myapp

and I wrote some cron code in it:

我在里面写了一些cron代码:

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.

我的目的是在特定时间每天调用名为'clearReserve'的函数。但它只能在我上传代码到heroku的第一天起作用,而且再也不会执行此cron作业。

PS: this "clearReserve" function will manipulate my database, I use MongoLab URI which I created in MongoLab, not the Heroku add-on;

PS:这个“clearReserve”函数会操纵我的数据库,我使用的是我在MongoLab中创建的MongoLab URI,而不是Heroku附加组件;

1 个解决方案

#1


2  

Your cron job is never ran again because Heroku dyno went to sleep. https://devcenter.heroku.com/articles/dynos#dyno-sleeping

你的cron工作再也没有跑过,因为Heroku dyno去睡觉了。 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.

为了让您的cron作业得以执行,您需要让Heroku应用程序保持全天候醒来状态。要确保您的Heroku应用程序不会进入睡眠状态,请安装NewRelic Heroku Addon,您可以设置可用性监视。您提供NewRelic每30秒ping一次的URL,从而保持您的应用程序清醒。此功能的目的是在出于业务和性能原因导致您的网站出现故障时提醒您,但它还具有防止您的应用空闲的额外好处。

Hope it helps!

希望能帮助到你!

#1


2  

Your cron job is never ran again because Heroku dyno went to sleep. https://devcenter.heroku.com/articles/dynos#dyno-sleeping

你的cron工作再也没有跑过,因为Heroku dyno去睡觉了。 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.

为了让您的cron作业得以执行,您需要让Heroku应用程序保持全天候醒来状态。要确保您的Heroku应用程序不会进入睡眠状态,请安装NewRelic Heroku Addon,您可以设置可用性监视。您提供NewRelic每30秒ping一次的URL,从而保持您的应用程序清醒。此功能的目的是在出于业务和性能原因导致您的网站出现故障时提醒您,但它还具有防止您的应用空闲的额外好处。

Hope it helps!

希望能帮助到你!