stapfen 1.2.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/stapfen/version.rb +1 -1
- data/lib/stapfen/worker.rb +45 -16
- data/spec/worker_spec.rb +17 -1
- metadata +3 -3
data/lib/stapfen/version.rb
CHANGED
data/lib/stapfen/worker.rb
CHANGED
@@ -6,12 +6,20 @@ module Stapfen
|
|
6
6
|
include Stapfen::Logger
|
7
7
|
|
8
8
|
class << self
|
9
|
-
attr_accessor :configuration, :consumers, :logger
|
9
|
+
attr_accessor :configuration, :consumers, :logger, :destructor
|
10
|
+
attr_accessor :workers
|
10
11
|
end
|
11
12
|
|
12
13
|
# Instantiate a new +Worker+ instance and run it
|
13
14
|
def self.run!
|
14
|
-
self.new
|
15
|
+
worker = self.new
|
16
|
+
|
17
|
+
@workers ||= []
|
18
|
+
@workers << worker
|
19
|
+
|
20
|
+
handle_signals
|
21
|
+
|
22
|
+
worker.run
|
15
23
|
end
|
16
24
|
|
17
25
|
# Expects a block to be passed which will yield the appropriate
|
@@ -30,7 +38,6 @@ module Stapfen
|
|
30
38
|
@logger = yield
|
31
39
|
end
|
32
40
|
|
33
|
-
|
34
41
|
# Main message consumption block
|
35
42
|
def self.consume(queue_name, headers={}, &block)
|
36
43
|
unless block_given?
|
@@ -40,14 +47,42 @@ module Stapfen
|
|
40
47
|
@consumers << [queue_name, headers, block]
|
41
48
|
end
|
42
49
|
|
43
|
-
|
50
|
+
# Optional method, specifes a block to execute when the worker is shutting
|
51
|
+
# down.
|
52
|
+
def self.shutdown(&block)
|
53
|
+
@destructor = block
|
54
|
+
end
|
55
|
+
|
56
|
+
# Utility method to set up the proper worker signal handlers
|
57
|
+
def self.handle_signals
|
58
|
+
return if @signals_handled
|
59
|
+
|
60
|
+
Signal.trap(:INT) do
|
61
|
+
workers.each do |w|
|
62
|
+
w.exit_cleanly
|
63
|
+
end
|
64
|
+
exit!
|
65
|
+
end
|
66
|
+
Signal.trap(:TERM) do
|
67
|
+
workers.each do |w|
|
68
|
+
w.exit_cleanly
|
69
|
+
end
|
70
|
+
end
|
44
71
|
|
45
|
-
|
46
|
-
handle_signals!
|
72
|
+
@signals_handled = true
|
47
73
|
end
|
48
74
|
|
75
|
+
|
76
|
+
|
77
|
+
############################################################################
|
78
|
+
# Instance Methods
|
79
|
+
############################################################################
|
80
|
+
|
81
|
+
attr_accessor :client
|
82
|
+
|
49
83
|
def run
|
50
84
|
@client = Stomp::Client.new(self.class.configuration)
|
85
|
+
debug("Running with #{@client} inside of Thread:#{Thread.current.object_id}")
|
51
86
|
|
52
87
|
self.class.consumers.each do |name, headers, block|
|
53
88
|
|
@@ -74,17 +109,11 @@ module Stapfen
|
|
74
109
|
end
|
75
110
|
end
|
76
111
|
|
77
|
-
|
78
|
-
|
79
|
-
exit_cleanly
|
80
|
-
exit!
|
81
|
-
end
|
82
|
-
Signal.trap(:TERM) do
|
83
|
-
exit_cleanly
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
112
|
+
# Invokes the shutdown block if it has been created, and closes the
|
113
|
+
# {{Stomp::Client}} connection unless it has already been shut down
|
87
114
|
def exit_cleanly
|
115
|
+
self.class.destructor.call if self.class.destructor
|
116
|
+
|
88
117
|
unless client.closed?
|
89
118
|
client.close
|
90
119
|
end
|
data/spec/worker_spec.rb
CHANGED
@@ -10,7 +10,8 @@ describe Stapfen::Worker do
|
|
10
10
|
it { should respond_to :run! }
|
11
11
|
it { should respond_to :configure }
|
12
12
|
it { should respond_to :consume }
|
13
|
-
it { should respond_to :
|
13
|
+
it { should respond_to :log }
|
14
|
+
it { should respond_to :shutdown }
|
14
15
|
|
15
16
|
describe '#configure' do
|
16
17
|
it 'should error when not passed a block' do
|
@@ -54,4 +55,19 @@ describe Stapfen::Worker do
|
|
54
55
|
end
|
55
56
|
end
|
56
57
|
|
58
|
+
context 'instance methods' do
|
59
|
+
describe '#exit_cleanly' do
|
60
|
+
let(:client) { double('RSpec Stomp Client') }
|
61
|
+
|
62
|
+
before :each do
|
63
|
+
worker.stub(:client).and_return(client)
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'should close the client' do
|
67
|
+
client.stub(:closed?).and_return(false)
|
68
|
+
client.should_receive(:close)
|
69
|
+
worker.exit_cleanly
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
57
73
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stapfen
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -63,7 +63,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
63
63
|
version: '0'
|
64
64
|
segments:
|
65
65
|
- 0
|
66
|
-
hash: -
|
66
|
+
hash: -3534259377773611788
|
67
67
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
68
|
none: false
|
69
69
|
requirements:
|
@@ -72,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
72
72
|
version: '0'
|
73
73
|
segments:
|
74
74
|
- 0
|
75
|
-
hash: -
|
75
|
+
hash: -3534259377773611788
|
76
76
|
requirements: []
|
77
77
|
rubyforge_project:
|
78
78
|
rubygems_version: 1.8.25
|