让 rufus-scheduler 在 Heroku 上工作

Make rufus-scheduler work on Heroku

我想使用 Rufus-Scheduler 每天发送一封邮件。 我严格按照 GitHub here 上给出的说明(代码段,使用瘦服务器等);但是什么都没发生(没有发送邮件),我无法根据 Heroku 日志找出原因

我的代码

# config/initializers/scheduler.rb

require 'rufus-scheduler'


s = Rufus::Scheduler.singleton


# Here goes my mailing code (already tested and works well)

s.every '1d' do

Rails.logger.info "hello, it's #{Time.now}"
Rails.logger.flush
end

为了使 rufus-schedul 正常工作,还有其他一些在 GitHub 中没有提到的要点吗?非常感谢

(免责声明,我不是 Heroku 专家,但我可以 google 我可以阅读文档)。

所以,这不是您的第一个 Heroku - rufus-scheduler 问题...(Rails_using Rufus in order to schedule sending mails daily)

你说 "in order to send a mail daily",那你为什么不使用 Heroku 调度程序插件呢? https://devcenter.heroku.com/articles/scheduler它可以每天甚至每小时安排(讽刺的是,它可能会错过一个时间表,所以他们推荐下面"custom clock process")。

你有没有意识到你的测功机可能在计划时间到了时睡着了?在一段时间不活动后,Heroku 将测功机设为 "sleep"。

Heroku 建议使用 "custom clock process": https://devcenter.heroku.com/articles/scheduled-jobs-custom-clock-processes#custom-clock-processes

你必须了解你的目标平台 Heroku,并使你的系统适应它,无论有没有 rufus-scheduler。

作为旁注,您之前的 post 提到了 Passenger,很难调整它以不杀死 rufus-scheduler 的线程,但这不会在您的 dyno 所在的 Heroku 上发挥重要作用' rufus-scheduler 不应该永远存在,它的寿命不能超过其 webapp 的测功机,因此 "custom clock process" 推荐。

你可以做到这一点,但你必须了解 hobby dynos 的工作原理。每个免费帐户 is allocated hours like this.

Accounts are given a base of 550 hours each month in which your Free dynos can run. In addition to these base hours, accounts which verify with a credit card will receive an additional 450 hours of Free dyno quota.

因为 rufus-scheduler 运行s 作为您 ruby 应用程序进程中的一个线程(这就是为什么当您 运行 rails 控制台),只要你有你的服务器(我用的是 puma)运行ning,rufus scheduler 就会 运行 就好了。

缺点是,如果您 运行 服务器中有两个进程,假设您 运行 puma 有 3-4 个工人,您将有 3-4 个调度程序 运行同时使它在 triplicate/quadruplicate 中执行您预定的事件,因此请记住这一点。

所以步骤很简单 - 确保你有足够的时间 运行 你的测力计整个月都在持续 - 使用像 pingdom 这样的服务,每隔几分钟 ping 你的应用程序以保持测功机处于活动状态,这样 Heroku 就不会在 30 分钟不活动后将其降速(这样做是为了释放测功机) - 这就是您需要做的所有事情

请记住,要 运行 一个月的测功机,您将需要大约 745 小时,这是您的主要分配涵盖的(当您添加信用卡时)。如果由于某种原因你 运行 不在工作时间(假设你 运行 帐户上有两个不同的应用程序并使用我在下面描述的方法)那么这可能会发生在你身上

A second notification will be sent when you reach 100% of your account quota, at which point your application’s dynos will be put to sleep for the remainder of that month. As a result, any apps using free dynos will not be accessible for the remainder of the month.

当您可以像其他人一样使用 heroku 调度程序来安排 rake 任务时,似乎很麻烦。