Module: SayWhen::Storage::BaseJob

Included in:
ActiveRecordStrategy::Job, MemoryStrategy::Job
Defined in:
lib/say_when/storage/base_job.rb

Constant Summary collapse

STATE_WAITING =

ready to be run, just waiting for its turn

'waiting'
STATE_ACQUIRED =

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

'acquired'
STATE_EXECUTING =

related job for the trigger is executing

'executing'
STATE_COMPLETE =

“Complete” means the trigger has no remaining fire times

'complete'
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.

'error'

Instance Method Summary collapse

Instance Method Details

#executeObject



52
53
54
# File 'lib/say_when/storage/base_job.rb', line 52

def execute
  execute_job(data)
end

#execute_job(options) ⇒ Object



64
65
66
67
68
# 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) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# 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) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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_triggerObject



56
57
58
59
60
61
62
# 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

#lockObject



23
24
25
# File 'lib/say_when/storage/base_job.rb', line 23

def lock
  @lock ||= Mutex.new
end

#releaseObject



44
45
46
47
48
49
50
# 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

#triggerObject



27
28
29
# File 'lib/say_when/storage/base_job.rb', line 27

def trigger
  @trigger ||= load_trigger
end