Clock work restart a job after a few minutes in rails

318 Views Asked by At

I am using Clock worker gem for back ground process

I have a Configuration table which contains key and value.

My clock.rb file

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

When run the worker: config/clock.rb

This will work fine but when i am changing time from Configuration, How worker will work without restart the clock worker file ??

1

There are 1 best solutions below

1
Aleksei Matiushkin On

I can think of:

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