Class: Sidekiq::Clockwork
- Inherits:
-
Object
- Object
- Sidekiq::Clockwork
- Defined in:
- lib/sidekiq/clockwork.rb,
lib/sidekiq/clockwork/version.rb
Constant Summary collapse
- SIGNALS =
%w[INT TERM HUP].freeze
- VERSION =
"0.1.1"
Instance Attribute Summary collapse
-
#error_handlers ⇒ Object
readonly
Returns the value of attribute error_handlers.
Class Method Summary collapse
Instance Method Summary collapse
- #error_handler(&block) ⇒ Object
- #error_message(error, prefix = nil) ⇒ Object
- #every(interval, &block) ⇒ Object
-
#initialize ⇒ Clockwork
constructor
A new instance of Clockwork.
- #interrupt! ⇒ Object
- #interrupt? ⇒ Boolean
- #jobs ⇒ Object
- #run ⇒ Object
- #run_error_handlers(error) ⇒ Object
- #run_job(job) ⇒ Object
- #sleep_timeout(timeout = nil) ⇒ Object
- #trap_signals ⇒ Object
- #use_default_error_handler ⇒ Object
Constructor Details
#initialize ⇒ Clockwork
Returns a new instance of Clockwork.
18 19 20 21 22 23 24 |
# File 'lib/sidekiq/clockwork.rb', line 18 def initialize @error_handlers = [] @sleep_timeout = 0.1 @interrupt = false use_default_error_handler end |
Instance Attribute Details
#error_handlers ⇒ Object (readonly)
Returns the value of attribute error_handlers.
16 17 18 |
# File 'lib/sidekiq/clockwork.rb', line 16 def error_handlers @error_handlers end |
Class Method Details
.run(&block) ⇒ Object
7 8 9 10 11 12 13 14 |
# 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 |
Instance Method Details
#error_handler(&block) ⇒ Object
40 41 42 |
# File 'lib/sidekiq/clockwork.rb', line 40 def error_handler(&block) error_handlers << block end |
#error_message(error, prefix = nil) ⇒ Object
54 55 56 |
# File 'lib/sidekiq/clockwork.rb', line 54 def (error, prefix = nil) "[SIDEKIQ:CLOCKWORK] #{prefix}#{error.class}: #{error.} (#{error.backtrace_locations.first})\n" end |
#every(interval, &block) ⇒ Object
35 36 37 38 |
# 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! ⇒ Object
77 78 79 |
# File 'lib/sidekiq/clockwork.rb', line 77 def interrupt! @interrupt = true end |
#interrupt? ⇒ Boolean
73 74 75 |
# File 'lib/sidekiq/clockwork.rb', line 73 def interrupt? @interrupt end |
#jobs ⇒ Object
26 27 28 |
# File 'lib/sidekiq/clockwork.rb', line 26 def jobs @jobs ||= [] end |
#run ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# 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) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# 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 << (handler_error, "Error handler raised exception. ") end end end |
#run_job(job) ⇒ Object
64 65 66 67 68 69 70 71 |
# 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) ⇒ Object
30 31 32 33 |
# File 'lib/sidekiq/clockwork.rb', line 30 def sleep_timeout(timeout = nil) @sleep_timeout = timeout if timeout @sleep_timeout end |
#trap_signals ⇒ Object
81 82 83 84 85 86 87 |
# 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 ⇒ Object
58 59 60 61 62 |
# File 'lib/sidekiq/clockwork.rb', line 58 def use_default_error_handler error_handler do |error| $stderr << (error) end end |