2018-06-04 14:06:37 +02:00
|
|
|
module Notifier
|
2018-08-01 12:30:48 +02:00
|
|
|
Configuration = Struct.new(:interval, :consecutive_failures)
|
2018-06-04 14:06:37 +02:00
|
|
|
|
|
|
|
class Processor
|
|
|
|
attr_reader :configuration
|
|
|
|
attr_reader :channels
|
|
|
|
attr_reader :resolver
|
|
|
|
|
|
|
|
def initialize(configuration = nil)
|
|
|
|
@configuration = configuration || default_configuration
|
|
|
|
|
|
|
|
@resolver = Resolver.new
|
|
|
|
@channels = {
|
|
|
|
email: Channels::Email.new,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_expires_soon
|
|
|
|
resolver.resolve_expires_soon.find_each do |notification|
|
|
|
|
notifier_channel_for(notification).notify(:expires_soon, notification)
|
|
|
|
|
|
|
|
sleep configuration.interval
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_recurrent_failures
|
2018-08-01 12:30:48 +02:00
|
|
|
resolver.resolve_check_failed(configuration.consecutive_failures).find_each do |notification|
|
2018-06-04 14:06:37 +02:00
|
|
|
notifier_channel_for(notification).notify(:recurrent_failures, notification)
|
|
|
|
|
|
|
|
sleep configuration.interval
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def default_configuration
|
|
|
|
config = Rails.configuration.chexpire.fetch("notifier", {})
|
|
|
|
|
|
|
|
Configuration.new(
|
|
|
|
config.fetch("interval") { 0.00 },
|
2018-08-01 12:30:48 +02:00
|
|
|
config.fetch("consecutive_failures") { 3 },
|
2018-06-04 14:06:37 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def notifier_channel_for(notification)
|
|
|
|
channels.fetch(notification.channel.to_sym)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|