21
1
Fork 0
mirror of https://github.com/Evolix/chexpire.git synced 2024-05-01 16:30:49 +02:00
chexpire/app/services/check_logger.rb
Colin Darie d98c1241c5
Log an error when a system command doesnt have an exit status.
Occurred at least 1 time with a check_http.
2018-07-06 12:58:25 +02:00

48 lines
1 KiB
Ruby

class CheckLogger
attr_reader :check_log
def initialize(check)
@check_log = CheckLog.create!(check: check, status: :pending)
end
def log(event, message)
case event
when :after_command
log_command_result(message)
when :parsed_response
log_parsed_response(message)
when :parser_error, :service_error, :standard_error
log_error(message)
end
end
private
def log_command_result(result)
check_log.exit_status = result.exit_status
check_log.raw_response = result.stdout
if result.exit_status.nil? || result.exit_status > 0 # rubocop:disable Style/NumericPredicate
check_log.error = result.stderr
check_log.status = :failed
end
check_log.save!
end
def log_parsed_response(response)
check_log.parsed_response = response.to_json
if response.valid?
check_log.succeed!
else
check_log.failed!
end
end
def log_error(exception)
check_log.error = [exception.message, exception.backtrace].join("\n")
check_log.failed!
end
end