Class: AethernalAgent::Operation

Inherits:
Object
  • Object
show all
Defined in:
lib/aethernal_agent/operation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plugin_name, method, options) ⇒ Operation

Returns a new instance of Operation.



4
5
6
7
8
9
# File 'lib/aethernal_agent/operation.rb', line 4

def initialize(plugin_name, method, options)
  self.plugin_name = plugin_name
  self.method = method
  self.options = options
  self.uuid = SecureRandom.uuid
end

Instance Attribute Details

#backtraceObject

Returns the value of attribute backtrace.



2
3
4
# File 'lib/aethernal_agent/operation.rb', line 2

def backtrace
  @backtrace
end

#completed_atObject

Returns the value of attribute completed_at.



2
3
4
# File 'lib/aethernal_agent/operation.rb', line 2

def completed_at
  @completed_at
end

#errorsObject

Returns the value of attribute errors.



2
3
4
# File 'lib/aethernal_agent/operation.rb', line 2

def errors
  @errors
end

#exceptionObject

Returns the value of attribute exception.



2
3
4
# File 'lib/aethernal_agent/operation.rb', line 2

def exception
  @exception
end

#methodObject

Returns the value of attribute method.



2
3
4
# File 'lib/aethernal_agent/operation.rb', line 2

def method
  @method
end

#optionsObject

Returns the value of attribute options.



2
3
4
# File 'lib/aethernal_agent/operation.rb', line 2

def options
  @options
end

#plugin_nameObject

Returns the value of attribute plugin_name.



2
3
4
# File 'lib/aethernal_agent/operation.rb', line 2

def plugin_name
  @plugin_name
end

#resultObject

Returns the value of attribute result.



2
3
4
# File 'lib/aethernal_agent/operation.rb', line 2

def result
  @result
end

#started_atObject

Returns the value of attribute started_at.



2
3
4
# File 'lib/aethernal_agent/operation.rb', line 2

def started_at
  @started_at
end

#uuidObject

Returns the value of attribute uuid.



2
3
4
# File 'lib/aethernal_agent/operation.rb', line 2

def uuid
  @uuid
end

Instance Method Details

#attributesObject



44
45
46
# File 'lib/aethernal_agent/operation.rb', line 44

def attributes
  {method: self.method, plugin_name: self.plugin_name, options: self.options, uuid: self.uuid, result: self.result, success: self.success?, failed: self.failed?, is_completed: self.is_completed?, errors: self.errors, exception: self.exception, backtrace: self.backtrace, completed_at: self.completed_at, started_at: self.started_at}
end

#failed?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/aethernal_agent/operation.rb', line 32

def failed?
  !self.exception.nil? || !self.errors.blank?
end

#is_completed?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/aethernal_agent/operation.rb', line 36

def is_completed?
  !self.completed_at.nil?
end

#is_started?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/aethernal_agent/operation.rb', line 40

def is_started?
  !self.started_at.nil?
end

#runObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/aethernal_agent/operation.rb', line 11

def run
  self.started_at = Time.now

  begin
    plugin = "AethernalAgent::#{self.plugin_name.camelcase}".constantize.new(self.options)
    self.result = plugin.send(self.method, self.options)
    if self.result.is_a?(Hash) && self.result.has_key?(:errors)
      self.errors = self.result[:errors]
    end
  rescue StandardError => e
    self.exception = e.message
    self.backtrace = e.backtrace
  end

  self.completed_at = Time.now
end

#success?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/aethernal_agent/operation.rb', line 28

def success?
  !self.failed?
end

#to_json(options = {}) ⇒ Object



48
49
50
# File 'lib/aethernal_agent/operation.rb', line 48

def to_json(options ={})
  self.attributes.to_json
end