mirror of
https://github.com/Evolix/chexpire.git
synced 2024-06-23 01:30:59 +02:00
CheckLogger for command & parser
This commit is contained in:
parent
ec4dc321f6
commit
123bec60af
47
app/services/check_logger.rb
Normal file
47
app/services/check_logger.rb
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
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
|
||||||
|
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 > 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
|
|
@ -26,6 +26,9 @@ module Whois
|
||||||
def call
|
def call
|
||||||
result = run_command
|
result = run_command
|
||||||
parse(result)
|
parse(result)
|
||||||
|
rescue StandardError => ex
|
||||||
|
logger.log :service_error, ex
|
||||||
|
raise
|
||||||
end
|
end
|
||||||
|
|
||||||
def run_command
|
def run_command
|
||||||
|
|
|
@ -5,7 +5,6 @@ module Whois
|
||||||
class UnsupportedDomainError < WhoisError; end
|
class UnsupportedDomainError < WhoisError; end
|
||||||
class ParserError < WhoisError; end
|
class ParserError < WhoisError; end
|
||||||
|
|
||||||
class CommentNotFoundError < ParserError; end
|
|
||||||
class FieldNotFoundError < ParserError; end
|
class FieldNotFoundError < ParserError; end
|
||||||
class MissingDateFormatError < ParserError; end
|
class MissingDateFormatError < ParserError; end
|
||||||
class InvalidDateError < ParserError; end
|
class InvalidDateError < ParserError; end
|
||||||
|
|
|
@ -26,7 +26,11 @@ module Whois
|
||||||
|
|
||||||
do_parse
|
do_parse
|
||||||
|
|
||||||
|
logger.log :parsed_response, response
|
||||||
|
|
||||||
response
|
response
|
||||||
|
rescue StandardError => ex
|
||||||
|
logger.log :parser_error, ex
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
|
@ -7,5 +7,9 @@ module Whois
|
||||||
def initialize(domain)
|
def initialize(domain)
|
||||||
@domain = domain
|
@domain = domain
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def valid?
|
||||||
|
created_at.present?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
81
test/services/check_logger_test.rb
Normal file
81
test/services/check_logger_test.rb
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
require "test_helper"
|
||||||
|
require "check_logger"
|
||||||
|
require "system_command"
|
||||||
|
|
||||||
|
class CheckLoggerTest < ActiveSupport::TestCase
|
||||||
|
setup do
|
||||||
|
@check = checks(:domain_example_org)
|
||||||
|
@logger = CheckLogger.new(@check)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "should create a pending CheckLog" do
|
||||||
|
assert_difference -> { CheckLog.where(check: @check).count }, +1 do
|
||||||
|
CheckLogger.new(@check)
|
||||||
|
end
|
||||||
|
|
||||||
|
assert last_log.pending?
|
||||||
|
end
|
||||||
|
|
||||||
|
test "should log a success raw result command" do
|
||||||
|
result = SystemCommandResult.new("command", 0, "the result", "")
|
||||||
|
|
||||||
|
assert_no_difference -> { CheckLog.where(check: @check).count } do
|
||||||
|
@logger.log :after_command, result
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_equal "the result", @logger.check_log.raw_response
|
||||||
|
assert_nil @logger.check_log.error
|
||||||
|
assert_equal 0, @logger.check_log.exit_status
|
||||||
|
assert @logger.check_log.pending?
|
||||||
|
end
|
||||||
|
|
||||||
|
test "should log a raw result command with an error" do
|
||||||
|
result = SystemCommandResult.new("command", 1, "optional stdout", "an error occured")
|
||||||
|
@logger.log :after_command, result
|
||||||
|
|
||||||
|
assert_equal "optional stdout", @logger.check_log.raw_response
|
||||||
|
assert_equal "an error occured", @logger.check_log.error
|
||||||
|
assert_equal 1, @logger.check_log.exit_status
|
||||||
|
assert @logger.check_log.failed?
|
||||||
|
end
|
||||||
|
|
||||||
|
test "should log a successful parsed command" do
|
||||||
|
response = OpenStruct.new(
|
||||||
|
domain: "example.fr",
|
||||||
|
extracted: "some data",
|
||||||
|
valid?: true,
|
||||||
|
)
|
||||||
|
@logger.log :parsed_response, response
|
||||||
|
|
||||||
|
assert_equal response.to_json, @logger.check_log.parsed_response
|
||||||
|
assert_nil @logger.check_log.error
|
||||||
|
assert @logger.check_log.succeed?
|
||||||
|
end
|
||||||
|
|
||||||
|
test "should log parser error with a backtrace" do
|
||||||
|
@logger.log :parser_error, mock_exception
|
||||||
|
|
||||||
|
assert_includes @logger.check_log.error, "my error occured"
|
||||||
|
assert_includes @logger.check_log.error, "minitest.rb"
|
||||||
|
assert @logger.check_log.failed?
|
||||||
|
end
|
||||||
|
|
||||||
|
test "should log service error" do
|
||||||
|
@logger.log :service_error, mock_exception
|
||||||
|
|
||||||
|
assert_not_nil @logger.check_log.error
|
||||||
|
assert @logger.check_log.failed?
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def last_log
|
||||||
|
CheckLog.where(check: @check).last
|
||||||
|
end
|
||||||
|
|
||||||
|
def mock_exception
|
||||||
|
exception = ArgumentError.new("my error occured")
|
||||||
|
exception.set_backtrace(caller)
|
||||||
|
exception
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue