module SayWhen::Storage::BaseJob

Constants

STATE_ACQUIRED

has been acquired b/c it is time to be triggered

STATE_COMPLETE

“Complete” means the trigger has no remaining fire times

STATE_ERROR

A Trigger arrives at the error state when the scheduler attempts to fire it, but cannot due to an error creating and executing its related job.

STATE_EXECUTING

related job for the trigger is executing

STATE_WAITING

ready to be run, just waiting for its turn

Public Instance Methods

execute() click to toggle source
# File lib/say_when/storage/base_job.rb, line 52
def execute
  execute_job(data)
end
execute_job(options) click to toggle source
# File lib/say_when/storage/base_job.rb, line 64
def execute_job(options)
  task_method = (job_method || 'execute').to_s
  task = get_task(task_method)
  task.send(task_method, options)
end
fired(fired_at = Time.now) click to toggle source
# File lib/say_when/storage/base_job.rb, line 31
def fired(fired_at = Time.now)
  lock.synchronize do
    self.last_fire_at = fired_at
    self.next_fire_at = trigger.next_fire_at(last_fire_at + 1.second) rescue nil

    if next_fire_at.nil?
      self.status = STATE_COMPLETE
    else
      self.status = STATE_WAITING
    end
  end
end
get_task(task_method) click to toggle source
# File lib/say_when/storage/base_job.rb, line 70
def get_task(task_method)
  task = nil

  if job_class
    tc = job_class.constantize
    if tc.respond_to?(task_method)
      task = tc
    else
      to = tc.new
      if to.respond_to?(task_method)
        task = to
      else
        raise "Neither '#{job_class}' class nor instance respond to '#{task_method}'"
      end
    end
  elsif scheduled
    if scheduled.respond_to?(task_method)
      task = scheduled
    else
      raise "Scheduled '#{scheduled.inspect}' does not respond to '#{task_method}'"
    end
  end
  task
end
load_trigger() click to toggle source
# File lib/say_when/storage/base_job.rb, line 56
def load_trigger
  strategy = trigger_strategy || :once
  require "say_when/triggers/#{strategy}_strategy"
  trigger_class_name = "SayWhen::Triggers::#{strategy.to_s.camelize}Strategy"
  trigger_class = trigger_class_name.constantize
  trigger_class.new((trigger_options || {}).merge(:job=>self))
end
lock() click to toggle source
# File lib/say_when/storage/base_job.rb, line 23
def lock
  @lock ||= Mutex.new
end
release() click to toggle source
# File lib/say_when/storage/base_job.rb, line 44
def release
  lock.synchronize do
    if self.status == STATE_ACQUIRED
      self.status = STATE_WAITING
    end
  end
end
trigger() click to toggle source
# File lib/say_when/storage/base_job.rb, line 27
def trigger
  @trigger ||= load_trigger
end