简体   繁体   English

使用Clockwork / Delayed_Job / ActiveJob安排每5分钟(圆形)运行

[英]Scheduling with Clockwork/Delayed_Job/ActiveJob to run every 5 minutes (round)

I am reading some sensor values and I want to schedule recurring jobs in order to repeat every :00, :10, :15, :20, :25, :30, :35 etc. 我正在阅读一些传感器值,我想安排重复工作,以便重复每一个:00,:10,:15,:20,:25,:30,:35等。

I am using clockwork/delayed_job/activeJob & foreman in Rails 5 and I am able to retrieve and store values to the database. 我在Rails 5中使用clockwork / delayed_job / activeJob和foreman,我能够检索并将值存储到数据库中。

What I am unable to do is to complete each task/job in its time, meaning that they appear to be queued in a series and then being executed at once. 我无法做的是在其时间内完成每个任务/作业,这意味着它们似乎在一系列中排队,然后立即执行。 I can see that they might run 5 times at once, while the whole task may take some ms. 我可以看到他们可能一次运行5次,而整个任务可能需要几毫秒。

clock.rb clock.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 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

What am I missing ? 我错过了什么?

You helped me! 你帮了我! Below the description: 下面的描述:

every(5.minutes, 'SensorJob') { Delayed::Job.enqueue SensorJob.new, queue: :default }

So I help you! 所以我帮助你! You should use: 你应该使用:

Delayed::Job.enqueue SensorJob.new.perform

instead of: 代替:

Delayed::Job.enqueue SensorJob.new

You have to use SensorJob 's method perform ! 你必须使用SensorJob的方法perform

Good Luck!! 祝好运!!

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

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