Class: Clockwork::Event

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options={})
  @manager = manager
  @period = period
  @job = job
  @at = At.parse(options[:at])
  @last = nil
  @block = block
  @if = options[:if]
  @thread = options[:thread]
  @timezone = options[:tz]
end

Instance Attribute Details

#jobObject

Returns the value of attribute job.



3
4
5
# File 'lib/clockwork/event.rb', line 3

def job
  @job
end

#lastObject

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 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

#executeObject



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

Returns:

  • (Boolean)


31
32
33
# File 'lib/clockwork/event.rb', line 31

def thread?
  @thread
end

#time?(t) ⇒ Boolean

Returns:

  • (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_sObject



17
18
19
# File 'lib/clockwork/event.rb', line 17

def to_s
  @job
end