我正在读取一些传感器值,我想安排重复作业以便每隔 :00、:10、:15、:20、:25、:30、:35 等重复一次。
我在 Rails 5 中使用 clockwork/delayed_job/activeJob & foreman,我能够检索值并将值存储到数据库中。
我无法做到的是按时完成每项任务/工作,这意味着它们似乎排成一列,然后立即执行。我可以看到它们可能一次运行 5 次,而整个任务可能需要几毫秒。
时钟.rb
require 'clockwork'
require File.expand_path('../boot', __FILE__)
require File.expand_path('../environment', __FILE__)
module Clockwork
handler do |job, time|
puts "Running #{job}, at #{time}"
end
every(5.minutes, 'SensorJob') { Delayed::Job.enqueue SensorJob.new, queue: :default }
end
sensor_job.rb
class SensorJob < ActiveJob::Base
queue_as :default
def perform
@temperature = Random.new.rand(20..30)
@humidity = Random.new.rand(30..70)
SensorReading.create(temperature: @temperature, humidity: @humidity)
end
end
我错过了什么?
最佳答案
你帮了我!在描述下方:
every(5.minutes, 'SensorJob') { Delayed::Job.enqueue SensorJob.new, queue: :default }
所以我帮你!你应该使用:
Delayed::Job.enqueue SensorJob.new.perform
代替:
Delayed::Job.enqueue SensorJob.new
你必须使用SensorJob
的方法perform
!
祝你好运!
关于ruby-on-rails - 使用 Clockwork/Delayed_Job/ActiveJob 安排每 5 分钟运行一次(轮),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38178772/