本文介绍了node-cron无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I deployed an app on Heroku: myapp

然后我在其中写了一些cron代码:

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'
});

我的目的是每天在特定时间调用名为"clearReserve"的函数.但是它只在我将代码上传到heroku的第一天就可以使用,而不再执行此cron工作.

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:此"clearReserve"功能将操纵我的数据库,我使用在MongoLab中创建的MongoLab URI,而不是Heroku附加组件;

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

推荐答案

您的cron作业再也没有运行过,因为Heroku dyno入睡了. https://devcenter.heroku.com/articles/dynos#dyno-sleeping

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

为了执行您的cron作业,您需要将Heroku应用保持24/7唤醒.为了确保您的Heroku应用程序不会进入睡眠状态,请安装NewRelic Heroku插件,您可以设置可用性监视.您提供了一个URL,NewRelic每30秒会对其执行一次ping操作,从而使您的应用保持清醒状态.此功能的预期目的是在您的网站因业务和性能原因而宕机时向您发出警报,但它还有防止应用程序闲置的附加好处.

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.

希望有帮助!

这篇关于node-cron无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 04:55