sigurd 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 87854913818982a8f760090652f1a5f23984d7e9b4c6636a9bd9504bc23a92eb
4
- data.tar.gz: 186bbe65f6b7b8a4bc6fd8c6241673322db2379f5e84e5b3809b0550951b901b
3
+ metadata.gz: 78b91ee76ef004d00edc84bba38adfdc5c53987574ab27f4843a555553d5753a
4
+ data.tar.gz: 786a1fe84d80e7b37f920c1b977e6a2957cd6b174bb86c34a82eb0785e7760db
5
5
  SHA512:
6
- metadata.gz: 89db617898198aeefb795e3e5ea93d287b7d23bd39841af5a8ca264c9aff2aae4b717f609039ab0a41414de5c8d1fc4a1eb231797aa25f6f5c1ee46ba091f05f
7
- data.tar.gz: 60eeb987aabb420dd01129ae6d8bd0811239283f964854763847094f7b7953b0250f47f97020c02859382b8d5d0d4369516644cb6a8a0b4f3768e2fd18593a97
6
+ metadata.gz: 03edd2b5c68e976c9b3b0de4395dc448b8b95c28939547e6d2eb6ebe1376bae177e6da7de92e8f5aed99a6b51bc81b9ba06b32a263ad3d65252b81b23544f714
7
+ data.tar.gz: 54cea634f6d9d674816f6553506cbff6f018fb10298a685315bd512f68834ac83abe1d139549e335d5dffdbb398decd9edf4ceecbb8c1ab88946114b517876be
data/.gitignore CHANGED
@@ -37,3 +37,5 @@
37
37
  .rvmrc
38
38
 
39
39
  .idea
40
+
41
+ Gemfile.lock
data/CHANGELOG.md CHANGED
@@ -7,5 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## UNRELEASED
9
9
 
10
+ ## [0.1.0] - 2022-07-20
11
+ - Added `exit_on_signal` to exit instead of raising a `SignalException`. Fixes #1.
12
+
13
+ ## [0.0.3] - 2020-11-25
14
+ - Allow to access the executor from outside the signal handler.
15
+ - Use `require "sigurd"` without having to require files individually.
16
+
10
17
  ## [0.0.1] - 2020-06-02
11
18
  - Initial release.
data/README.md CHANGED
@@ -1,2 +1,106 @@
1
1
  # sigurd
2
- Small gem to manage executing looping processes and signal handling
2
+ Small gem to manage executing looping processes and signal handling.
3
+
4
+ <p align="center">
5
+ <a href="https://badge.fury.io/rb/sigurd"><img src="https://badge.fury.io/rb/sigurd.svg" alt="Gem Version" height="18"></a>
6
+ <a href="https://codeclimate.com/github/flipp-oss/sigurd/maintainability"><img src="https://api.codeclimate.com/v1/badges/a5fc45a193abadc4e45b/maintainability" /></a>
7
+ </p>
8
+
9
+ # Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+ ```ruby
13
+ gem 'sigurd'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install sigurd
23
+
24
+ # Versioning
25
+
26
+ We use a version of semver for this gem. Any change in previous behavior
27
+ (something works differently or something old no longer works)
28
+ is denoted with a bump in the minor version (0.4 -> 0.5). Patch versions
29
+ are for bugfixes or new functionality which does not affect existing code. You
30
+ should be locking your Gemfile to the minor version:
31
+
32
+ ```ruby
33
+ gem 'sigurd', '0.0.3'
34
+ ```
35
+
36
+ # Usage
37
+
38
+ Sigurd exposes two classes for use with a third class. The ideas is as follows:
39
+
40
+ * You have any object which responds to the `start` and `stop` methods.
41
+ This object is called a "Runner". When the `stop` method is called,
42
+ the runner should gracefully shut down.
43
+ * You create an `Executor` class - this manages a thread pool for a
44
+ list of runners.
45
+ * You create a `SignalHandler` which is the topmost object. This will
46
+ handle the signals sent by the system and gracefully forward the
47
+ requests. You pass the executor into the SignalHandler.
48
+ * Finally, you call `start` on the `SignalHandler` to begin the execution.
49
+
50
+ Sample code:
51
+
52
+ ```ruby
53
+ class TestRunner
54
+
55
+ def start
56
+ loop do
57
+ break if @signal_to_stop
58
+ # do some logic here
59
+ end
60
+ end
61
+
62
+ def stop
63
+ @signal_to_stop = true
64
+ end
65
+ end
66
+
67
+ runners = (1..2).map { TestRunner.new }
68
+ executor = Sigurd::Executor.new(runners, sleep_seconds: 5, logger: Logger.new(STDOUT))
69
+ Sigurd::SignalHandler.new(executor).run!
70
+ ```
71
+
72
+ If you have only a single runner, you can pass it into the `SignalHandler`
73
+ directly, without using an `Executor`:
74
+
75
+ ```ruby
76
+ Sigurd::SignalHandler.new(runner).run!
77
+ ```
78
+
79
+ By default, if any of your runners fails, Sigurd will use an exponential
80
+ backoff to wait before restarting it. You can instead use the `sleep_seconds`
81
+ setting to always sleep a fixed amount of time before retrying. There
82
+ is no limit to retries.
83
+
84
+ ## Configuration
85
+
86
+ By default, sigurd will exit the process when a TERM, KILL or QUIT signal is received. You can change this
87
+ behavior to instead raise the original `SignalException` by setting
88
+
89
+ Sigurd.exit_on_signal = true
90
+
91
+ ## Contributing
92
+
93
+ Bug reports and pull requests are welcome on GitHub at https://github.com/flipp-oss/sigurd .
94
+
95
+ ### Linting
96
+
97
+ Sigurd uses Rubocop to lint the code. Please run Rubocop on your code
98
+ before submitting a PR.
99
+
100
+ ---
101
+ <p align="center">
102
+ Sponsored by<br/>
103
+ <a href="https://corp.flipp.com/">
104
+ <img src="support/flipp-logo.png" title="Flipp logo" style="border:none;"/>
105
+ </a>
106
+ </p>
@@ -5,6 +5,7 @@ module Sigurd
5
5
  # signals to ask them to stop nicely.
