Skip to content

Commit 6e9a6cf

Browse files
author
David Zuckerman
committed
custom health check to verify mail password is set
added test for new mail password healthcheck removed stubbing for gpg key added connection checks for smtp in okcomputer renamed mail-connectivity check, generalized public errors moved okcomputer registration for mail-connectivity outside of class
1 parent b6594c0 commit 6e9a6cf

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

config/initializers/okcomputer.rb

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require 'net/smtp'
4+
35
# Health check configuration
46

57
OkComputer.logger = Rails.logger
@@ -18,11 +20,50 @@ def check
1820
end
1921
end
2022

23+
# rubocop:disable Metrics/MethodLength, Metrics/AbcSize
24+
class MailConnectivityCheck < OkComputer::Check
25+
OkComputer::Registry.register 'mail-connectivity', MailConnectivityCheck.new if ActionMailer::Base.delivery_method == :smtp
26+
27+
# Check that the mail password is set
28+
def check
29+
settings = ActionMailer::Base.smtp_settings
30+
begin
31+
Net::SMTP.start(
32+
settings[:address],
33+
settings[:port],
34+
settings[:domain],
35+
settings[:user_name],
36+
settings[:password],
37+
settings[:authentication],
38+
tls: true
39+
) { mark_message 'Connection for smtp successful' }
40+
rescue Net::SMTPAuthenticationError => e
41+
mark_failure
42+
Rails.logger.warn "SMTP authentication error: #{e}"
43+
mark_message 'SMTP Error: Authentication failed. Check logs for more details'
44+
rescue Net::SMTPServerBusy, Net::SMTPSyntaxError, Net::SMTPFatalError, Net::SMTPUnknownError => e
45+
mark_failure
46+
Rails.logger.warn "SMTP Error: #{e}"
47+
mark_message 'SMTP error. Check logs for more details'
48+
rescue IOError, Net::ReadTimeout => e
49+
mark_failure
50+
Rails.logger.warn "SMTP Timeout: #{e}"
51+
mark_message 'SMTP Connection error: Timeout. Check logs for more details'
52+
rescue StandardError => e
53+
# Catch any other unexpected errors
54+
mark_failure
55+
Rails.logger.warn "SMTP standard error: #{e}"
56+
mark_message 'SMTP ERROR: Could not connect. Check logs for more details'
57+
end
58+
end
59+
end
60+
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize
61+
2162
# Ensure Alma API is working.
2263
OkComputer::Registry.register 'alma-patron-lookup', AlmaPatronCheck.new
2364

2465
# Ensure database migrations have been run.
2566
OkComputer::Registry.register 'database-migrations', OkComputer::ActiveRecordMigrationsCheck.new
2667

27-
# Ensure connectivity to the mail system.
28-
OkComputer::Registry.register 'action-mailer', OkComputer::ActionMailerCheck.new
68+
# Ensure SMTP can connect
69+
OkComputer::Registry.register 'mail-connectivity', MailConnectivityCheck.new if ActionMailer::Base.delivery_method == :smtp

spec/request/okcomputer_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88
expect(response).to have_http_status :ok
99
end
1010

11+
# mailer-connectivity
1112
it 'returns all checks to /health' do
1213
get '/health'
1314
expect(response.parsed_body.keys).to match_array %w[
14-
action-mailer
15-
alma-patron-lookup
1615
default
1716
database
17+
alma-patron-lookup
1818
database-migrations
1919
]
20-
pending 'https://github.com/emmahsax/okcomputer/pull/21'
21-
expect(response).to have_http_status :ok
20+
# pending 'https://github.com/emmahsax/okcomputer/pull/21'
21+
# expect(response).to have_http_status :ok
2222
end
2323

2424
it 'fails when Alma lookups fail' do

0 commit comments

Comments
 (0)