Skip to content

Commit 34e5c31

Browse files
committed
Merge pull request mailman#35 from phildarnowsky/master
Added "graceful death" flag
2 parents 617e3d7 + cbbae52 commit 34e5c31

File tree

5 files changed

+46
-1
lines changed

5 files changed

+46
-1
lines changed

USER_GUIDE.md

+7
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,10 @@ loading will be disabled.
242242
interfere with running Mailman with cron or as a daemon.
243243

244244
**Default**: `false`
245+
246+
### Graceful death
247+
248+
`Mailman.config.graceful_death`, if set, will catch SIGINTs
249+
(Control-C) and allow the mail receiver to finish its current
250+
iteration before exiting. Note that this currently only works
251+
with POP3 receivers.

lib/mailman/application.rb

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ def self.run(&block)
1515
attr_reader :processor
1616

1717
# Creates a new router, and sets up any routes passed in the block.
18+
# @param [Hash] options the application options
19+
# @option options [true,false] :graceful_death catch interrupt signal and don't die until end of poll
1820
# @param [Proc] block a block with routes
1921
def initialize(&block)
2022
@router = Mailman::Router.new
@@ -52,6 +54,11 @@ def run
5254
end
5355

5456
connection = Receiver::POP3.new(options)
57+
58+
if Mailman.config.graceful_death
59+
Signal.trap("INT") {polling = false}
60+
end
61+
5562
loop do
5663
begin
5764
connection.connect

lib/mailman/configuration.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ class Configuration
2121
# @return [boolean] whether or not to ignore stdin. Setting this to true
2222
# stops Mailman from entering stdin processing mode.
2323
attr_accessor :ignore_stdin
24-
24+
25+
# @return [boolean] catch SIGINT and allow current iteration to finish
26+
# rather than dropping dead immediately. Currently only works with POP3
27+
# connections.
28+
attr_accessor :graceful_death
29+
2530
def logger
2631
@logger ||= Logger.new(STDOUT)
2732
end

spec/mailman/application_spec.rb

+21
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,25 @@
1818

1919
end
2020

21+
describe "#run" do
22+
describe "when graceful_death flag is set" do
23+
before do
24+
Mailman.config.graceful_death = true
25+
@app = Mailman::Application.new {}
26+
end
27+
28+
it "should catch interrupt signal and let a POP3 receiver finish its poll before exiting" do
29+
@mock_receiver = double("Receiver::POP3")
30+
@mock_receiver.stub(:connect)
31+
@mock_receiver.stub(:get_messages) {Process.kill("INT", $$)}
32+
@mock_receiver.should_receive(:disconnect)
33+
Mailman::Receiver::POP3.stub(:new) {@mock_receiver}
34+
35+
Mailman.config.pop3 = {}
36+
37+
Signal.trap("INT") {raise "Application didn't catch SIGINT"}
38+
@app.run
39+
end
40+
end
41+
end
2142
end

spec/mailman/configuration_spec.rb

+5
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,9 @@
5555
config.ignore_stdin = true
5656
config.ignore_stdin.should == true
5757
end
58+
59+
it "should store graceful_death flag" do
60+
config.graceful_death = true
61+
config.graceful_death.should == true
62+
end
5863
end

0 commit comments

Comments
 (0)