6
6
  class SignalHandler
7
7
  SIGNALS = %i(INT TERM QUIT).freeze
8
+ attr_reader :runner
8
9
 
9
10
  # Takes any object that responds to the `start` and `stop` methods.
10
11
  # @param runner[#start, #stop]
@@ -20,10 +21,15 @@ module Sigurd
20
21
  @runner.start
21
22
 
22
23
  loop do
23
- case signal_queue.pop
24
+ signal = signal_queue.pop
25
+ case signal
24
26
  when *SIGNALS
25
27
  @runner.stop
26
- break
28
+ if Sigurd.exit_on_signal
29
+ exit 0
30
+ else
31
+ raise(SignalException, signal)
32
+ end
27
33
  else
28
34
  ready = IO.select([reader, writer])
29
35
 
@@ -35,14 +41,13 @@ module Sigurd
35
41
 
36
42
  private
37
43
 
38
- attr_reader :reader, :writer, :signal_queue, :executor
44
+ attr_reader :reader, :writer, :signal_queue
39
45
 
40
46
  # https://stackoverflow.com/questions/29568298/run-code-when-signal-is-sent-but-do-not-trap-the-signal-in-ruby
41
47
  def prepend_handler(signal)
42
48
  previous = Signal.trap(signal) do
43
- previous = -> { raise SignalException, signal } unless previous.respond_to?(:call)
44
49
  yield
45
- previous.call
50
+ previous.call if previous&.respond_to?(:call)
46
51
  end
47
52
  end
48
53
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Sigurd
4
- VERSION = '0.0.1'
4
+ VERSION = '0.1.0'
5
5
  end
data/lib/sigurd.rb CHANGED
@@ -1,7 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'sigurd/version'
4
+ require 'sigurd/executor'
5
+ require 'sigurd/signal_handler'
4
6
 
5
7
  # :nodoc:
6
8
  module Sigurd
9
+
10
+ class << self
11
+ attr_accessor :exit_on_signal
12
+ end
13
+
7
14
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'sigurd/signal_handler'
3
+ require 'sigurd'
4
4
 
5
5
  RSpec.describe Sigurd::SignalHandler do
6
6
  describe '#run!' do
@@ -11,8 +11,22 @@ RSpec.describe Sigurd::SignalHandler do
11
11
  expect(runner).to receive(:stop)
12
12
 
13
13
  signal_handler = described_class.new(runner)
14
- signal_handler.send(:unblock, described_class::SIGNALS.first)
15
- signal_handler.run!
14
+ Thread.new { sleep 1; Process.kill('TERM', 0) }
15
+ expect { signal_handler.run! }.to raise_error(SignalException)
16
16
  end
17
+
18
+ context 'when exit_on_signal is true' do
19
+ it 'should raise a SignalException' do
20
+ Sigurd.exit_on_signal = true
21
+ runner = TestRunners::TestRunner.new
22
+ expect(runner).to receive(:start)
23
+ expect(runner).to receive(:stop)
24
+
25
+ signal_handler = described_class.new(runner)
26
+ Thread.new { sleep 1; Process.kill('TERM', 0) }
27
+ expect { signal_handler.run! }.to raise_error(SystemExit)
28
+ end
29
+ end
30
+
17
31
  end
18
32
  end
data/spec/spec_helper.rb CHANGED
@@ -55,4 +55,8 @@ RSpec.configure do |config|
55
55
  mocks.yield_receiver_to_any_instance_implementation_blocks = true
56
56
  mocks.verify_partial_doubles = true
57
57
  end
58
+
59
+ config.before(:each) do
60
+ Sigurd.exit_on_signal = false
61
+ end
58
62
  end
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sigurd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Orner
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-02 00:00:00.000000000 Z
11
+ date: 2022-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -136,7 +136,7 @@ dependencies:
136
136
  - - "~>"
137
137
  - !ruby/object:Gem::Version
138
138
  version: '1.27'
139
- description:
139
+ description:
140
140
  email:
141
141
  - daniel.orner@flipp.com
142
142
  executables: []
@@ -153,7 +153,6 @@ files:
153
153
  - CHANGELOG.md
