简体   繁体   English

在Heroku服务器上的node.js中使用'cron'模块

[英]Using 'cron' module in node.js on heroku server

I am using cron in node.js to schedule a function that sends text messages at user-determined times. 我在node.js中使用cron计划了一个在用户确定的时间发送文本消息的函数。 It works on my local server, but when i deploy to heroku the functions never get called. 它可以在我的本地服务器上运行,但是当我部署到heroku时,这些函数永远不会被调用。

I'm using a cron job on heroku with node. 我在带有节点的heroku上使用cron作业。 Here is my top level stuff 这是我的顶级物品

var CronJob = require('cron').CronJob;

var dailyJob = new CronJob({
      cronTime: '0 0 0 * * *',
      onTick: function () {
        // Do daily function
        console.log('I get called 1 time a day.');
      },
      start: false
    });

dailyJob.start();

On Heroku you may need to use the Heroku Scheduler. 在Heroku上,您可能需要使用Heroku Scheduler。

See: https://devcenter.heroku.com/articles/scheduler 参见: https : //devcenter.heroku.com/articles/scheduler

I'll elaborate a little bit more on what @rsp said above, so that if anyone else finds this question they'll understand why using Heroku Scheduler is the correct answer here. 我将详细说明@rsp的内容,以便在其他人发现此问题的情况下,他们将理解为什么在这里使用Heroku Scheduler是正确的答案。

When you're running software on Heroku, what happens is that Heroku will take your project, and run it on a random dyno (server) in the Heroku collection of servers on Amazon. 当您在Heroku上运行软件时,发生的事情是Heroku将接管您的项目,并在Amazon Heroku服务器集合中的随机dyno(服务器)上运行它。

For a number of reasons (including to help distribute application load evenly across a large number of Amazon servers), Heroku will periodically move your dyno from one Amazon server to another. 由于多种原因(包括帮助在许多Amazon服务器上平均分配应用程序负载),Heroku会定期将您的dyno从一台Amazon服务器移动到另一台Amazon服务器。 This happens many times per day, automatically, behind the scenes. 每天自动在幕后进行多次操作。

This means that your application code will be periodically restarting all the time when running on Heroku. 这意味着在Heroku上运行时,您的应用程序代码将一直定期重新启动。

Now -- this isn't a bad thing from an end-user perspective, because while your application code is restarting, Heroku will queue up incoming requests, then just send them to your application once it's been successfully restarted on a new host. 现在-从最终用户的角度来看这并不是一件坏事,因为在重新启动应用程序代码时,Heroku将排队入站请求,然后在成功在新主机上成功重启后将它们发送到您的应用程序。 So to the end user, this behavior is 100% transparent. 因此,对于最终用户而言,此行为是100%透明的。

What's important to know here, though, is that since your application code may randomly restart, you shouldn't use it to do things like run long tasks that take a while to finish, or queue up jobs to be executed in the future (what the cron module does). 不过,重要的是,由于您的应用程序代码可能会随机重新启动,因此您不应使用它来执行诸如运行需要一段时间才能完成的长任务或将将来要执行的作业排入队列的事情( cron模块可以)。

Instead: Heroku created a free scheduler addon that you can use to basically say "Hey Heroku, run this Node script every minute|hour|etc." 而是:Heroku创建了一个免费的调度程序插件,您可以使用该插件基本说“嘿,Heroku,每分钟|小时|等运行此Node脚本”。

The scheduler addon Heroku provides will reliably execute your cron task because Heroku keeps track of that timing stuff separately (outside of your application logic). Heroku提供的调度程序插件将可靠地执行您的cron任务,因为Heroku单独跟踪该计时内容(在您的应用程序逻辑之外)。

Hopefully this helps! 希望这会有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM