Skip to content

Commit

Permalink
Merge pull request #3 from flipp-oss/exit-on-signal
Browse files Browse the repository at this point in the history
#1: Rather than raising a `SignalException`, sigurd will now call `ex…
  • Loading branch information
Daniel Orner authored Jul 20, 2022
2 parents 230c6a0 + fbf36d7 commit 2ae43a7
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 111 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@
.rvmrc

.idea

Gemfile.lock
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## UNRELEASED

## [0.1.0] - 2022-07-20
- Added `exit_on_signal` to exit instead of raising a `SignalException`. Fixes #1.

## [0.0.3] - 2020-11-25
- Allow to access the executor from outside the signal handler.
- Use `require "sigurd"` without having to require files individually.
Expand Down
103 changes: 0 additions & 103 deletions Gemfile.lock

This file was deleted.

7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ backoff to wait before restarting it. You can instead use the `sleep_seconds`
setting to always sleep a fixed amount of time before retrying. There
is no limit to retries.

## Configuration

By default, sigurd will exit the process when a TERM, KILL or QUIT signal is received. You can change this
behavior to instead raise the original `SignalException` by setting

Sigurd.exit_on_signal = true

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/flipp-oss/sigurd .
Expand Down
5 changes: 5 additions & 0 deletions lib/sigurd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@

# :nodoc:
module Sigurd

class << self
attr_accessor :exit_on_signal
end

end
12 changes: 8 additions & 4 deletions lib/sigurd/signal_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ def run!
@runner.start

loop do
case signal_queue.pop
signal = signal_queue.pop
case signal
when *SIGNALS
@runner.stop
break
if Sigurd.exit_on_signal
exit 0
else
raise(SignalException, signal)
end
else
ready = IO.select([reader, writer])

Expand All @@ -41,9 +46,8 @@ def run!
# https://stackoverflow.com/questions/29568298/run-code-when-signal-is-sent-but-do-not-trap-the-signal-in-ruby
def prepend_handler(signal)
previous = Signal.trap(signal) do
previous = -> { raise SignalException, signal } unless previous.respond_to?(:call)
yield
previous.call
previous.call if previous&.respond_to?(:call)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/sigurd/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Sigurd
VERSION = '0.0.3'
VERSION = '0.1.0'
end
20 changes: 17 additions & 3 deletions spec/signal_handler_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'sigurd/signal_handler'
require 'sigurd'

RSpec.describe Sigurd::SignalHandler do
describe '#run!' do
Expand All @@ -11,8 +11,22 @@
expect(runner).to receive(:stop)

signal_handler = described_class.new(runner)
signal_handler.send(:unblock, described_class::SIGNALS.first)
signal_handler.run!
Thread.new { sleep 1; Process.kill('TERM', 0) }
expect { signal_handler.run! }.to raise_error(SignalException)
end

context 'when exit_on_signal is true' do
it 'should raise a SignalException' do
Sigurd.exit_on_signal = true
runner = TestRunners::TestRunner.new
expect(runner).to receive(:start)
expect(runner).to receive(:stop)

signal_handler = described_class.new(runner)
Thread.new { sleep 1; Process.kill('TERM', 0) }
expect { signal_handler.run! }.to raise_error(SystemExit)
end
end

end
end
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ def stop
mocks.yield_receiver_to_any_instance_implementation_blocks = true
mocks.verify_partial_doubles = true
end

config.before(:each) do
Sigurd.exit_on_signal = false
end
end

0 comments on commit 2ae43a7

Please sign in to comment.