Class: Rearview::ResultsHandler

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/rearview/results_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

#logger

Constructor Details

#initialize(job, monitor_results) ⇒ ResultsHandler

Returns a new instance of ResultsHandler.



6
7
8
9
10
# File 'lib/rearview/results_handler.rb', line 6

def initialize(job,monitor_results)
  @job = job
  @status = Job::Status::ERROR
  @monitor_results = monitor_results || {}
end

Instance Attribute Details

#jobObject (readonly)

Returns the value of attribute job.



4
5
6
# File 'lib/rearview/results_handler.rb', line 4

def job
  @job
end

#monitor_resultsObject (readonly)

Returns the value of attribute monitor_results.



4
5
6
# File 'lib/rearview/results_handler.rb', line 4

def monitor_results
  @monitor_results
end

#statusObject (readonly)

Returns the value of attribute status.



4
5
6
# File 'lib/rearview/results_handler.rb', line 4

def status
  @status
end

Instance Method Details

#runObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rearview/results_handler.rb', line 11

def run
  logger.info "#{self} run"
  normalized_results = Rearview::MonitorRunner.normalize_results(@monitor_results)
  @status = normalized_results[:status] || @status

  job_data = JobData.find_or_create_by(job_id: @job.id)
  job_data.data = normalized_results
  job_data.save!

  @job.last_run = Time.now.utc
  event = if Job::Status.values.include?(@status.to_s)
            @status.to_sym
          else
            :error
          end
  logger.info "#{self} firing event :#{event} for #{@job.inspect}"
  @job.fire_event(event,{:monitor_results=>@monitor_results,:job_error=>{:message=>normalized_results[:output]}})

  self
rescue
  logger.error "#{self} process results failed: #{$!}\n#{$@.join("\n")}"
  self
end