pebbles-river 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,10 +11,10 @@ module Pebbles
11
11
  #
12
12
  # * `name`: Name of the process. Optional, defaults to `$0`.
13
13
  #
14
- # * `configure_start_command(command)`. Implement to add more options to
15
- # the start command, eg. configuration options. Optional.
14
+ # * `configure_start_command(command)`. Implement to add more
15
+ # options to the start command, eg. configuration options. Optional.
16
16
  #
17
- # * `on_start(options)`. Implement to inject code before the program
17
+ # * `on_start(options, helper)`. Implement to inject code before the program
18
18
  # starts. Options are the start options, a hash. Optional.
19
19
  #
20
20
  # * `configure_supervisor(supervisor)`. This must call `add_listener` on the
@@ -22,6 +22,12 @@ module Pebbles
22
22
  #
23
23
  class DaemonHelper
24
24
 
25
+ attr_accessor :logger
26
+
27
+ def self.run(adapter, options = {})
28
+ new(adapter, options).run
29
+ end
30
+
25
31
  def initialize(adapter, options = {})
26
32
  @adapter = adapter
27
33
 
@@ -41,7 +47,9 @@ module Pebbles
41
47
  c.option :pidfile, '-p', '--pidfile PIDFILE', 'Path to pid file.'
42
48
  c.option :workers, Integer, '-w', '--workers N', 'Set number of workers per queue (defaults to 1).'
43
49
  c.action do |_, options|
44
- start(options)
50
+ handle_exceptions do
51
+ start(options)
52
+ end
45
53
  end
46
54
  if @adapter.respond_to?(:configure_start_command)
47
55
  @adapter.configure_start_command(c)
@@ -52,7 +60,9 @@ module Pebbles
52
60
  c.description 'Stops daemon'
53
61
  c.option :pidfile, '-p', '--pidfile PIDFILE', 'Path to pid file.'
54
62
  c.action do |_, options|
55
- stop(options)
63
+ handle_exceptions do
64
+ stop(options)
65
+ end
56
66
  end
57
67
  end
58
68
  p.command(:status) do |c|
@@ -60,7 +70,9 @@ module Pebbles
60
70
  c.description 'Prints daemon status'
61
71
  c.option :pidfile, '-p', '--pidfile PIDFILE', 'Path to pid file.'
62
72
  c.action do |_, options|
63
- status(options)
73
+ handle_exceptions do
74
+ status(options)
75
+ end
64
76
  end
65
77
  end
66
78
  end
@@ -76,7 +88,7 @@ module Pebbles
76
88
 
77
89
  def start(options)
78
90
  if @adapter.respond_to?(:on_start)
79
- @adapter.on_start(options)
91
+ @adapter.on_start(options, self)
80
92
  end
81
93
 
82
94
  daemon = new_daemon(options)
@@ -146,6 +158,17 @@ module Pebbles
146
158
  nochdir: true)
147
159
  end
148
160
 
161
+ def handle_exceptions(&block)
162
+ yield
163
+ rescue => e
164
+ if @logger.respond_to?(:exception)
165
+ @logger.exception(e)
166
+ end
167
+ $stderr.puts "Error: #{e.class}: #{e}"
168
+ $stderr.puts e.backtrace.map { |s| "\t#{s}\n" }.join
169
+ exit(1)
170
+ end
171
+
149
172
  end
150
173
 
151
174
  end
@@ -22,12 +22,14 @@ module Pebbles
22
22
  def connect
23
23
  unless @session
24
24
  handle_session_error do
25
- @session = Bunny::Session.new
26
- @session.start
25
+ session = Bunny::Session.new
26
+ session.start
27
27
 
28
- @channel = @session.create_channel
28
+ channel = session.create_channel
29
29
 
30
- @exchange = @channel.exchange(exchange_name, EXCHANGE_OPTIONS.dup)
30
+ exchange = channel.exchange(exchange_name, EXCHANGE_OPTIONS.dup)
31
+
32
+ @session, @channel, @exchange = session, channel, exchange
31
33
  end
32
34
  end
33
35
  end
@@ -12,17 +12,22 @@ module Pebbles
12
12
 
13
13
  @worker_count = options[:worker_count] || 1
14
14
 
15
- @queue_modules = []
15
+ @prefork_pools = []
16
+
17
+ @worker_modules = []
16
18
  end
17
19
 
18
20
  def start_workers
19
- if @queue_modules.empty?
21
+ if @worker_modules.empty?
20
22
  raise ConfigurationError.new("No listeners configured")
