Class: Clockwork::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/clockwork.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(period, job, block, options = {}) ⇒ Event

Returns a new instance of Event.



55
56
57
58
59
60
61
# File 'lib/clockwork.rb', line 55

def initialize(period, job, block, options={})
  @period = period
  @job = job
  @at = At.parse(options[:at])
  @last = nil
  @block = block
end

Instance Attribute Details

#jobObject

Returns the value of attribute job.



53
54
55
# File 'lib/clockwork.rb', line 53

def job
  @job
end

#lastObject

Returns the value of attribute last.



53
54
55
# File 'lib/clockwork.rb', line 53

def last
  @last
end

Instance Method Details

#exception_message(e) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/clockwork.rb', line 83

def exception_message(e)
  msg = [ "Exception #{e.class} -> #{e.message}" ]

  base = File.expand_path(Dir.pwd) + '/'
  e.backtrace.each do |t|
    msg << "   #{File.expand_path(t).gsub(/#{base}/, '')}"
  end

  msg.join("\n")
end

#log_error(e) ⇒ Object



79
80
81
# File 'lib/clockwork.rb', line 79

def log_error(e)
  STDERR.puts exception_message(e)
end

#run(t) ⇒ Object



72
73
74
75
76
77
# File 'lib/clockwork.rb', line 72

def run(t)
  @last = t
  @block.call(@job)
rescue => e
  log_error(e)
end

#time?(t) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
# File 'lib/clockwork.rb', line 67

def time?(t)
  ellapsed_ready = (@last.nil? or (t - @last).to_i >= @period)
  ellapsed_ready and (@at.nil? or @at.ready?(t))
end

#to_sObject



63
64
65
# File 'lib/clockwork.rb', line 63

def to_s
  @job
end