在系统启动时启动后台 Rails 进程

Start a background Rails process at system boot

我想要一个始终 运行 的后台进程,最好在服务器启动时启动。此进程将负责定期轮询网络设备并将结果保存在 Active Record 中以供 Rails 应用程序访问,因此据我了解,它需要成为 rails 应用程序的一部分(以便它可以访问 Active Record)

在 rails 中,我如何指定服务器启动时应启动 运行ning 的内容?如果我在服务器启动时使用 https://github.com/thuehlinger/daemons 并使用 ruby myserver_control.rb start 调用我的进程,该进程是否可以访问 Active Record?

您可以使用 clockwork gem 定期 运行 作业。

示例 lib/clock.rb 您在其中安排作业[=​​14=]
require 'rake'
require 'clockwork'
require File.expand_path('../../config/boot',        __FILE__)
require File.expand_path('../../config/environment', __FILE__)

module Clockwork
  configure do |config|
    config[:sleep_timeout] = 60
  end

  every(10.minutes, 'inbox:process_all') do
    `rake inbox:process_all`
  end
  every(1.day, 'email:send_daily_summary', at: '00:00') do
    `rake email:send_daily_summary`
  end
end