class Mamiya::Agent::Tasks::Abstract

Attributes

agent[R]
error[R]
logger[R]
task[R]
task_queue[R]

Public Class Methods

identifier() click to toggle source
# File lib/mamiya/agent/tasks/abstract.rb, line 16
def self.identifier
  self.name.split(/::/).last.gsub(/(.)([A-Z])/, '\1_\2').downcase
end
new(task_queue, task, agent: nil, logger: Mamiya::Logger.new, raise_error: false) click to toggle source
# File lib/mamiya/agent/tasks/abstract.rb, line 7
def initialize(task_queue, task, agent: nil, logger: Mamiya::Logger.new, raise_error: false)
  @agent = agent
  @task_queue = task_queue
  @task = task.merge('task' => self.class.identifier)
  @error = nil
  @raise_error = raise_error
  @logger = logger["#{self.class.identifier}:#{self.task_id}"]
end

Public Instance Methods

after() click to toggle source
# File lib/mamiya/agent/tasks/abstract.rb, line 51
def after
end
before() click to toggle source
# File lib/mamiya/agent/tasks/abstract.rb, line 45
def before
end
errored() click to toggle source
# File lib/mamiya/agent/tasks/abstract.rb, line 54
def errored
end
execute() click to toggle source
# File lib/mamiya/agent/tasks/abstract.rb, line 30
def execute
  @logger.info "Task started: #{task.inspect}"
  before
  run
rescue Exception => error
  @error = error
  raise if raise_error?
  errored
  @logger.error "Encountered error: #{error.inspect}\n\t#{error.backtrace.join("\n\t")}"
ensure
  enqueue_chained unless error
  after
  @logger.info "Task finished"
end
raise_error?() click to toggle source
# File lib/mamiya/agent/tasks/abstract.rb, line 22
def raise_error?
  !!@raise_error
end
run() click to toggle source
# File lib/mamiya/agent/tasks/abstract.rb, line 48
def run
end
task_id() click to toggle source
# File lib/mamiya/agent/tasks/abstract.rb, line 26
def task_id
  task['id'] || "0x#{self.__id__.to_s(16)}"
end

Private Instance Methods

config() click to toggle source
# File lib/mamiya/agent/tasks/abstract.rb, line 71
def config
  @config ||= agent ? agent.config : nil
end
enqueue_chained() click to toggle source
# File lib/mamiya/agent/tasks/abstract.rb, line 59
def enqueue_chained
  return if !task['_chain'] || task['_chain'].empty?

  next_task = task.dup
  next_task.delete('task')

  next_task_name, *next_task['_chain'] = task['_chain']
  next_task.delete('_chain') if next_task['_chain'].empty?

  task_queue.enqueue(next_task_name.to_sym, next_task)
end