时钟工作在几分钟后重新启动作业

时间:2017-06-28 12:15:25

标签: ruby-on-rails ruby ruby-on-rails-5 clockwork

我正在使用Clock worker gem进行背景处理

我有一个包含键和值的配置表。

我的clock.rb文件

require 'clockwork'
require './config/boot'
require './config/environment'
module Clockwork
  handler do |job|  
    puts "Running #{job}" 
  end

  time = Configuration.find_by(:key=>"time") 
  if time.present?
    min = time.value.to_i     
    every(min.minutes, 'calculate user details') { 
           User.calculate_time}    
  end
end

运行worker:config / clock.rb

这样可以正常工作,但是当我从Configuration更改时间时, 如何在不重启时钟工作器文件的情况下工作?

1 个答案:

答案 0 :(得分:0)

我能想到:

module Clockwork
  GET_TIME = -> { Configuration.find_by(key: "time") }
  SETTINGS = { time: GET_TIME.() }
  every(1.minutes, 'reset settings') do 
    Clockwork::SETTINGS[:time] = Clockwork::GET_TIME.()
  end

  handler do |job|  
    puts "Running #{job}" 
  end

  if SETTINGS[:time].present?
    min = SETTINGS[:time].value.to_i     
    every(min.minutes, 'calculate user details') do
      User.calculate_time
    end
  end
end