class Seam::Worker

Attributes

operation_args[RW]

Public Class Methods

all() click to toggle source
# File lib/seam/worker.rb, line 29
def self.all
  (@handlers || []).map { |x| x.new }
end
handler_for(step) click to toggle source
# File lib/seam/worker.rb, line 21
def self.handler_for step
  @handlers.each do |handler|
    instance = handler.new
    return instance if instance.step == step
  end
  nil
end
inherited(handler) click to toggle source
# File lib/seam/worker.rb, line 16
def self.inherited handler
  @handlers ||= []
  @handlers << handler
end

Public Instance Methods

current_step() click to toggle source
# File lib/seam/worker.rb, line 104
def current_step
  effort.flow["steps"][effort.completed_steps.count]
end
effort() click to toggle source
# File lib/seam/worker.rb, line 91
def effort
  @current_effort
end
eject() click to toggle source
# File lib/seam/worker.rb, line 37
def eject
  @operation_to_execute = :eject
end
execute(effort) click to toggle source
# File lib/seam/worker.rb, line 9
def execute effort
  set_current_effort effort
  before_process
  process
  after_process
end
execute_all() click to toggle source
# File lib/seam/worker.rb, line 33
def execute_all
  efforts_to_execute.each { |e| execute e }
end
handles(step) click to toggle source
# File lib/seam/worker.rb, line 5
def handles step
  @step = step
end
history() click to toggle source
# File lib/seam/worker.rb, line 95
def history
  @current_run
end
move_to_next_step(options = {}) click to toggle source
# File lib/seam/worker.rb, line 41
def move_to_next_step(options = {})
  @operation_to_execute = :move_to_next_step
  operation_args[:next_execute_at] = (options[:on] || Time.now)
end
operations() click to toggle source
# File lib/seam/worker.rb, line 61
def operations
  {
    try_again_in:      -> do
                            seconds = operation_args[:seconds]
                            try_again_on = Time.now + seconds

                            history[:try_again_on] = try_again_on

                            effort.next_execute_at = try_again_on
                          end,
    try_again_on:      -> do
                            history[:try_again_on] = operation_args[:time]
                            effort.next_execute_at = operation_args[:time]
                          end,
    move_to_next_step: -> do
                            effort.next_execute_at = operation_args[:next_execute_at] if operation_args[:next_execute_at]

                            effort.completed_steps << effort.next_step

                            steps = effort.flow['steps'].map { |x| x['name'] }
                            next_step = steps[effort.completed_steps.count]

                            effort.next_step = next_step
                            mark_effort_as_complete if next_step.nil?

                          end,
    eject:             -> { mark_effort_as_complete }
  }
end
step() click to toggle source
# File lib/seam/worker.rb, line 99
def step
  s = @step || self.class.name.underscore.gsub('_worker', '').split('/')[-1]
  s.to_s
end
try_again_in(seconds) click to toggle source
# File lib/seam/worker.rb, line 46
def try_again_in seconds
  @operation_to_execute    = :try_again_in
  operation_args[:seconds] = seconds
end
try_again_on(time) click to toggle source
# File lib/seam/worker.rb, line 51
def try_again_on time
  @operation_to_execute = :try_again_on
  operation_args[:time] = time
end

Private Instance Methods

after_process() click to toggle source
# File lib/seam/worker.rb, line 130
def after_process
  execute_the_appropriate_operation
  stamp_the_new_history_record
  save_the_effort
end
before_process() click to toggle source
# File lib/seam/worker.rb, line 120
def before_process
  run = { 
          started_at:  Time.now,
          step:        step, 
          step_id:     current_step['id'],
          data_before: stamping_the_history? ? effort.data.clone : nil
        }
  @current_run = HashWithIndifferentAccess.new run
end
efforts_to_execute() click to toggle source
# File lib/seam/worker.rb, line 136
def efforts_to_execute
  Seam::Effort.find_all_by_step step
end
execute_the_appropriate_operation() click to toggle source
# File lib/seam/worker.rb, line 140
def execute_the_appropriate_operation
  @operation_to_execute ||= :move_to_next_step
  operations[@operation_to_execute].call
end
mark_effort_as_complete() click to toggle source
# File lib/seam/worker.rb, line 110
def mark_effort_as_complete
  effort.next_step    = nil
  effort.complete     = true
  effort.completed_at = Time.now
end
save_the_effort() click to toggle source
# File lib/seam/worker.rb, line 160
def save_the_effort
  effort.save
end
set_current_effort(effort) click to toggle source
# File lib/seam/worker.rb, line 116
def set_current_effort effort
  @current_effort = effort
end
stamp_the_new_history_record() click to toggle source
# File lib/seam/worker.rb, line 145
def stamp_the_new_history_record
  history[:result]     = @operation_to_execute
  history[:stopped_at] = Time.now

  if stamping_the_history?
    history[:data_after] = effort.data.clone
  end

  effort.history << history
end
stamping_the_history?() click to toggle source
# File lib/seam/worker.rb, line 156
def stamping_the_history?
  effort.flow['stamp_data_history']
end