0

我正在使用 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

这可以正常工作,但是当我从配置更改时间时,如果不重新启动时钟工作文件,工作人员将如何工作?

4

1 回答 1

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
于 2017-06-28T12:54:56.203 回答