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
17
# 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])
  @block = block
  @if = options[:if]
  @thread = options.fetch(:thread, @manager.config[:thread])
  @timezone = options.fetch(:tz, @manager.config[:tz])
  @skip_first_run = options[:skip_first_run]
  @last = @skip_first_run ? convert_timezone(Time.now) : nil
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



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

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

#run(t) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/clockwork/event.rb', line 35

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)


23
24
25
26
27
28
29
# File 'lib/clockwork/event.rb', line 23

def run_now?(t)
  t = convert_timezone(t)
  return false unless elapsed_ready?(t)
  return false unless run_at?(t)
  return false unless run_if?(t)
  true
end

#thread?Boolean

Returns:

  • (Boolean)


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

def thread?
  @thread
end

#to_sObject



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

def to_s
  job.to_s
end