Class: Clockwork::Event
- Inherits:
-
Object
- Object
- Clockwork::Event
- Defined in:
- lib/clockwork/event.rb
Instance Attribute Summary collapse
-
#job ⇒ Object
Returns the value of attribute job.
-
#last ⇒ Object
Returns the value of attribute last.
Instance Method Summary collapse
- #convert_timezone(t) ⇒ Object
- #exception_message(e) ⇒ Object
- #execute ⇒ Object
- #handle_error(e) ⇒ Object
-
#initialize(manager, period, job, block, options = {}) ⇒ Event
constructor
A new instance of Event.
- #log_error(e) ⇒ Object
- #run(t) ⇒ Object
- #thread? ⇒ Boolean
- #time?(t) ⇒ Boolean
- #to_s ⇒ Object
Constructor Details
#initialize(manager, period, job, block, options = {}) ⇒ Event
Returns a new instance of Event.
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/clockwork/event.rb', line 5 def initialize(manager, period, job, block, ={}) @manager = manager @period = period @job = job @at = At.parse([:at]) @last = nil @block = block @if = [:if] @thread = [:thread] @timezone = [:tz] end |
Instance Attribute Details
#job ⇒ Object
Returns the value of attribute job.
3 4 5 |
# File 'lib/clockwork/event.rb', line 3 def job @job end |
#last ⇒ Object
Returns the value of attribute last.
3 4 5 |
# File 'lib/clockwork/event.rb', line 3 def last @last end |
Instance Method Details
#convert_timezone(t) ⇒ Object
21 22 23 |
# File 'lib/clockwork/event.rb', line 21 def convert_timezone(t) @timezone ? t.in_time_zone(@timezone) : t end |
#exception_message(e) ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/clockwork/event.rb', line 61 def (e) msg = [ "Exception #{e.class} -> #{e.}" ] base = File.(Dir.pwd) + '/' e.backtrace.each do |t| msg << " #{File.(t).gsub(/#{base}/, '')}" end msg.join("\n") end |
#execute ⇒ Object
50 51 52 53 54 55 |
# File 'lib/clockwork/event.rb', line 50 def execute @block.call(@job, @last) rescue => e log_error e handle_error e end |
#handle_error(e) ⇒ Object
72 73 74 75 76 |
# File 'lib/clockwork/event.rb', line 72 def handle_error(e) if handler = @manager.get_error_handler handler.call(e) end end |
#log_error(e) ⇒ Object
57 58 59 |
# File 'lib/clockwork/event.rb', line 57 def log_error(e) @manager.config[:logger].error(e) end |
#run(t) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/clockwork/event.rb', line 35 def run(t) t = convert_timezone(t) @last = t if thread? if @manager.thread_available? Thread.new { execute } else log_error "Threads exhausted; skipping #{self}" end else execute end end |
#thread? ⇒ Boolean
31 32 33 |
# File 'lib/clockwork/event.rb', line 31 def thread? @thread end |
#time?(t) ⇒ Boolean
25 26 27 28 29 |
# File 'lib/clockwork/event.rb', line 25 def time?(t) t = convert_timezone(t) elapsed_ready = (@last.nil? or (t - @last).to_i >= @period) elapsed_ready and (@at.nil? or @at.ready?(t)) and (@if.nil? or @if.call(t)) end |
#to_s ⇒ Object
17 18 19 |
# File 'lib/clockwork/event.rb', line 17 def to_s @job end |