class Tickwork::Event

Attributes

data_store_key[RW]
job[RW]

Public Class Methods

new(manager, period, job, block, options={}) click to toggle source
# 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

Public Instance Methods

convert_timezone(t) click to toggle source
# File lib/tickwork/event.rb, line 31
def convert_timezone(t)
  @timezone ? t.in_time_zone(@timezone) : t
end
elapsed_ready(t) click to toggle source
# File lib/tickwork/event.rb, line 40
def elapsed_ready(t)
  last.nil? || (t - last.to_i).to_i >= @period
end
last() click to toggle source
# File lib/tickwork/event.rb, line 23
def last
  @manager.data_store.read(data_store_key)
end
last=(value) click to toggle source
# File lib/tickwork/event.rb, line 27
def last=(value)
  @manager.data_store.write(data_store_key, value)
end
run(t) click to toggle source
# 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) click to toggle source
# 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?() click to toggle source
# File lib/tickwork/event.rb, line 44
def thread?
  @thread
end
to_s() click to toggle source
# File lib/tickwork/event.rb, line 65
def to_s
  job
end

Private Instance Methods

execute() click to toggle source
# File lib/tickwork/event.rb, line 70
def execute
  @block.call(@job, last)
rescue => e
  @manager.log_error e
  @manager.handle_error e
end
validate_if_option(if_option) click to toggle source
# File lib/tickwork/event.rb, line 77
def validate_if_option(if_option)
  if if_option && !if_option.respond_to?(:call)
    raise ArgumentError.new(':if expects a callable object, but #{if_option} does not respond to call')
  end
end