class Sidekiq::Clockwork
Constants
- SIGNALS
- VERSION
Attributes
error_handlers[R]
Public Class Methods
new()
click to toggle source
# File lib/sidekiq/clockwork.rb, line 18 def initialize @error_handlers = [] @sleep_timeout = 0.1 @interrupt = false use_default_error_handler end
run(&block)
click to toggle source
# File lib/sidekiq/clockwork.rb, line 7 def self.run(&block) return unless Sidekiq.server? clockwork = new clockwork.instance_eval(&block) clockwork.run clockwork end
Public Instance Methods
error_handler(&block)
click to toggle source
# File lib/sidekiq/clockwork.rb, line 40 def error_handler(&block) error_handlers << block end
error_message(error, prefix = nil)
click to toggle source
# File lib/sidekiq/clockwork.rb, line 54 def error_message(error, prefix = nil) "[SIDEKIQ:CLOCKWORK] #{prefix}#{error.class}: #{error.message} (#{error.backtrace_locations.first})\n" end
every(interval, &block)
click to toggle source
# File lib/sidekiq/clockwork.rb, line 35 def every(interval, &block) run_at = Time.now + interval jobs << {run_at: run_at, interval: interval, block: block} end
interrupt!()
click to toggle source
# File lib/sidekiq/clockwork.rb, line 77 def interrupt! @interrupt = true end
interrupt?()
click to toggle source
# File lib/sidekiq/clockwork.rb, line 73 def interrupt? @interrupt end
jobs()
click to toggle source
# File lib/sidekiq/clockwork.rb, line 26 def jobs @jobs ||= [] end
run()
click to toggle source
# File lib/sidekiq/clockwork.rb, line 89 def run trap_signals Thread.new do loop do jobs.each do |job| run_job(job) end if interrupt? break else sleep(sleep_timeout) end end end end
run_error_handlers(error)
click to toggle source
# File lib/sidekiq/clockwork.rb, line 44 def run_error_handlers(error) error_handlers.each do |handler| begin handler.call(error) rescue StandardError => handler_error $stderr << error_message(handler_error, "Error handler raised exception. ") end end end
run_job(job)
click to toggle source
# File lib/sidekiq/clockwork.rb, line 64 def run_job(job) now = Time.now return if now < job[:run_at] job[:run_at] = now + job[:interval] job[:block].call rescue StandardError => error run_error_handlers(error) end
sleep_timeout(timeout = nil)
click to toggle source
# File lib/sidekiq/clockwork.rb, line 30 def sleep_timeout(timeout = nil) @sleep_timeout = timeout if timeout @sleep_timeout end
trap_signals()
click to toggle source
# File lib/sidekiq/clockwork.rb, line 81 def trap_signals SIGNALS.each do |signal| trap(signal) do interrupt! end end end
use_default_error_handler()
click to toggle source
# File lib/sidekiq/clockwork.rb, line 58 def use_default_error_handler error_handler do |error| $stderr << error_message(error) end end