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.


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/clockwork.rb', line 58

def initialize(period, job, block, options={})
  @period = period
  @job = job
  @at = At.parse(options[:at])
  @last = nil
  @block = block
  if options[:if]
    if options[:if].respond_to?(:call)
      @if = options[:if]
    else
      raise ArgumentError.new(':if expects a callable object, but #{options[:if]} does not respond to call')
    end
  end

  tz = options[:tz] || Clockwork.config[:tz]
  @timezone = TZInfo::Timezone.get(tz) if tz
end

Instance Attribute Details

#jobObject

Returns the value of attribute job


56
57
58
# File 'lib/clockwork.rb', line 56

def job
  @job
end

#lastObject

Returns the value of attribute last


56
57
58
# File 'lib/clockwork.rb', line 56

def last
  @last
end

Instance Method Details

#convert_timezone(t) ⇒ Object


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

def convert_timezone(t)
  @timezone ? @timezone.utc_to_local(t.dup.utc) : t
end

#exception_message(e) ⇒ Object


102
103
104
105
106
107
108
109
110
111
# File 'lib/clockwork.rb', line 102

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


98
99
100
# File 'lib/clockwork.rb', line 98

def log_error(e)
  Clockwork.config[:logger].error(e)
end

#run(t) ⇒ Object


90
91
92
93
94
95
96
# File 'lib/clockwork.rb', line 90

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

#time?(t) ⇒ Boolean

Returns:

  • (Boolean)

84
85
86
87
88
# File 'lib/clockwork.rb', line 84

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_sObject


76
77
78
# File 'lib/clockwork.rb', line 76

def to_s
  @job
end