mirror of
https://github.com/Evolix/chexpire.git
synced 2024-05-04 01:35:10 +02:00
Add a logger to CheckProcessor to log errors for each batch of checks
This commit is contained in:
parent
5326dcc7aa
commit
1235845e54
|
@ -2,23 +2,54 @@
|
||||||
# License: GNU AGPL-3+ (see full text in LICENSE file)
|
# License: GNU AGPL-3+ (see full text in LICENSE file)
|
||||||
|
|
||||||
module CheckProcessor
|
module CheckProcessor
|
||||||
attr_reader :configuration
|
attr_reader :configuration, :logger
|
||||||
|
|
||||||
def initialize(configuration:, logger: NullLogger.new)
|
def initialize(configuration:, logger: NullLogger.new)
|
||||||
|
# Levels: debug info warn error fatal
|
||||||
@logger = logger
|
@logger = logger
|
||||||
@configuration = configuration
|
@configuration = configuration
|
||||||
end
|
end
|
||||||
|
|
||||||
def sync_dates
|
def sync_dates # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
|
||||||
@sync_started_at = Time.now
|
sync_started_at = Time.now
|
||||||
resolvers.each do |resolver|
|
logger.info "#{self.class.name}: sync_dates has started"
|
||||||
public_send(resolver).find_each(batch_size: 100).each do |check|
|
|
||||||
process(check)
|
|
||||||
|
|
||||||
sleep configuration.interval
|
resolvers.each do |resolver|
|
||||||
|
logger.info "#{self.class.name}: using resolver '#{resolver}'"
|
||||||
|
|
||||||
|
public_send(resolver).find_in_batches(batch_size: 100) do |checks|
|
||||||
|
group_started_at = Time.now
|
||||||
|
|
||||||
|
checks.each do |check|
|
||||||
|
logger.info "#{self.class.name}: processing check ##{check.id}"
|
||||||
|
process(check)
|
||||||
|
|
||||||
|
logger.debug "#{self.class.name}: sleeping #{configuration.interval} seconds"
|
||||||
|
sleep configuration.interval
|
||||||
|
end
|
||||||
|
|
||||||
|
group_finished_at = Time.now
|
||||||
|
|
||||||
|
check_errors_scope(check_ids: checks.map(&:id),
|
||||||
|
after_date: group_started_at,
|
||||||
|
before_date: group_finished_at).includes(:check).each do |check_log|
|
||||||
|
message = "#{self.class.name}: check ##{check_log.check_id} for '#{check_log.check.domain}' failed (#{check_log.exit_status}) ; #{check_log.error.lines.first}" # rubocop:disable Metrics/LineLength
|
||||||
|
logger.error(message)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@sync_finished_at = Time.now
|
|
||||||
|
sync_finished_at = Time.now
|
||||||
|
duration = (sync_finished_at - sync_started_at).to_i
|
||||||
|
logger.info "#{self.class.name}: sync_dates has finished (#{duration}s)"
|
||||||
|
end
|
||||||
|
|
||||||
|
def check_errors_scope(check_ids:, before_date: nil, after_date: nil)
|
||||||
|
scope = CheckLog.failed.where("exit_status > 0").where(id: check_ids)
|
||||||
|
scope = scope.where("created_at <= ?", before_date) if before_date
|
||||||
|
scope = scope.where("created_at >= ?", after_date) if after_date
|
||||||
|
|
||||||
|
scope
|
||||||
end
|
end
|
||||||
|
|
||||||
# :nocov:
|
# :nocov:
|
||||||
|
|
|
@ -1,27 +1,48 @@
|
||||||
# Copyright (C) 2018 Colin Darie <colin@darie.eu>, 2018 Evolix <info@evolix.fr>
|
# Copyright (C) 2018 Colin Darie <colin@darie.eu>, 2018 Evolix <info@evolix.fr>
|
||||||
# License: GNU AGPL-3+ (see full text in LICENSE file)
|
# License: GNU AGPL-3+ (see full text in LICENSE file)
|
||||||
|
|
||||||
|
require "null_logger"
|
||||||
|
|
||||||
namespace :checks do
|
namespace :checks do
|
||||||
namespace :sync_dates do
|
namespace :sync_dates do
|
||||||
task all: [:domain, :ssl]
|
task all: [:domain, :ssl]
|
||||||
|
|
||||||
|
def stdout_logger(env={})
|
||||||
|
verbose_mode = env.fetch("VERBOSE") { 0 }.to_i == 1
|
||||||
|
quiet_mode = env.fetch("QUIET") { 0 }.to_i == 1
|
||||||
|
silent_mode = env.fetch("SILENT") { 0 }.to_i == 1
|
||||||
|
|
||||||
|
if silent_mode
|
||||||
|
NullLogger.new
|
||||||
|
else
|
||||||
|
logger = Logging.logger(STDOUT)
|
||||||
|
logger.level = if quiet_mode
|
||||||
|
:error
|
||||||
|
elsif verbose_mode
|
||||||
|
:debug
|
||||||
|
else
|
||||||
|
:info
|
||||||
|
end
|
||||||
|
|
||||||
|
logger
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
desc "Refresh domains expiry dates"
|
desc "Refresh domains expiry dates"
|
||||||
task domain: :environment do
|
task domain: :environment do
|
||||||
logger = Logging.logger(STDOUT)
|
process = CheckDomainProcessor.new(
|
||||||
logger.level = :warn
|
logger: stdout_logger(ENV),
|
||||||
configuration = Rails.configuration.chexpire.fetch("checks_domain")
|
configuration: Rails.configuration.chexpire.fetch("checks_domain")
|
||||||
|
)
|
||||||
process = CheckDomainProcessor.new(logger: logger, configuration: configuration)
|
|
||||||
process.sync_dates
|
process.sync_dates
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "Refresh SSL expiry dates"
|
desc "Refresh SSL expiry dates"
|
||||||
task ssl: :environment do
|
task ssl: :environment do
|
||||||
logger = Logging.logger(STDOUT)
|
process = CheckSSLProcessor.new(
|
||||||
logger.level = :warn
|
logger: stdout_logger(ENV),
|
||||||
configuration = Rails.configuration.chexpire.fetch("checks_ssl")
|
configuration: Rails.configuration.chexpire.fetch("checks_ssl")
|
||||||
|
)
|
||||||
process = CheckSSLProcessor.new(logger: logger, configuration: configuration)
|
|
||||||
process.sync_dates
|
process.sync_dates
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -108,19 +108,15 @@ class CheckProcessorTest < ActiveSupport::TestCase
|
||||||
test "#sync_dates respects the interval configuration between sends" do
|
test "#sync_dates respects the interval configuration between sends" do
|
||||||
create_list(:check, 3, :expires_next_week)
|
create_list(:check, 3, :expires_next_week)
|
||||||
|
|
||||||
configuration = Minitest::Mock.new
|
configuration = OpenStruct.new(long_term_interval: 300,
|
||||||
2.times do configuration.expect(:long_term_interval, 300) end
|
long_term_frequency: 4,
|
||||||
configuration.expect(:long_term_frequency, 4)
|
interval: 0.000001)
|
||||||
|
|
||||||
3.times do
|
|
||||||
configuration.expect(:interval, 0.000001)
|
|
||||||
end
|
|
||||||
|
|
||||||
processor = CheckDummyProcessor.new(configuration: configuration)
|
processor = CheckDummyProcessor.new(configuration: configuration)
|
||||||
|
|
||||||
mock = Minitest::Mock.new
|
mock = Minitest::Mock.new
|
||||||
assert_stub = lambda { |actual_time|
|
assert_stub = lambda { |actual_time|
|
||||||
assert_equal 0.000001, actual_time
|
assert_equal configuration.interval, actual_time
|
||||||
mock
|
mock
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,7 +126,6 @@ class CheckProcessorTest < ActiveSupport::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
configuration.verify
|
|
||||||
mock.verify
|
mock.verify
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue