【问题标题】:Scheduling with Clockwork/Delayed_Job/ActiveJob to run every 5 minutes (round)使用 Clockwork/Delayed_Job/ActiveJob 安排每 5 分钟运行一次(轮次)
【发布时间】:2016-11-05 20:02:25
【问题描述】:

我正在读取一些传感器值,我想安排重复作业,以便每隔 :00、:10、:15、:20、:25、:30、:35 等重复一次。

我在 Rails 5 中使用 clockwork/delayed_job/activeJob 和工头,我能够检索值并将其存储到数据库中。

我不能做的是按时完成每个任务/作业,这意味着它们似乎按一系列排队,然后立即执行。我可以看到它们可能一次运行 5 次,而整个任务可能需要一些毫秒。

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

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

我错过了什么?

【问题讨论】:

    标签: ruby-on-rails delayed-job rails-activejob clockwork


    【解决方案1】:

    你帮了我!在说明下方:

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

    所以我帮你!你应该使用:

    Delayed::Job.enqueue SensorJob.new.perform
    

    代替:

    Delayed::Job.enqueue SensorJob.new
    

    你必须使用SensorJob的方法perform

    祝你好运!!

    【讨论】:

      猜你喜欢
      • 2017-01-18
      • 2020-07-18
      • 2014-03-29
      • 2017-12-16
      • 1970-01-01
      • 2012-02-05
      • 1970-01-01
      • 2013-11-27
      • 1970-01-01
      相关资源
      最近更新 更多