superbolt 0.3.5 → 0.3.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/superbolt/app.rb +30 -31
- data/lib/superbolt/runner/ack.rb +9 -0
- data/lib/superbolt/runner/ack_one.rb +13 -0
- data/lib/superbolt/runner/base.rb +28 -0
- data/lib/superbolt/runner/default.rb +31 -0
- data/lib/superbolt/runner/greedy.rb +6 -0
- data/lib/superbolt/runner/pop.rb +23 -0
- data/lib/superbolt/version.rb +1 -1
- data/lib/superbolt.rb +10 -0
- data/spec/app_spec.rb +33 -7
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dfc8976e1d00e4f51ff26d28b413bd750ae3f317
|
4
|
+
data.tar.gz: 13cf9ddcbd97306aa7ea95862531a3d4d256e473
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 122d5b6a367f4aa7bc6fd7641de8113d2fb70042066d362b8c385dc8641eb75e205965bd44077700417078fd730eb1c581d127cb98bf09a5926935d8cdb639ce
|
7
|
+
data.tar.gz: 085ecc038bba6700579e840a3192b38ba9929e622b1fb59861150a051f86f3fc48a43e151ef7608027867daa06c0519aa59953adfeb7cc9d53c9dd3134d88fdb
|
data/lib/superbolt/app.rb
CHANGED
@@ -1,19 +1,29 @@
|
|
1
1
|
module Superbolt
|
2
2
|
class App
|
3
|
-
attr_reader :config, :env
|
3
|
+
attr_reader :config, :env, :runner_type
|
4
4
|
attr_accessor :logger
|
5
5
|
|
6
6
|
def initialize(name, options={})
|
7
7
|
@name = name
|
8
|
-
@env =
|
9
|
-
@logger =
|
10
|
-
@
|
8
|
+
@env = options[:env] || Superbolt.env
|
9
|
+
@logger = options[:logger] || Logger.new($stdout)
|
10
|
+
@runner_type = options[:runner] || :default
|
11
|
+
@config = options[:config] || Superbolt.config
|
11
12
|
end
|
12
13
|
|
13
14
|
def name
|
14
15
|
env ? "#{@name}_#{env}" : @name
|
15
16
|
end
|
16
17
|
|
18
|
+
# just in case you have a handle to the app and want to quit it
|
19
|
+
def quit_queue
|
20
|
+
Queue.new("#{connection.name}.quit", connection.config)
|
21
|
+
end
|
22
|
+
|
23
|
+
def error_queue
|
24
|
+
Queue.new("#{connection.name}.error", connection.config)
|
25
|
+
end
|
26
|
+
|
17
27
|
def connection
|
18
28
|
@connection ||= Connection::App.new(name, config)
|
19
29
|
end
|
@@ -30,29 +40,11 @@ module Superbolt
|
|
30
40
|
connection.qq
|
31
41
|
end
|
32
42
|
|
33
|
-
def quit_queue
|
34
|
-
Queue.new("#{connection.name}.quit", connection.config)
|
35
|
-
end
|
36
|
-
|
37
|
-
def error_queue
|
38
|
-
Queue.new("#{connection.name}.error", connection.config)
|
39
|
-
end
|
40
|
-
|
41
43
|
def run(&block)
|
42
44
|
EventMachine.run do
|
43
|
-
# only get one message at a time for performance
|
44
|
-
# and tell it to auto reconnect, duh!
|
45
|
-
queue.channel.prefetch(1)
|
46
45
|
queue.channel.auto_recovery = true
|
47
46
|
|
48
|
-
|
49
|
-
message = IncomingMessage.new(metadata, payload, channel)
|
50
|
-
processor = Processor.new(message, logger, &block)
|
51
|
-
unless processor.perform
|
52
|
-
on_error(message.parse, processor.exception)
|
53
|
-
end
|
54
|
-
message.ack
|
55
|
-
end
|
47
|
+
runner_class.new(queue, error_queue, logger, block).run
|
56
48
|
|
57
49
|
quit_subscriber_queue.subscribe do |message|
|
58
50
|
quit(message)
|
@@ -60,14 +52,21 @@ module Superbolt
|
|
60
52
|
end
|
61
53
|
end
|
62
54
|
|
63
|
-
def
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
55
|
+
def runner_class
|
56
|
+
runner_map[runner_type] || default_runner
|
57
|
+
end
|
58
|
+
|
59
|
+
def runner_map
|
60
|
+
{
|
61
|
+
pop: Runner::Pop,
|
62
|
+
ack_one: Runner::AckOne,
|
63
|
+
ack: Runner::Ack,
|
64
|
+
greedy: Runner::Greedy
|
65
|
+
}
|
66
|
+
end
|
67
|
+
|
68
|
+
def default_runner
|
69
|
+
runner_map[:ack_one]
|
71
70
|
end
|
72
71
|
|
73
72
|
def quit(message='no message given')
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Superbolt
|
2
|
+
module Runner
|
3
|
+
class Base
|
4
|
+
attr_reader :queue, :error_queue, :logger, :block
|
5
|
+
|
6
|
+
def initialize(queue, error_queue, logger, block)
|
7
|
+
@queue = queue
|
8
|
+
@error_queue = error_queue
|
9
|
+
@logger = logger
|
10
|
+
@block = block
|
11
|
+
end
|
12
|
+
|
13
|
+
def channel
|
14
|
+
queue.channel
|
15
|
+
end
|
16
|
+
|
17
|
+
def on_error(message, error)
|
18
|
+
error_message = message.merge({error: {
|
19
|
+
class: error.class,
|
20
|
+
message: error.message,
|
21
|
+
backtrace: error.backtrace,
|
22
|
+
errored_at: Time.now
|
23
|
+
}})
|
24
|
+
error_queue.push(error_message)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Superbolt
|
2
|
+
module Runner
|
3
|
+
class Default < Base
|
4
|
+
def run
|
5
|
+
set_prefetch
|
6
|
+
subscribe
|
7
|
+
end
|
8
|
+
|
9
|
+
def set_prefetch
|
10
|
+
channel.prefetch(prefetch) if prefetch
|
11
|
+
end
|
12
|
+
|
13
|
+
def subscribe
|
14
|
+
queue.subscribe(ack: ack) do |metadata, payload|
|
15
|
+
message = IncomingMessage.new(metadata, payload, channel)
|
16
|
+
processor = Processor.new(message, logger, &block)
|
17
|
+
unless processor.perform
|
18
|
+
on_error(message.parse, processor.exception)
|
19
|
+
end
|
20
|
+
message.ack if ack
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def ack
|
25
|
+
end
|
26
|
+
|
27
|
+
def prefetch
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Superbolt
|
2
|
+
module Runner
|
3
|
+
class Pop < Base
|
4
|
+
attr_reader :message
|
5
|
+
|
6
|
+
def run
|
7
|
+
EventMachine.add_periodic_timer(0.01) do
|
8
|
+
next if message
|
9
|
+
|
10
|
+
queue.pop do |metadata, payload|
|
11
|
+
@message = IncomingMessage.new(metadata, payload, channel)
|
12
|
+
processor = Processor.new(message, logger, &block)
|
13
|
+
|
14
|
+
unless processor.perform
|
15
|
+
on_error(message.parse, processor.exception)
|
16
|
+
end
|
17
|
+
@message = nil
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/superbolt/version.rb
CHANGED
data/lib/superbolt.rb
CHANGED
@@ -7,12 +7,22 @@ require 'active_support/core_ext/module/delegation'
|
|
7
7
|
|
8
8
|
require "superbolt/version"
|
9
9
|
require "superbolt/config"
|
10
|
+
|
10
11
|
require "superbolt/adapter/base"
|
11
12
|
require "superbolt/adapter/bunny"
|
12
13
|
require "superbolt/adapter/amqp"
|
14
|
+
|
13
15
|
require "superbolt/connection/base"
|
14
16
|
require "superbolt/connection/queue"
|
15
17
|
require "superbolt/connection/app"
|
18
|
+
|
19
|
+
require "superbolt/runner/base"
|
20
|
+
require "superbolt/runner/default"
|
21
|
+
require "superbolt/runner/ack_one"
|
22
|
+
require "superbolt/runner/ack"
|
23
|
+
require "superbolt/runner/pop"
|
24
|
+
require "superbolt/runner/greedy"
|
25
|
+
|
16
26
|
require "superbolt/queue"
|
17
27
|
require "superbolt/incoming_message"
|
18
28
|
require "superbolt/app"
|
data/spec/app_spec.rb
CHANGED
@@ -1,15 +1,17 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Superbolt::App do
|
4
|
+
let(:app) {
|
5
|
+
Superbolt::App.new(name, {
|
6
|
+
env: env,
|
7
|
+
logger: logger,
|
8
|
+
runner: runner_type
|
9
|
+
})
|
10
|
+
}
|
11
|
+
|
4
12
|
let(:env) { 'test' }
|
5
13
|
let(:name) { 'superbolt' }
|
6
14
|
let(:logger) { Logger.new('/dev/null') }
|
7
|
-
let(:app) {
|
8
|
-
Superbolt::App.new(name, {
|
9
|
-
env: env,
|
10
|
-
logger: logger
|
11
|
-
})
|
12
|
-
}
|
13
15
|
let(:queue) { Superbolt::Queue.new("#{name}_#{env}") }
|
14
16
|
let(:quit_queue) { Superbolt::Queue.new("#{name}_#{env}.quit") }
|
15
17
|
let(:error_queue) { Superbolt::Queue.new("#{name}_#{env}.error") }
|
@@ -21,7 +23,7 @@ describe Superbolt::App do
|
|
21
23
|
error_queue.clear
|
22
24
|
end
|
23
25
|
|
24
|
-
|
26
|
+
shared_examples 'app' do
|
25
27
|
it "shuts down with any message to the quit queue" do
|
26
28
|
queue.push({please: 'stop'})
|
27
29
|
|
@@ -95,4 +97,28 @@ describe Superbolt::App do
|
|
95
97
|
error_queue.size.should == 1
|
96
98
|
end
|
97
99
|
end
|
100
|
+
|
101
|
+
context 'when runner acknowledges one' do
|
102
|
+
let(:runner_type) { :ack_one }
|
103
|
+
|
104
|
+
it_should_behave_like "app"
|
105
|
+
end
|
106
|
+
|
107
|
+
context 'when runner acknowledges without a prefetch limit' do
|
108
|
+
let(:runner_type) { :ack }
|
109
|
+
|
110
|
+
it_should_behave_like 'app'
|
111
|
+
end
|
112
|
+
|
113
|
+
context 'when runner does not acknowledge and has no limits' do
|
114
|
+
let(:runner_type) { :greedy }
|
115
|
+
|
116
|
+
it_should_behave_like 'app'
|
117
|
+
end
|
118
|
+
|
119
|
+
context 'when the runner pops without acknowledgment' do
|
120
|
+
let(:runner_type) { :pop }
|
121
|
+
|
122
|
+
it_should_behave_like "app"
|
123
|
+
end
|
98
124
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: superbolt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- socialchorus
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-01-
|
11
|
+
date: 2014-01-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -135,6 +135,12 @@ files:
|
|
135
135
|
- lib/superbolt/messenger.rb
|
136
136
|
- lib/superbolt/processor.rb
|
137
137
|
- lib/superbolt/queue.rb
|
138
|
+
- lib/superbolt/runner/ack.rb
|
139
|
+
- lib/superbolt/runner/ack_one.rb
|
140
|
+
- lib/superbolt/runner/base.rb
|
141
|
+
- lib/superbolt/runner/default.rb
|
142
|
+
- lib/superbolt/runner/greedy.rb
|
143
|
+
- lib/superbolt/runner/pop.rb
|
138
144
|
- lib/superbolt/spec_helpers.rb
|
139
145
|
- lib/superbolt/version.rb
|
140
146
|
- spec/app_spec.rb
|