当 rails 服务器作为守护程序运行时,Rufus Scheduler 不是 运行

Rufus Scheduler not running when rails server runs as daemon

我有一个 rails 应用程序正在使用 Rufus Scheduler。当我打开 rails 服务器时:

rails s --port=4000

Rufus 调度器 运行 它的任务。如果我 运行 rails 服务器有:

rails s --port=4000 --daemon

Rufus 不再执行其任务。我添加了几条日志消息。这是时间表代码:

class AtTaskScheduler

  def self.start
    scheduler = Rufus::Scheduler.new

    p "Starting Attask scheduler"

    scheduler.every('5m') do
      # test sending hip chat message
      issue = Issue.new
      issue.post_to_hipchat("Starting sync with AtTask","SYNC")
      p "Launching Sync"
      Issue.synchronize
    end

  end

end

Hipchat 永远不会从调度程序获取消息,日志也永远不会获取语句 "Launching Sync"。

关于可能导致此问题的原因有什么想法吗?

rufus-scheduler docs 中有关于此问题的文档:

There is the handy rails server -d that starts a development Rails as a daemon. The annoying thing is that the scheduler as seen above is started in the main process that then gets forked and daemonized. The rufus-scheduler thread (and any other thread) gets lost, no scheduling happens.

I avoid running -d in development mode and bother about daemonizing only for production deployment.

These are two well crafted articles on process daemonization, please read them:

If anyway, you need something like rails server -d, why not try bundle exec unicorn -D instead? In my (limited) experience, it worked out of the box (well, had to add gem 'unicorn' to Gemfile first).