Skip to content

Commit

Permalink
Merge pull request #6546 from pulibrary/cache_smtp
Browse files Browse the repository at this point in the history
SMTP check should happen every 5 minutes.
  • Loading branch information
tpendragon authored Nov 15, 2024
2 parents 3aebf33 + 87181d0 commit c29517e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/monitors/smtp_status.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
# frozen_string_literal: true
class SmtpStatus < HealthMonitor::Providers::Base
class << self
attr_writer :next_check_timestamp

def next_check_timestamp
@next_check_timestamp || 0
end
end
def check!
return unless Time.current > self.class.next_check_timestamp
settings = ActionMailer::Base.smtp_settings
tls_setting = settings[:enable_starttls] == false ? false : :auto
smtp = Net::SMTP.new(settings[:address], settings[:port], starttls: tls_setting)
smtp.open_timeout = 1
smtp.start
self.class.next_check_timestamp = Time.current + 5.minutes
end
end
14 changes: 14 additions & 0 deletions spec/requests/health_monitor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,25 @@
end

it "errors when it can't contact the SMTP server" do
SmtpStatus.next_check_timestamp = 0
get "/health.json?providers[]=smtpstatus"

expect(response).not_to be_successful
end

it "caches a success on SMTP and doesn't call it twice in a short window" do
smtp_double = instance_double(Net::SMTP)
allow(Net::SMTP).to receive(:new).and_return(smtp_double)
allow(smtp_double).to receive(:open_timeout=)
allow(smtp_double).to receive(:start)

get "/health.json?providers[]=smtpstatus"
expect(response).to be_successful
get "/health.json?providers[]=smtpstatus"

expect(Net::SMTP).to have_received(:new).exactly(1).times
end

it "errors when solr is down" do
allow(Blacklight.default_index.connection).to receive(:uri).and_return(URI("http://example.com/bla"))
stub_request(:get, "http://example.com/solr/admin/cores?action=STATUS").to_return(body: { responseHeader: { status: 500 } }.to_json, headers: { "Content-Type" => "text/json" })
Expand Down

0 comments on commit c29517e

Please sign in to comment.