Class: Clockwork::Event

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

Direct Known Subclasses

DatabaseEvents::Event

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
16
# File 'lib/clockwork/event.rb', line 5

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



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

def convert_timezone(t)
  @timezone ? t.in_time_zone(@timezone) : t
end

#run(t) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/clockwork/event.rb', line 31

def run(t)
  @manager.log "Triggering '#{self}'"
  @last = convert_timezone(t)
  if thread?
    if @manager.thread_available?
      t = Thread.new do
        execute
      end
      t['creator'] = @manager
    else
      @manager.log_error "Threads exhausted; skipping #{self}"
    end
  else
    execute
  end
end

#run_now?(t) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
# File 'lib/clockwork/event.rb', line 22

def run_now?(t)
  t = convert_timezone(t)
  elapsed_ready(t) and (@at.nil? or @at.ready?(t)) and (@if.nil? or @if.call(t))
end

#thread?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/clockwork/event.rb', line 27

def thread?
  @thread
end

#to_sObject



48
49
50
# File 'lib/clockwork/event.rb', line 48

def to_s
  job.to_s
end