Class: Tickwork::Event

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

Defined Under Namespace

Classes: IllegalJobName

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Event.

Raises:



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/tickwork/event.rb', line 7

def initialize(manager, period, job, block, options={})
  validate_if_option(options[:if])
  @manager = manager
  @period = period
  raise IllegalJobName unless job.is_a?(String) && !job.empty? && Tickwork::Manager::MANAGER_KEY != job
  @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])
  namespace = options[:namespace] 
  namespace ||= '_tickwork_'
  @data_store_key = namespace + @job
end

Instance Attribute Details

#data_store_keyObject

Returns the value of attribute data_store_key.



5
6
7
# File 'lib/tickwork/event.rb', line 5

def data_store_key
  @data_store_key
end

#jobObject

Returns the value of attribute job.



5
6
7
# File 'lib/tickwork/event.rb', line 5

def job
  @job
end

Instance Method Details

#convert_timezone(t) ⇒ Object



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

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

#elapsed_ready(t) ⇒ Object



40
41
42
# File 'lib/tickwork/event.rb', line 40

def elapsed_ready(t)
  last.nil? || (t - last.to_i).to_i >= @period
end

#lastObject



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

def last
  @manager.data_store.read(data_store_key)
end

#last=(value) ⇒ Object



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

def last=(value)
  @manager.data_store.write(data_store_key, value)
end

#run(t) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/tickwork/event.rb', line 48

def run(t)
  @manager.log "Triggering '#{self}'"
  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)


35
36
37
38
# File 'lib/tickwork/event.rb', line 35

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)


44
45
46
# File 'lib/tickwork/event.rb', line 44

def thread?
  @thread
end

#to_sObject



65
66
67
# File 'lib/tickwork/event.rb', line 65

def to_s
  job
end