Class: Mamiya::Agent::Tasks::Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/mamiya/agent/tasks/abstract.rb

Direct Known Subclasses

Clean, Notifyable, Ping

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task_queue, task, agent: nil, logger: Mamiya::Logger.new, raise_error: false) ⇒ Abstract

Returns a new instance of Abstract.



7
8
9
10
11
12
13
14
# 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

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



20
21
22
# File 'lib/mamiya/agent/tasks/abstract.rb', line 20

def agent
  @agent
end

#errorObject (readonly)

Returns the value of attribute error.



20
21
22
# File 'lib/mamiya/agent/tasks/abstract.rb', line 20

def error
  @error
end

#loggerObject (readonly)

Returns the value of attribute logger.



20
21
22
# File 'lib/mamiya/agent/tasks/abstract.rb', line 20

def logger
  @logger
end

#taskObject (readonly)

Returns the value of attribute task.



20
21
22
# File 'lib/mamiya/agent/tasks/abstract.rb', line 20

def task
  @task
end

#task_queueObject (readonly)

Returns the value of attribute task_queue.



20
21
22
# File 'lib/mamiya/agent/tasks/abstract.rb', line 20

def task_queue
  @task_queue
end

Class Method Details

.identifierObject



16
17
18
# File 'lib/mamiya/agent/tasks/abstract.rb', line 16

def self.identifier
  self.name.split(/::/).last.gsub(/(.)([A-Z])/, '\1_\2').downcase
end

Instance Method Details

#afterObject



51
52
# File 'lib/mamiya/agent/tasks/abstract.rb', line 51

def after
end

#beforeObject



45
46
# File 'lib/mamiya/agent/tasks/abstract.rb', line 45

def before
end

#erroredObject



54
55
# File 'lib/mamiya/agent/tasks/abstract.rb', line 54

def errored
end

#executeObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/mamiya/agent/tasks/abstract.rb', line 22

def raise_error?
  !!@raise_error
end

#runObject



48
49
# File 'lib/mamiya/agent/tasks/abstract.rb', line 48

def run
end

#task_idObject



26
27
28
# File 'lib/mamiya/agent/tasks/abstract.rb', line 26

def task_id
  task['id'] || "0x#{self.__id__.to_s(16)}"
end