21
23
  end
22
24
 
23
- @prefork = MultiPrefork.new(
24
- min_workers: @worker_count,
25
- modules: @queue_modules)
25
+ @worker_modules.each do |m|
26
+ @prefork_pools.push(
27
+ Servolux::Prefork.new(
28
+ min_workers: @worker_count,
29
+ module: m))
30
+ end
26
31
  end
27
32
 
28
33
  def add_listener(listener, queue_spec)
@@ -39,10 +44,16 @@ module Pebbles
39
44
  process_name = "#{@name}: queue worker: #{queue_spec[:name]}"
40
45
  logger = @logger
41
46
 
42
- @queue_modules.push(-> {
43
- $0 = process_name
44
- trap('TERM') { worker.stop }
45
- worker.run
47
+ @worker_modules.push(Module.new {
48
+ define_method :execute do
49
+ $0 = process_name
50
+ trap('TERM') do
51
+ logger.info "Worker received TERM"
52
+ worker.stop
53
+ exit(0)
54
+ end
55
+ worker.run
56
+ end
46
57
  })
47
58
  end
48
59
 
@@ -51,7 +62,9 @@ module Pebbles
51
62
  $0 = "#{name}: master"
52
63
 
53
64
  logger.info "Starting workers"
54
- @prefork.start(1)
65
+ @prefork_pools.each do |prefork|
66
+ prefork.start(1)
67
+ end
55
68
  end
56
69
 
57
70
  # From Servolux::Server
@@ -66,7 +79,9 @@ module Pebbles
66
79
 
67
80
  # From Servolux::Server
68
81
  def run
69
- @prefork.ensure_worker_pool_size
82
+ @prefork_pools.each do |prefork|
83
+ prefork.ensure_worker_pool_size
84
+ end
70
85
  rescue => e
71
86
  if logger.respond_to? :exception
72
87
  logger.exception(e)
@@ -80,9 +95,9 @@ module Pebbles
80
95
 
81
96
  def shutdown_workers
82
97
  logger.info "Shutting down all workers"
83
- @prefork.stop
98
+ @prefork_pools.each(&:stop)
84
99
  loop do
85
- break if @prefork.live_worker_count <= 0
100
+ break if @prefork_pools.all? { |prefork| prefork.live_worker_count <= 0 }
86
101
  logger.info "Waiting for workers to quit"
87
102
  sleep 0.25
88
103
  end
@@ -1,5 +1,5 @@
1
1
  module Pebbles
2
2
  module River
3
- VERSION = '0.1.1'
3
+ VERSION = '0.1.3'
4
4
  end
5
5
  end
data/lib/pebbles/river.rb CHANGED
@@ -8,7 +8,6 @@ require 'servolux'
8
8
  require_relative "river/version"
9
9
  require_relative "river/errors"
10
10
  require_relative "river/message"
11
- require_relative "river/multi_prefork"
12
11
  require_relative "river/worker"
13
12
  require_relative "river/subscription"
14
13
  require_relative "river/supervisor"
@@ -16,5 +15,4 @@ require_relative "river/routing"
16
15
  require_relative "river/river"
17
16
  require_relative "river/compatibility"
18
17
  require_relative "river/rate_limiter"
19
- require_relative "river/multi_prefork"
20
18
  require_relative "river/daemon_helper"
metadata CHANGED
@@ -1,7 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pebbles-river
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Alexander Staubo
@@ -9,11 +10,12 @@ authors:
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2014-06-26 00:00:00.000000000 Z
13
+ date: 2014-07-09 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: pebblebed
16
17
  requirement: !ruby/object:Gem::Requirement
18
+ none: false
17
19
  requirements:
18
20
  - - ! '>='
19
21
  - !ruby/object:Gem::Version
@@ -21,6 +23,7 @@ dependencies:
21
23
  type: :runtime
22
24
  prerelease: false
23
25
  version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
24
27
  requirements:
25
28
  - - ! '>='
26
29
  - !ruby/object:Gem::Version
@@ -28,6 +31,7 @@ dependencies:
28
31
  - !ruby/object:Gem::Dependency
29
32
  name: bunny
30
33
  requirement: !ruby/object:Gem::Requirement
34
+ none: false
31
35
  requirements:
32
36
  - - ~>
33
37
  - !ruby/object:Gem::Version
@@ -35,6 +39,7 @@ dependencies:
35
39
  type: :runtime
36
40
  prerelease: false
37
41
  version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
38
43
  requirements:
39
44
  - - ~>
40
45
  - !ruby/object:Gem::Version
@@ -42,6 +47,7 @@ dependencies:
42
47
  - !ruby/object:Gem::Dependency
43
48
  name: activesupport
44
49
  requirement: !ruby/object:Gem::Requirement
50
+ none: false
45
51
  requirements:
46
52
  - - ! '>='
47
53
  - !ruby/object:Gem::Version
@@ -49,6 +55,7 @@ dependencies:
49
55
  type: :runtime
50
56
  prerelease: false
51
57
  version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
52
59
  requirements:
53
60
  - - ! '>='
54
61
  - !ruby/object:Gem::Version
@@ -56,6 +63,7 @@ dependencies:
56
63
  - !ruby/object:Gem::Dependency
57
64
  name: servolux
58
65
  requirement: !ruby/object:Gem::Requirement
66
+ none: false
59
67
  requirements:
60
68
  - - ~>
61
69
  - !ruby/object:Gem::Version
@@ -63,6 +71,7 @@ dependencies:
63
71
  type: :runtime
64
72
  prerelease: false
65
73
  version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
66
75
  requirements:
67
76
  - - ~>
68
77
  - !ruby/object:Gem::Version
@@ -70,6 +79,7 @@ dependencies:
70
79
  - !ruby/object:Gem::Dependency
71
80
  name: mercenary
72
81
  requirement: !ruby/object:Gem::Requirement
82
+ none: false
73
83
  requirements:
74
84
  - - ~>
75
85
  - !ruby/object:Gem::Version
@@ -77,6 +87,7 @@ dependencies:
77
87
  type: :runtime
78
88
  prerelease: false
79
89
  version_requirements: !ruby/object:Gem::Requirement
90
+ none: false
80
91
  requirements:
81
92
  - - ~>
82
93
  - !ruby/object:Gem::Version
@@ -84,6 +95,7 @@ dependencies:
84
95
  - !ruby/object:Gem::Dependency
85
96
  name: rspec
86
97
  requirement: !ruby/object:Gem::Requirement
98
+ none: false
87
99
  requirements:
88
100
  - - ~>
89
101
  - !ruby/object:Gem::Version
@@ -91,6 +103,7 @@ dependencies:
91
103
  type: :development
92
104
  prerelease: false
93
105
  version_requirements: !ruby/object:Gem::Requirement
106
+ none: false
94
107
  requirements:
95
108
  - - ~>
96
109
  - !ruby/object:Gem::Version
@@ -98,6 +111,7 @@ dependencies:
98
111
  - !ruby/object:Gem::Dependency
99
112
  name: bundler
100
113
  requirement: !ruby/object:Gem::Requirement
114
+ none: false
101
115
  requirements:
102
116
  - - ~>
103
117
  - !ruby/object:Gem::Version
@@ -105,6 +119,7 @@ dependencies:
105
119
  type: :development
106
120
  prerelease: false
107
121
  version_requirements: !ruby/object:Gem::Requirement
122
+ none: false
108
123
  requirements:
109
124
  - - ~>
110
125
  - !ruby/object:Gem::Version
@@ -112,6 +127,7 @@ dependencies:
112
127
  - !ruby/object:Gem::Dependency
113
128
  name: rake
114
129
  requirement: !ruby/object:Gem::Requirement
130
+ none: false
115
131
  requirements:
116
132
  - - ! '>='
117
133
  - !ruby/object:Gem::Version
@@ -119,6 +135,7 @@ dependencies:
119
135
  type: :development
120
136
  prerelease: false
121
137
  version_requirements: !ruby/object:Gem::Requirement
138
+ none: false
122
139
  requirements:
123
140
  - - ! '>='
124
141
  - !ruby/object:Gem::Version
@@ -126,6 +143,7 @@ dependencies:
126
143
  - !ruby/object:Gem::Dependency
127
144
  name: simplecov
128
145
  requirement: !ruby/object:Gem::Requirement
146
+ none: false
129
147
  requirements:
130
148
  - - ! '>='
131
149
  - !ruby/object:Gem::Version
@@ -133,6 +151,7 @@ dependencies:
133
151
  type: :development
134
152
  prerelease: false
135
153
  version_requirements: !ruby/object:Gem::Requirement
154
+ none: false
136
155
  requirements:
137
156
  - - ! '>='
138
157
  - !ruby/object:Gem::Version
@@ -154,7 +173,6 @@ files:
154
173
  - lib/pebbles/river/daemon_helper.rb
155
174
  - lib/pebbles/river/errors.rb
156
175
  - lib/pebbles/river/message.rb
157
- - lib/pebbles/river/multi_prefork.rb
158
176
  - lib/pebbles/river/rate_limiter.rb
159
177
  - lib/pebbles/river/river.rb
160
178
  - lib/pebbles/river/routing.rb
@@ -171,26 +189,27 @@ files:
171
189
  homepage: ''
172
190
  licenses:
173
191
  - MIT
174
- metadata: {}
175
192
  post_install_message:
176
193
  rdoc_options: []
177
194
  require_paths:
178
195
  - lib
179
196
  required_ruby_version: !ruby/object:Gem::Requirement
197
+ none: false
180
198
  requirements:
181
199
  - - ! '>='
182
200
  - !ruby/object:Gem::Version
183
201
  version: '0'
184
202
  required_rubygems_version: !ruby/object:Gem::Requirement
203
+ none: false
185
204
  requirements:
186
205
  - - ! '>='
187
206
  - !ruby/object:Gem::Version
188
207
  version: '0'
189
208
  requirements: []
190
209
  rubyforge_project:
191
- rubygems_version: 2.2.2
210
+ rubygems_version: 1.8.23.2
192
211
  signing_key:
193
- specification_version: 4
212
+ specification_version: 3
194
213
  summary: Implements an event river mechanism for Pebblebed.
195
214
  test_files:
196
215
  - spec/lib/river_spec.rb
checksums.yaml DELETED
@@ -1,15 +0,0 @@
1
- ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- YzUyMjRmYmNiMDMxODU5NTYwMjYxZjg1ZWE1ZmJkZDg2ZWE5NzY0Zg==
5
- data.tar.gz: !binary |-
6
- ZDc2ZjlmNzFkMWUxYWQ5NTk3NzkwZmNjMDQ1NGYwN2FmY2UwYjJlNg==
7
- SHA512:
8
- metadata.gz: !binary |-
9
- MzBjMDMzY2VhOTMwMDIzMjM4NTY5MGRjNzJkYzM0NTM2MGVmZGM5ZmZkNzUw
10
- NTdjOWEzMWI1NDUyNjc0MzI2OWVhMjIxNTdmYTEzNmY4YmM5NTg4NzAzYzUw
11
- ZTMyNjFiNjYxMTgxYWQxY2UzNTI5NjkyYjMzYjhmMDE4MTRkZDM=
12
- data.tar.gz: !binary |-
13
- YzRjMWUyZmFmMWQxNDA4NWMzNDE5MTYxYTQ1NzJjYjJmZjk5Zjc1MTIxYjNl
14
- YjZmNTQ3ZDJjYTc4NmRkODZjNTdkM2I5ZTk5YjdhNTk3MzA5MzMxYTZmOWNi
15
- NzhiZTBjNDIwMmVhZDgxODMyY2M4OGU1MGRjYzE4YmI2OWFlYzk=
@@ -1,49 +0,0 @@
1
- module Pebbles
2
- module River
3
-
4
- # Overrides preforker to instantiate a round-robin set of modules. The
5
- # desired worker count is multiplied by the number of modules; for example,
6
- # if specifying modules [A, B] and the worker count is 2, then 4 actual
7
- # processes will be forked.
8
- class MultiPrefork < ::Servolux::Prefork
9
-
10
- def initialize(options, &block)
11
- raise ArgumentError, "Block invocation not supported" if block
12
- raise ArgumentError, "Must pass :modules, not :module" if options[:module]
13
-
14
- # Like Prefork we support passing procs
15
- @modules = options[:modules].map { |m|
16
- if m.is_a?(Proc)
17
- Module.new { define_method :execute, &m }
18
- else
19
- m
20
- end
21
- }
22
- @index = 0
23
-
24
- options = {}.merge(options)
25
- options[:module] = @modules.first
26
- options.delete(:modules)
27
- if (min_workers = options[:min_workers])
28
- options[:min_workers] = min_workers * @modules.length
29
- end
30
- if (max_workers = options[:max_workers])
31
- options[:max_workers] = max_workers * @modules.length
32
- end
33
-
34
- super(options)
35
- end
36
-
37
- # This cheats by overriding `Prefork#add_workers` and replacing the
38
- # `@module instance` variable.
39
- def add_workers(number = 1)
40
- number.times do
41
- @module = @modules[@index]
42
- super(1)
43
- @index = (@index + 1) % @modules.length
44
- end
45
- end
46
- end
47
-
48
- end
49
- end