154
154
  - CODE_OF_CONDUCT.md
155
155
  - Gemfile
156
- - Gemfile.lock
157
156
  - Guardfile
158
157
  - LICENSE.md
159
158
  - README.md
@@ -165,11 +164,12 @@ files:
165
164
  - spec/executor_spec.rb
166
165
  - spec/signal_handler_spec.rb
167
166
  - spec/spec_helper.rb
167
+ - support/flipp-logo.png
168
168
  homepage: ''
169
169
  licenses:
170
170
  - Apache-2.0
171
171
  metadata: {}
172
- post_install_message:
172
+ post_install_message:
173
173
  rdoc_options: []
174
174
  require_paths:
175
175
  - lib
@@ -185,7 +185,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
185
185
  version: '0'
186
186
  requirements: []
187
187
  rubygems_version: 3.1.2
188
- signing_key:
188
+ signing_key:
189
189
  specification_version: 4
190
190
  summary: Small gem to manage executing looping processes and signal handling.
191
191
  test_files:
data/Gemfile.lock DELETED
@@ -1,103 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- sigurd (0.0.1)
5
- concurrent-ruby (~> 1)
6
- exponential-backoff
7
-
8
- GEM
9
- remote: https://rubygems.org/
10
- specs:
11
- ast (2.4.0)
12
- coderay (1.1.3)
13
- concurrent-ruby (1.1.6)
14
- diff-lcs (1.3)
15
- exponential-backoff (0.0.4)
16
- ffi (1.13.0)
17
- formatador (0.2.5)
18
- guard (2.16.2)
19
- formatador (>= 0.2.4)
20
- listen (>= 2.7, < 4.0)
21
- lumberjack (>= 1.0.12, < 2.0)
22
- nenv (~> 0.1)
23
- notiffany (~> 0.0)
24
- pry (>= 0.9.12)
25
- shellany (~> 0.0)
26
- thor (>= 0.18.1)
27
- guard-compat (1.2.1)
28
- guard-rspec (4.7.3)
29
- guard (~> 2.1)
30
- guard-compat (~> 1.1)
31
- rspec (>= 2.99.0, < 4.0)
32
- guard-rubocop (1.3.0)
33
- guard (~> 2.0)
34
- rubocop (~> 0.20)
35
- listen (3.2.1)
36
- rb-fsevent (~> 0.10, >= 0.10.3)
37
- rb-inotify (~> 0.9, >= 0.9.10)
38
- lumberjack (1.2.5)
39
- method_source (1.0.0)
40
- nenv (0.3.0)
41
- notiffany (0.1.3)
42
- nenv (~> 0.1)
43
- shellany (~> 0.0)
44
- parallel (1.19.1)
45
- parser (2.7.1.3)
46
- ast (~> 2.4.0)
47
- pry (0.13.1)
48
- coderay (~> 1.1)
49
- method_source (~> 1.0)
50
- rainbow (3.0.0)
51
- rb-fsevent (0.10.4)
52
- rb-inotify (0.10.1)
53
- ffi (~> 1.0)
54
- regexp_parser (1.7.0)
55
- rexml (3.2.4)
56
- rspec (3.9.0)
57
- rspec-core (~> 3.9.0)
58
- rspec-expectations (~> 3.9.0)
59
- rspec-mocks (~> 3.9.0)
60
- rspec-core (3.9.2)
61
- rspec-support (~> 3.9.3)
62
- rspec-expectations (3.9.2)
63
- diff-lcs (>= 1.2.0, < 2.0)
64
- rspec-support (~> 3.9.0)
65
- rspec-mocks (3.9.1)
66
- diff-lcs (>= 1.2.0, < 2.0)
67
- rspec-support (~> 3.9.0)
68
- rspec-support (3.9.3)
69
- rspec_junit_formatter (0.4.1)
70
- rspec-core (>= 2, < 4, != 2.12.0)
71
- rubocop (0.85.0)
72
- parallel (~> 1.10)
73
- parser (>= 2.7.0.1)
74
- rainbow (>= 2.2.2, < 4.0)
75
- regexp_parser (>= 1.7)
76
- rexml
77
- rubocop-ast (>= 0.0.3)
78
- ruby-progressbar (~> 1.7)
79
- unicode-display_width (>= 1.4.0, < 2.0)
80
- rubocop-ast (0.0.3)
81
- parser (>= 2.7.0.1)
82
- rubocop-rspec (1.39.0)
83
- rubocop (>= 0.68.1)
84
- ruby-progressbar (1.10.1)
85
- shellany (0.0.1)
86
- thor (1.0.1)
87
- unicode-display_width (1.7.0)
88
-
89
- PLATFORMS
90
- ruby
91
-
92
- DEPENDENCIES
93
- guard (~> 2)
94
- guard-rspec (~> 4)
95
- guard-rubocop (~> 1)
96
- rspec (~> 3)
97
- rspec_junit_formatter (~> 0.3)
98
- rubocop (~> 0.72)
99
- rubocop-rspec (~> 1.27)
100
- sigurd!
101
-
102
- BUNDLED WITH
103
- 2.1.2