2018-08-02 00:09:29 +02:00
# Copyright (C) 2018 Colin Darie <colin@darie.eu>, 2018 Evolix <info@evolix.fr>
# License: GNU AGPL-3+ (see full text in LICENSE file)
2018-07-02 14:25:35 +02:00
module CheckProcessor
2018-08-30 17:50:38 +02:00
attr_reader :configuration , :logger
2018-06-05 15:24:44 +02:00
2018-08-29 12:14:34 +02:00
def initialize ( configuration : , logger : NullLogger . new )
2018-08-30 17:50:38 +02:00
# Levels: debug info warn error fatal
2018-08-29 12:14:34 +02:00
@logger = logger
@configuration = configuration
2018-06-05 15:24:44 +02:00
end
2018-08-30 17:50:38 +02:00
def sync_dates # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
sync_started_at = Time . now
logger . info " #{ self . class . name } : sync_dates has started "
2018-07-02 14:25:35 +02:00
resolvers . each do | resolver |
2018-08-30 17:50:38 +02:00
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
2018-06-05 15:24:44 +02:00
2018-08-30 17:50:38 +02:00
group_finished_at = Time . now
2018-09-03 14:05:07 +02:00
check_ids = checks . map ( & :id )
check_logs = check_errors_scope ( check_ids : check_ids ,
after_date : group_started_at ,
before_date : group_finished_at ) . includes ( :check ) . all
2018-06-05 15:24:44 +02:00
2018-09-03 14:05:07 +02:00
message = " #{ self . class . name } : #{ check_logs . count } error(s) found for checks ' #{ check_ids . join ( ',' ) } ' between ' #{ group_started_at } ' and ' #{ group_finished_at } ' " # rubocop:disable Metrics/LineLength
logger . debug ( message )
check_logs . each do | check_log |
2018-08-30 17:50:38 +02:00
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
2018-06-05 15:24:44 +02:00
end
end
2018-08-30 17:50:38 +02:00
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
2018-06-05 15:24:44 +02:00
end
2018-07-02 14:25:35 +02:00
# :nocov:
def resolvers
fail NotImplementedError , " #{ self . class . name } did not implemented method #{ __callee__ } "
end
# :nocov:
2018-06-05 15:24:44 +02:00
def resolve_last_run_failed
scope . last_run_failed
end
def resolve_expire_long_term
scope
. where ( " DATE(domain_expires_at) >= DATE_ADD(CURDATE(), INTERVAL ? DAY) " ,
2018-08-01 12:30:48 +02:00
configuration . long_term_interval )
2018-06-05 15:24:44 +02:00
. where ( " DATEDIFF(domain_expires_at, CURDATE()) MOD ? = 0 " ,
configuration . long_term_frequency )
end
def resolve_expire_short_term
scope . where ( " DATE(domain_expires_at) < DATE_ADD(CURDATE(), INTERVAL ? DAY) " ,
2018-08-01 12:30:48 +02:00
configuration . long_term_interval )
2018-06-05 15:24:44 +02:00
end
def resolve_unknown_expiry
scope . where ( " domain_expires_at IS NULL " )
end
2018-07-02 17:21:08 +02:00
def resolve_all
scope
end
2018-07-02 14:25:35 +02:00
protected
2018-06-05 15:24:44 +02:00
2018-07-02 14:25:35 +02:00
def base_scope
2018-06-05 15:24:44 +02:00
Check
. active
2018-08-29 17:46:04 +02:00
. auto
2018-06-05 15:24:44 +02:00
. where ( " last_run_at IS NULL OR last_run_at < DATE_SUB(NOW(), INTERVAL 12 HOUR) " )
end
2018-07-02 14:25:35 +02:00
# :nocov:
def scope
fail NotImplementedError , " #{ self . class . name } did not implemented method #{ __callee__ } "
end
2018-07-02 17:21:08 +02:00
def process ( _check )
2018-07-02 14:25:35 +02:00
fail NotImplementedError , " #{ self . class . name } did not implemented method #{ __callee__ } "
end
# :nocov:
2018-06-05 15:24:44 +02:00
end