-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
1,129 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,46 @@ | ||
module Jobs | ||
module SslReminder | ||
class ScanCertificates < Jobs::Scheduled | ||
every 1.day | ||
|
||
DEFAULT_BATCH_SIZE = 100 | ||
DAILY_THRESHOLD_DAYS = 7 | ||
WEEKLY_THRESHOLD_DAYS = 31 | ||
WEEKLY_DAY_TO_SEND = 1 | ||
|
||
def execute(_args) | ||
::SslReminder::Domain.find_in_batches(batch_size: DEFAULT_BATCH_SIZE) do |domains| | ||
domains.each { |domain| process_domain(domain) } | ||
end | ||
end | ||
|
||
private | ||
|
||
def process_domain(domain) | ||
update_certificate_info(domain) | ||
send_report(domain) if valid_to_send?(domain) | ||
end | ||
|
||
def update_certificate_info(domain) | ||
scanner = ::SslReminder::Certificates::Scanner.new(domain.url) | ||
new_expiration_date = scanner.scan | ||
return if new_expiration_date.blank? | ||
|
||
domain.expiration_date = new_expiration_date | ||
domain.save! if domain.changed? | ||
end | ||
|
||
def send_report(domain) | ||
report = ::SslReminderReportMailer.report(domain) | ||
Email::Sender.new(report, :ssl_reminder).send | ||
end | ||
|
||
def valid_to_send?(domain) | ||
return false unless domain.notification_enabled? && domain.days_remaining.present? | ||
|
||
domain.days_remaining <= DAILY_THRESHOLD_DAYS || | ||
domain.days_remaining <= WEEKLY_THRESHOLD_DAYS && Date.current.wday == WEEKLY_DAY_TO_SEND | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,28 @@ | ||
require_dependency "email/message_builder" | ||
|
||
class SslReminderReportMailer < ActionMailer::Base | ||
include Email::BuildEmailHelper | ||
|
||
def report(domain) | ||
email_opts = { | ||
template: "ssl_reminder", | ||
html_override: html(domain) | ||
} | ||
|
||
build_email(domain.user.email, email_opts) | ||
end | ||
|
||
private | ||
|
||
def html(domain) | ||
ActionView::Base.new("plugins/ssl-reminder/app/views").render( | ||
template: "email/ssl_reminder", | ||
format: :html, | ||
locals: { | ||
domain_name: domain.name, | ||
domain_url: domain.url, | ||
days_remaining: domain.days_remaining | ||
} | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 1,13 @@ | ||
module SslReminder | ||
class Domain < ActiveRecord::Base | ||
belongs_to :user | ||
|
||
def days_remaining | ||
if expiration_date.blank? | ||
nil | ||
else | ||
[(expiration_date - Date.current).to_i, 0].max | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.