startback 0.11.5 → 0.12.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/startback/context/middleware.rb +5 -9
- data/lib/startback/context.rb +7 -0
- data/lib/startback/event/agent.rb +76 -0
- data/lib/startback/event/bus/bunny/async.rb +162 -0
- data/lib/startback/{bus → event/bus}/bunny.rb +0 -0
- data/lib/startback/event/bus/memory/async.rb +45 -0
- data/lib/startback/event/bus/memory/sync.rb +35 -0
- data/lib/startback/{bus → event/bus}/memory.rb +0 -0
- data/lib/startback/event/bus.rb +100 -0
- data/lib/startback/event/engine.rb +132 -56
- data/lib/startback/event/ext/context.rb +5 -0
- data/lib/startback/event/ext/operation.rb +13 -0
- data/lib/startback/event.rb +7 -7
- data/lib/startback/support/robustness.rb +0 -2
- data/lib/startback/version.rb +2 -2
- data/spec/spec_helper.rb +26 -1
- data/spec/unit/context/test_dup.rb +3 -7
- data/spec/unit/context/test_fork.rb +38 -0
- data/spec/unit/context/test_h_factory.rb +0 -20
- data/spec/unit/context/test_middleware.rb +6 -8
- data/spec/unit/event/bus/memory/test_async.rb +43 -0
- data/spec/unit/event/bus/memory/test_sync.rb +43 -0
- data/spec/unit/test_event.rb +12 -19
- data/spec/unit/web/test_catch_all.rb +1 -1
- metadata +13 -10
- data/lib/startback/bus/bunny/async.rb +0 -123
- data/lib/startback/bus/memory/async.rb +0 -40
- data/lib/startback/bus/memory/sync.rb +0 -30
- data/lib/startback/bus.rb +0 -94
- data/spec/unit/bus/memory/test_async.rb +0 -41
- data/spec/unit/bus/memory/test_sync.rb +0 -41
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7f4770d1267ad944224e11f6c10ddaae3f2c3666860ef31c05ea3323282dda34
|
4
|
+
data.tar.gz: ec1a61367ab0947be2088dae981c6956359a26bc2517282a464e44e6449331b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 19813029ac9f81b1aed96f69a8e6ddd45e509d87e5b3bd05859f0b9155c16c5a05035cf2a28ff7df2df28f467a368702fa83180c24a62caf498337c3dde6d7da
|
7
|
+
data.tar.gz: 57abc7390c595f5c024276ed07ccb3580312d2de76a6a622e0a3284afefb38244b000b26e9f005d4516de67f3afccdc4789988466ffbb220f106cce3514f83b4
|
@@ -18,7 +18,7 @@ module Startback
|
|
18
18
|
#
|
19
19
|
# # Use a user defined context class
|
20
20
|
# Rack::Builder.new do
|
21
|
-
# use Startback::Context::Middleware,
|
21
|
+
# use Startback::Context::Middleware, MyContextClass.new
|
22
22
|
#
|
23
23
|
# run ->(env){
|
24
24
|
# ctx = env[Startback::Context::Middleware::RACK_ENV_KEY]
|
@@ -31,18 +31,14 @@ module Startback
|
|
31
31
|
|
32
32
|
RACK_ENV_KEY = 'SAMBACK_CONTEXT'
|
33
33
|
|
34
|
-
|
35
|
-
context_class: Context
|
36
|
-
}
|
37
|
-
|
38
|
-
def initialize(app, options = {})
|
34
|
+
def initialize(app, context = Context.new)
|
39
35
|
@app = app
|
40
|
-
@
|
36
|
+
@context = context
|
41
37
|
end
|
42
|
-
attr_reader :
|
38
|
+
attr_reader :context
|
43
39
|
|
44
40
|
def call(env)
|
45
|
-
env[RACK_ENV_KEY] ||=
|
41
|
+
env[RACK_ENV_KEY] ||= context.dup.tap{|c|
|
46
42
|
c.original_rack_env = env.dup
|
47
43
|
}
|
48
44
|
@app.call(env)
|
data/lib/startback/context.rb
CHANGED
@@ -112,6 +112,13 @@ module Startback
|
|
112
112
|
to_h.to_json(*args, &bl)
|
113
113
|
end
|
114
114
|
|
115
|
+
def fork(h = nil)
|
116
|
+
dup.tap{|duped|
|
117
|
+
self.class.h_factor!(duped, h) if h
|
118
|
+
yield(duped) if block_given?
|
119
|
+
}
|
120
|
+
end
|
121
|
+
|
115
122
|
def dup
|
116
123
|
super.tap{|c|
|
117
124
|
c.send(:clean_factored!)
|
@@ -1,7 +1,83 @@
|
|
1
1
|
module Startback
|
2
2
|
class Event
|
3
|
+
#
|
4
|
+
# An agent listen to specific events and react with its
|
5
|
+
# `call` method.
|
6
|
+
#
|
7
|
+
# This class is intended to be subclasses and the following
|
8
|
+
# methods overriden:
|
9
|
+
#
|
10
|
+
# - install_listeners that installs sync and async listeners
|
11
|
+
# - call to create a context and implement reaction behavior
|
12
|
+
#
|
3
13
|
class Agent
|
4
14
|
include Support::OperationRunner
|
15
|
+
include Support::Robustness
|
16
|
+
|
17
|
+
def initialize(engine)
|
18
|
+
@engine = engine
|
19
|
+
@context = nil
|
20
|
+
install_listeners
|
21
|
+
end
|
22
|
+
attr_reader :engine
|
23
|
+
attr_accessor :context
|
24
|
+
protected :context=
|
25
|
+
|
26
|
+
protected
|
27
|
+
|
28
|
+
# Installs the various event handlers by calling `sync`
|
29
|
+
# and `async` methods.
|
30
|
+
#
|
31
|
+
# This method is intended to be overriden.
|
32
|
+
def install_listeners
|
33
|
+
end
|
34
|
+
|
35
|
+
# Returns the underlying bus
|
36
|
+
def bus
|
37
|
+
engine.bus
|
38
|
+
end
|
39
|
+
|
40
|
+
# Asynchronously listen to a specific event.
|
41
|
+
#
|
42
|
+
# See Bus#listen
|
43
|
+
def async(exchange, queue)
|
44
|
+
bus.async.listen(exchange, queue) do |event_data|
|
45
|
+
event = engine.factor_event(event_data)
|
46
|
+
with_context(event.context).call(event)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# Synchronously listen to a specific event.
|
51
|
+
#
|
52
|
+
# See Bus#listen
|
53
|
+
def sync(exchange, queue)
|
54
|
+
bus.listen(exchange, queue) do |event_data|
|
55
|
+
event = engine.factor_event(event_data)
|
56
|
+
with_context(event.context).call(event)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Reacts to a specific event.
|
61
|
+
#
|
62
|
+
# This method must be implemented by subclasses and raises
|
63
|
+
# an error by default.
|
64
|
+
def call(event = nil)
|
65
|
+
log(:fatal, {
|
66
|
+
op: self.class,
|
67
|
+
op_data: event,
|
68
|
+
error: %Q{Unexpected call to Startback::Event::Agent#call},
|
69
|
+
backtrace: caller
|
70
|
+
})
|
71
|
+
raise NotImplementedError
|
72
|
+
end
|
73
|
+
|
74
|
+
def with_context(context)
|
75
|
+
dup.tap{|a| a.send(:context=, context) }
|
76
|
+
end
|
77
|
+
|
78
|
+
def operation_world(op)
|
79
|
+
super(op).merge(context: ctx)
|
80
|
+
end
|
5
81
|
|
6
82
|
end # class Agent
|
7
83
|
end # class Event
|
@@ -0,0 +1,162 @@
|
|
1
|
+
require 'bunny'
|
2
|
+
module Startback
|
3
|
+
class Event
|
4
|
+
class Bus
|
5
|
+
module Bunny
|
6
|
+
#
|
7
|
+
# Asynchronous implementation of the bus abstraction, on top of RabbitMQ
|
8
|
+
# and using the 'bunny' gem (you need to include it in your Gemfile
|
9
|
+
# yourself: it is NOT a startback official dependency).
|
10
|
+
#
|
11
|
+
# This bus implementation emits events by dumping them to RabbitMQ using
|
12
|
+
# the event type as exchange name. Listeners may use the `processor`
|
13
|
+
# parameter to specify the queue name ; otherwise a default "main" queue
|
14
|
+
# is used.
|
15
|
+
#
|
16
|
+
# Examples:
|
17
|
+
#
|
18
|
+
# # Connects to RabbitMQ using all default options
|
19
|
+
# #
|
20
|
+
# # Uses the STARTBACK_BUS_BUNNY_ASYNC_URL environment variable for
|
21
|
+
# # connection URL if present.
|
22
|
+
# Startback::Bus::Bunny::Async.new
|
23
|
+
#
|
24
|
+
# # Connects to RabbitMQ using a specific URL
|
25
|
+
# Startback::Bus::Bunny::Async.new("amqp://rabbituser:rabbitpass@192.168.17.17")
|
26
|
+
# Startback::Bus::Bunny::Async.new(url: "amqp://rabbituser:rabbitpass@192.168.17.17")
|
27
|
+
#
|
28
|
+
# # Connects to RabbitMQ using specific connection options. See Bunny's own
|
29
|
+
# # documentation
|
30
|
+
# Startback::Bus::Bunny::Async.new({
|
31
|
+
# connection_options: {
|
32
|
+
# host: "192.168.17.17"
|
33
|
+
# }
|
34
|
+
# })
|
35
|
+
#
|
36
|
+
class Async
|
37
|
+
include Support::Robustness
|
38
|
+
|
39
|
+
CHANNEL_KEY = 'Startback::Bus::Bunny::Async::ChannelKey'
|
40
|
+
|
41
|
+
DEFAULT_OPTIONS = {
|
42
|
+
# (optional) The URL to use for connecting to RabbitMQ.
|
43
|
+
url: ENV['STARTBACK_BUS_BUNNY_ASYNC_URL'],
|
44
|
+
|
45
|
+
# (optional) The options has to pass to ::Bunny constructor
|
46
|
+
connection_options: nil,
|
47
|
+
|
48
|
+
# (optional) The options to use for the emitter/listener fanout
|
49
|
+
fanout_options: {},
|
50
|
+
|
51
|
+
# (optional) The options to use for the listener queue
|
52
|
+
queue_options: {},
|
53
|
+
|
54
|
+
# (optional) Default event factory to use, if any
|
55
|
+
event_factory: nil,
|
56
|
+
|
57
|
+
# (optional) A default context to use for general logging
|
58
|
+
context: nil,
|
59
|
+
|
60
|
+
# (optional) Size of consumer pool
|
61
|
+
consumer_pool_size: 1,
|
62
|
+
|
63
|
+
# (optional) Whether the program must be aborted on consumption
|
64
|
+
# error
|
65
|
+
abort_on_exception: true,
|
66
|
+
|
67
|
+
# (optional) Whether connection occurs immediately,
|
68
|
+
# or on demand later
|
69
|
+
autoconnect: false
|
70
|
+
}
|
71
|
+
|
72
|
+
# Creates a bus instance, using the various options provided to
|
73
|
+
# fine-tune behavior.
|
74
|
+
def initialize(options = {})
|
75
|
+
options = { url: options } if options.is_a?(String)
|
76
|
+
@options = DEFAULT_OPTIONS.merge(options)
|
77
|
+
connect if @options[:autoconnect]
|
78
|
+
end
|
79
|
+
attr_reader :options
|
80
|
+
|
81
|
+
def connect
|
82
|
+
disconnect
|
83
|
+
conn = options[:connection_options] || options[:url]
|
84
|
+
try_max_times(10) do
|
85
|
+
@bunny = ::Bunny.new(conn)
|
86
|
+
@bunny.start
|
87
|
+
channel # make sure we already create the channel
|
88
|
+
log(:info, {op: "#{self.class.name}#connect", op_data: conn}, options[:context])
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def disconnect
|
93
|
+
if channel = Thread.current[CHANNEL_KEY]
|
94
|
+
channel.close
|
95
|
+
Thread.current[CHANNEL_KEY] = nil
|
96
|
+
end
|
97
|
+
@bunny.close if @bunny
|
98
|
+
end
|
99
|
+
|
100
|
+
def channel
|
101
|
+
unless @bunny
|
102
|
+
raise Startback::Errors::Error, "Please connect your bus first, or use autoconnect: true"
|
103
|
+
end
|
104
|
+
|
105
|
+
Thread.current[CHANNEL_KEY] ||= @bunny.create_channel(
|
106
|
+
nil,
|
107
|
+
consumer_pool_size, # consumer_pool_size
|
108
|
+
abort_on_exception? # consumer_pool_abort_on_exception
|
109
|
+
)
|
110
|
+
end
|
111
|
+
|
112
|
+
def emit(event)
|
113
|
+
stop_errors(self, "emit", event.context) do
|
114
|
+
fanout = channel.fanout(event.type.to_s, fanout_options)
|
115
|
+
fanout.publish(event.to_json)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def listen(type, processor = nil, listener = nil, &bl)
|
120
|
+
raise ArgumentError, "A listener must be provided" unless listener || bl
|
121
|
+
|
122
|
+
fanout = channel.fanout(type.to_s, fanout_options)
|
123
|
+
queue = channel.queue((processor || "main").to_s, queue_options)
|
124
|
+
queue.bind(fanout)
|
125
|
+
queue.subscribe do |delivery_info, properties, body|
|
126
|
+
stop_errors(self, "listen") do
|
127
|
+
(listener || bl).call(body)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
protected
|
133
|
+
|
134
|
+
def consumer_pool_size
|
135
|
+
options[:consumer_pool_size]
|
136
|
+
end
|
137
|
+
|
138
|
+
def abort_on_exception?
|
139
|
+
options[:abort_on_exception]
|
140
|
+
end
|
141
|
+
|
142
|
+
def fanout_options
|
143
|
+
options[:fanout_options]
|
144
|
+
end
|
145
|
+
|
146
|
+
def queue_options
|
147
|
+
options[:queue_options]
|
148
|
+
end
|
149
|
+
|
150
|
+
def factor_event(body)
|
151
|
+
if options[:event_factory]
|
152
|
+
options[:event_factory].call(body)
|
153
|
+
else
|
154
|
+
Event.json(body, options)
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
end # class Async
|
159
|
+
end # module Bunny
|
160
|
+
end # class Bus
|
161
|
+
end # class Event
|
162
|
+
end # module Startback
|
File without changes
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Startback
|
2
|
+
class Event
|
3
|
+
class Bus
|
4
|
+
module Memory
|
5
|
+
#
|
6
|
+
# Asynchronous implementation of the Bus abstraction, for use between
|
7
|
+
# components sharing the same process.
|
8
|
+
#
|
9
|
+
# This implementation actually calls listeners synchronously (it mays)
|
10
|
+
# but hides error raised by them. See Bus::Bunny::Async for another
|
11
|
+
# implementation that is truly asynchronous and relies on RabbitMQ.
|
12
|
+
#
|
13
|
+
class Async
|
14
|
+
include Support::Robustness
|
15
|
+
|
16
|
+
DEFAULT_OPTIONS = {
|
17
|
+
}
|
18
|
+
|
19
|
+
def initialize(options = {})
|
20
|
+
@options = DEFAULT_OPTIONS.merge(options)
|
21
|
+
@listeners = {}
|
22
|
+
end
|
23
|
+
|
24
|
+
def connect
|
25
|
+
end
|
26
|
+
|
27
|
+
def emit(event)
|
28
|
+
(@listeners[event.type.to_s] || []).each do |l|
|
29
|
+
stop_errors(self, "emit", event) {
|
30
|
+
l.call(event)
|
31
|
+
}
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def listen(type, processor = nil, listener = nil, &bl)
|
36
|
+
raise ArgumentError, "A listener must be provided" unless listener || bl
|
37
|
+
@listeners[type.to_s] ||= []
|
38
|
+
@listeners[type.to_s] << (listener || bl)
|
39
|
+
end
|
40
|
+
|
41
|
+
end # class Sync
|
42
|
+
end # module Memory
|
43
|
+
end # class Bus
|
44
|
+
end # class Event
|
45
|
+
end # module Startback
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Startback
|
2
|
+
class Event
|
3
|
+
class Bus
|
4
|
+
module Memory
|
5
|
+
#
|
6
|
+
# Synchronous implementation of the Bus abstraction, for use between
|
7
|
+
# components sharing the same process.
|
8
|
+
#
|
9
|
+
class Sync
|
10
|
+
include Support::Robustness
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@listeners = {}
|
14
|
+
end
|
15
|
+
|
16
|
+
def connect
|
17
|
+
end
|
18
|
+
|
19
|
+
def emit(event)
|
20
|
+
(@listeners[event.type.to_s] || []).each do |l|
|
21
|
+
l.call(event)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def listen(type, processor = nil, listener = nil, &bl)
|
26
|
+
raise ArgumentError, "A listener must be provided" unless listener || bl
|
27
|
+
@listeners[type.to_s] ||= []
|
28
|
+
@listeners[type.to_s] << (listener || bl)
|
29
|
+
end
|
30
|
+
|
31
|
+
end # class Sync
|
32
|
+
end # module Memory
|
33
|
+
end # class Bus
|
34
|
+
end # class Event
|
35
|
+
end # module Startback
|
File without changes
|
@@ -0,0 +1,100 @@
|
|
1
|
+
module Startback
|
2
|
+
class Event
|
3
|
+
#
|
4
|
+
# Sync and async bus abstraction allowing to register listeners and
|
5
|
+
# emitting events towards them.
|
6
|
+
#
|
7
|
+
# This bus actually decorates two busses, one in synchronous and the
|
8
|
+
# other one is asynchronous (optional).
|
9
|
+
#
|
10
|
+
# * A synchronous bus MUST call the listeners as part of emitting
|
11
|
+
# process, and MUST re-raise any error occuring during that process.
|
12
|
+
# See, e.g. Startback::Bus::Memory::Sync
|
13
|
+
#
|
14
|
+
# * An asynchronous bus MAY call the listeners later, but MUST hide
|
15
|
+
# errors to the emitter.
|
16
|
+
# See, e.g. Startback::Bus::Memory::Async
|
17
|
+
#
|
18
|
+
# This bus facade emits events to both sync and async busses (if any),
|
19
|
+
# and listen on the sync one by default.
|
20
|
+
#
|
21
|
+
# For emitters:
|
22
|
+
#
|
23
|
+
# # This will synchronously call every listeners who `listen`
|
24
|
+
# # on the synchronous bus (& reraise exceptions) then call
|
25
|
+
# # (possibly later) all listeners who `listen` on the
|
26
|
+
# # asynchronous bus if any (& hide exceptions).
|
27
|
+
# bus.emit(event)
|
28
|
+
#
|
29
|
+
# # This only reaches sync listeners
|
30
|
+
# bus.sync.emit(event)
|
31
|
+
#
|
32
|
+
# # This only reaches async listeners (an async bus must be set)
|
33
|
+
# bus.async.emit(event)
|
34
|
+
#
|
35
|
+
# Please note that there is currently no way to reach sync listeners
|
36
|
+
# without having to implement error handling on the emitter side.
|
37
|
+
#
|
38
|
+
# For listeners:
|
39
|
+
#
|
40
|
+
# # This will listen synchronously and make the emitter fail if
|
41
|
+
# # anything goes wrong with the callback:
|
42
|
+
# bus.listen(event_type) do |event|
|
43
|
+
# ...
|
44
|
+
# end
|
45
|
+
#
|
46
|
+
# # It is a shortcut for:
|
47
|
+
# bus.sync.listen(event_type) do |event| ... end
|
48
|
+
#
|
49
|
+
# This will listen asynchronously and could not make the emitter
|
50
|
+
# fail if something goes wrong with the callback.
|
51
|
+
# bus.async.listen(event_type) do |event|
|
52
|
+
# ...
|
53
|
+
# end
|
54
|
+
#
|
55
|
+
# Feel free to access the sync and async busses directly for specific
|
56
|
+
# cases though.
|
57
|
+
#
|
58
|
+
class Bus
|
59
|
+
include Support::Robustness
|
60
|
+
|
61
|
+
def initialize(sync = Memory::Sync.new, async = nil)
|
62
|
+
@sync = sync
|
63
|
+
@async = async
|
64
|
+
end
|
65
|
+
attr_reader :sync, :async
|
66
|
+
|
67
|
+
def connect
|
68
|
+
sync.connect if sync
|
69
|
+
async.connect if async
|
70
|
+
end
|
71
|
+
|
72
|
+
# Emits a particular event to the listeners.
|
73
|
+
#
|
74
|
+
# @arg event an event, should be an Event instance (through duck
|
75
|
+
# typing is allowed)
|
76
|
+
def emit(event)
|
77
|
+
monitor({
|
78
|
+
op: "Startback::Bus#emit",
|
79
|
+
op_data: {
|
80
|
+
event: { type: event.type }
|
81
|
+
}
|
82
|
+
}, event.context) do
|
83
|
+
sync.emit(event)
|
84
|
+
async.emit(event) if async
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
# Registers `listener` as being interested in receiving events of
|
89
|
+
# a specific type.
|
90
|
+
#
|
91
|
+
# @arg type: Symbol, the type of event the listener is interested in.
|
92
|
+
# @arg listener: Proc, the listener itself.
|
93
|
+
def listen(type, processor = nil, listener = nil, &bl)
|
94
|
+
sync.listen(type, processor, listener, &bl)
|
95
|
+
end
|
96
|
+
|
97
|
+
end # class Bus
|
98
|
+
end # class Event
|
99
|
+
end # module Startback
|
100
|
+
require_relative 'bus/memory'
|