forked from tpitale/mail_room
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When MailRoom is run in Kubernetes, we have found occasions where MailRoom appears to have attempted to stop running, but `Net::IMAP` is stuck waiting for threads (ruby/net-imap#14). This commit adds an HTTP liveness checker to enable detection of a terminated MailRoom pod.
- Loading branch information
Showing
12 changed files
with
184 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,9 @@ You will also need to install `faraday` or `letter_opener` if you use the `postb | |
|
||
```yaml | ||
--- | ||
:health_check: | ||
:address: "127.0.0.1" | ||
:port: 8080 | ||
:mailboxes: | ||
- | ||
:email: "[email protected]" | ||
|
@@ -133,6 +136,7 @@ You will also need to install `faraday` or `letter_opener` if you use the `postb | |
**Note:** If using `delete_after_delivery`, you also probably want to use | ||
`expunge_deleted` unless you really know what you're doing. | ||
|
||
<<<<<<< HEAD | ||
## inbox_method | ||
|
||
By default, IMAP mode is assumed for reading a mailbox. | ||
|
@@ -218,6 +222,16 @@ for Microsoft Cloud for US Government: | |
:graph_endpoint: https://graph.microsoft.us | ||
``` | ||
|
||
## health_check ## | ||
|
||
Requires `webrick` gem to be installed. | ||
|
||
This option enables an HTTP server that listens to a bind address | ||
defined by `address` and `port`. The following endpoints are supported: | ||
|
||
* `/liveness`: This returns a 200 status code with `OK` as the body if | ||
the server is running. Otherwise, it returns a 500 status code. | ||
|
||
## delivery_method ## | ||
|
||
### postback ### | ||
|
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
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
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,60 @@ | ||
# frozen_string_literal: true | ||
|
||
module MailRoom | ||
class HealthCheck | ||
attr_reader :address, :port, :running | ||
|
||
def initialize(attributes = {}) | ||
@address = attributes[:address] | ||
@port = attributes[:port] | ||
|
||
validate! | ||
end | ||
|
||
def run | ||
@server = create_server | ||
|
||
@thread = Thread.new do | ||
@server.start | ||
end | ||
|
||
@thread.abort_on_exception = true | ||
@running = true | ||
end | ||
|
||
def quit | ||
@running = false | ||
@server&.shutdown | ||
@thread&.join(60) | ||
end | ||
|
||
private | ||
|
||
def validate! | ||
raise 'No health check address specified' unless address | ||
raise "Health check port #{@port.to_i} is invalid" unless port.to_i.positive? | ||
end | ||
|
||
def create_server | ||
require 'webrick' | ||
|
||
server = ::WEBrick::HTTPServer.new(Port: port, BindAddress: address, AccessLog: []) | ||
|
||
server.mount_proc '/liveness' do |_req, res| | ||
handle_liveness(res) | ||
end | ||
|
||
server | ||
end | ||
|
||
def handle_liveness(res) | ||
if @running | ||
res.status = 200 | ||
res.body = "OK\n" | ||
else | ||
res.status = 500 | ||
res.body = "Not running\n" | ||
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
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,4 +1,7 @@ | ||
--- | ||
:health_check: | ||
:address: "127.0.0.1" | ||
:port: 8080 | ||
:mailboxes: | ||
- | ||
:email: "[email protected]" | ||
|
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
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,57 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe MailRoom::HealthCheck do | ||
let(:address) { '127.0.0.1' } | ||
let(:port) { 8000 } | ||
let(:params) { { address: address, port: port } } | ||
subject { described_class.new(params) } | ||
|
||
describe '#initialize' do | ||
context 'with valid parameters' do | ||
it 'validates successfully' do | ||
expect(subject).to be_a(described_class) | ||
end | ||
end | ||
|
||
context 'with invalid address' do | ||
let(:address) { nil } | ||
|
||
it 'raises an error' do | ||
expect { subject }.to raise_error('No health check address specified') | ||
end | ||
end | ||
|
||
context 'with invalid port' do | ||
let(:port) { nil } | ||
|
||
it 'raises an error' do | ||
expect { subject }.to raise_error('Health check port 0 is invalid') | ||
end | ||
end | ||
end | ||
|
||
describe '#run' do | ||
it 'sets running to true' do | ||
server = stub(start: true) | ||
subject.stubs(:create_server).returns(server) | ||
|
||
subject.run | ||
|
||
expect(subject.running).to be true | ||
end | ||
end | ||
|
||
describe '#quit' do | ||
it 'sets running to false' do | ||
server = stub(start: true, shutdown: true) | ||
subject.stubs(:create_server).returns(server) | ||
|
||
subject.run | ||
subject.quit | ||
|
||
expect(subject.running).to be false | ||
end | ||
end | ||
end |