Class: Sidekiq::Clockwork

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClockwork

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_handlersObject (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_message(error, prefix = nil)
  "[SIDEKIQ:CLOCKWORK] #{prefix}#{error.class}: #{error.message} (#{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

Returns:

  • (Boolean)


73
74
75
# File 'lib/sidekiq/clockwork.rb', line 73

def interrupt?
  @interrupt
end

#jobsObject



26
27
28
# File 'lib/sidekiq/clockwork.rb', line 26

def jobs
  @jobs ||= []
end

#runObject



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 << error_message(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_signalsObject



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_handlerObject



58
59
60
61
62
# File 'lib/sidekiq/clockwork.rb', line 58

def use_default_error_handler
  error_handler do |error|
    $stderr << error_message(error)
  end
end