startback 0.11.5 → 0.12.2
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.
- 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
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: startback
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.12.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bernard Lambeau
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-05-
|
11
|
+
date: 2022-05-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -385,12 +385,6 @@ files:
|
|
385
385
|
- lib/startback/audit/prometheus.rb
|
386
386
|
- lib/startback/audit/shared.rb
|
387
387
|
- lib/startback/audit/trailer.rb
|
388
|
-
- lib/startback/bus.rb
|
389
|
-
- lib/startback/bus/bunny.rb
|
390
|
-
- lib/startback/bus/bunny/async.rb
|
391
|
-
- lib/startback/bus/memory.rb
|
392
|
-
- lib/startback/bus/memory/async.rb
|
393
|
-
- lib/startback/bus/memory/sync.rb
|
394
388
|
- lib/startback/caching/entity_cache.rb
|
395
389
|
- lib/startback/caching/no_store.rb
|
396
390
|
- lib/startback/caching/store.rb
|
@@ -399,7 +393,15 @@ files:
|
|
399
393
|
- lib/startback/errors.rb
|
400
394
|
- lib/startback/event.rb
|
401
395
|
- lib/startback/event/agent.rb
|
396
|
+
- lib/startback/event/bus.rb
|
397
|
+
- lib/startback/event/bus/bunny.rb
|
398
|
+
- lib/startback/event/bus/bunny/async.rb
|
399
|
+
- lib/startback/event/bus/memory.rb
|
400
|
+
- lib/startback/event/bus/memory/async.rb
|
401
|
+
- lib/startback/event/bus/memory/sync.rb
|
402
402
|
- lib/startback/event/engine.rb
|
403
|
+
- lib/startback/event/ext/context.rb
|
404
|
+
- lib/startback/event/ext/operation.rb
|
403
405
|
- lib/startback/ext.rb
|
404
406
|
- lib/startback/ext/date_time.rb
|
405
407
|
- lib/startback/ext/time.rb
|
@@ -431,13 +433,14 @@ files:
|
|
431
433
|
- spec/spec_helper.rb
|
432
434
|
- spec/unit/audit/test_prometheus.rb
|
433
435
|
- spec/unit/audit/test_trailer.rb
|
434
|
-
- spec/unit/bus/memory/test_async.rb
|
435
|
-
- spec/unit/bus/memory/test_sync.rb
|
436
436
|
- spec/unit/caching/test_entity_cache.rb
|
437
437
|
- spec/unit/context/test_abstraction_factory.rb
|
438
438
|
- spec/unit/context/test_dup.rb
|
439
|
+
- spec/unit/context/test_fork.rb
|
439
440
|
- spec/unit/context/test_h_factory.rb
|
440
441
|
- spec/unit/context/test_middleware.rb
|
442
|
+
- spec/unit/event/bus/memory/test_async.rb
|
443
|
+
- spec/unit/event/bus/memory/test_sync.rb
|
441
444
|
- spec/unit/support/hooks/test_after_hook.rb
|
442
445
|
- spec/unit/support/hooks/test_before_hook.rb
|
443
446
|
- spec/unit/support/operation_runner/test_around_run.rb
|
@@ -1,123 +0,0 @@
|
|
1
|
-
require 'bunny'
|
2
|
-
module Startback
|
3
|
-
class Bus
|
4
|
-
module Bunny
|
5
|
-
#
|
6
|
-
# Asynchronous implementation of the bus abstraction, on top of RabbitMQ
|
7
|
-
# and using the 'bunny' gem (you need to include it in your Gemfile
|
8
|
-
# yourself: it is NOT a startback official dependency).
|
9
|
-
#
|
10
|
-
# This bus implementation emits events by dumping them to RabbitMQ using
|
11
|
-
# the event type as exchange name. Listeners may use the `processor`
|
12
|
-
# parameter to specify the queue name ; otherwise a default "main" queue
|
13
|
-
# is used.
|
14
|
-
#
|
15
|
-
# Examples:
|
16
|
-
#
|
17
|
-
# # Connects to RabbitMQ using all default options
|
18
|
-
# #
|
19
|
-
# # Uses the STARTBACK_BUS_BUNNY_ASYNC_URL environment variable for
|
20
|
-
# # connection URL if present.
|
21
|
-
# Startback::Bus::Bunny::Async.new
|
22
|
-
#
|
23
|
-
# # Connects to RabbitMQ using a specific URL
|
24
|
-
# Startback::Bus::Bunny::Async.new("amqp://rabbituser:rabbitpass@192.168.17.17")
|
25
|
-
# Startback::Bus::Bunny::Async.new(url: "amqp://rabbituser:rabbitpass@192.168.17.17")
|
26
|
-
#
|
27
|
-
# # Connects to RabbitMQ using specific connection options. See Bunny's own
|
28
|
-
# # documentation
|
29
|
-
# Startback::Bus::Bunny::Async.new({
|
30
|
-
# connection_options: {
|
31
|
-
# host: "192.168.17.17"
|
32
|
-
# }
|
33
|
-
# })
|
34
|
-
#
|
35
|
-
class Async
|
36
|
-
include Support::Robustness
|
37
|
-
|
38
|
-
CHANNEL_KEY = 'Startback::Bus::Bunny::Async::ChannelKey'
|
39
|
-
|
40
|
-
DEFAULT_OPTIONS = {
|
41
|
-
# (optional) The URL to use for connecting to RabbitMQ.
|
42
|
-
url: ENV['STARTBACK_BUS_BUNNY_ASYNC_URL'],
|
43
|
-
|
44
|
-
# (optional) The options has to pass to ::Bunny constructor
|
45
|
-
connection_options: nil,
|
46
|
-
|
47
|
-
# (optional) The options to use for the emitter/listener fanout
|
48
|
-
fanout_options: {},
|
49
|
-
|
50
|
-
# (optional) The options to use for the listener queue
|
51
|
-
queue_options: {},
|
52
|
-
|
53
|
-
# (optional) Default event factory to use, if any
|
54
|
-
event_factory: nil,
|
55
|
-
|
56
|
-
# (optional) A default context to use for general logging
|
57
|
-
context: nil
|
58
|
-
}
|
59
|
-
|
60
|
-
# Creates a bus instance, using the various options provided to
|
61
|
-
# fine-tune behavior.
|
62
|
-
def initialize(options = {})
|
63
|
-
options = { url: options } if options.is_a?(String)
|
64
|
-
@options = DEFAULT_OPTIONS.merge(options)
|
65
|
-
retried = 0
|
66
|
-
conn = options[:connection_options] || options[:url]
|
67
|
-
try_max_times(10) do
|
68
|
-
@bunny = ::Bunny.new(conn)
|
69
|
-
@bunny.start
|
70
|
-
channel
|
71
|
-
log(:info, {op: "#{self.class.name}#connect", op_data: conn}, options[:context])
|
72
|
-
end
|
73
|
-
end
|
74
|
-
attr_reader :options
|
75
|
-
|
76
|
-
def channel
|
77
|
-
Thread.current[CHANNEL_KEY] ||= @bunny.create_channel
|
78
|
-
end
|
79
|
-
|
80
|
-
def emit(event)
|
81
|
-
stop_errors(self, "emit", event.context) do
|
82
|
-
fanout = channel.fanout(event.type.to_s, fanout_options)
|
83
|
-
fanout.publish(event.to_json)
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
def listen(type, processor = nil, listener = nil, &bl)
|
88
|
-
raise ArgumentError, "A listener must be provided" unless listener || bl
|
89
|
-
fanout = channel.fanout(type.to_s, fanout_options)
|
90
|
-
queue = channel.queue((processor || "main").to_s, queue_options)
|
91
|
-
queue.bind(fanout)
|
92
|
-
queue.subscribe do |delivery_info, properties, body|
|
93
|
-
event = stop_errors(self, "listen") do
|
94
|
-
factor_event(body)
|
95
|
-
end
|
96
|
-
stop_errors(self, "listen", event.context) do
|
97
|
-
(listener || bl).call(event)
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
protected
|
103
|
-
|
104
|
-
def fanout_options
|
105
|
-
options[:fanout_options]
|
106
|
-
end
|
107
|
-
|
108
|
-
def queue_options
|
109
|
-
options[:queue_options]
|
110
|
-
end
|
111
|
-
|
112
|
-
def factor_event(body)
|
113
|
-
if options[:event_factory]
|
114
|
-
options[:event_factory].call(body)
|
115
|
-
else
|
116
|
-
Event.json(body, options)
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
end # class Async
|
121
|
-
end # module Bunny
|
122
|
-
end # class Bus
|
123
|
-
end # module Klaro
|
@@ -1,40 +0,0 @@
|
|
1
|
-
module Startback
|
2
|
-
class Bus
|
3
|
-
module Memory
|
4
|
-
#
|
5
|
-
# Asynchronous implementation of the Bus abstraction, for use between
|
6
|
-
# components sharing the same process.
|
7
|
-
#
|
8
|
-
# This implementation actually calls listeners synchronously (it mays)
|
9
|
-
# but hides error raised by them. See Bus::Bunny::Async for another
|
10
|
-
# implementation that is truly asynchronous and relies on RabbitMQ.
|
11
|
-
#
|
12
|
-
class Async
|
13
|
-
include Support::Robustness
|
14
|
-
|
15
|
-
DEFAULT_OPTIONS = {
|
16
|
-
}
|
17
|
-
|
18
|
-
def initialize(options = {})
|
19
|
-
@options = DEFAULT_OPTIONS.merge(options)
|
20
|
-
@listeners = {}
|
21
|
-
end
|
22
|
-
|
23
|
-
def emit(event)
|
24
|
-
(@listeners[event.type.to_s] || []).each do |l|
|
25
|
-
stop_errors(self, "emit", event) {
|
26
|
-
l.call(event)
|
27
|
-
}
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
def listen(type, processor = nil, listener = nil, &bl)
|
32
|
-
raise ArgumentError, "A listener must be provided" unless listener || bl
|
33
|
-
@listeners[type.to_s] ||= []
|
34
|
-
@listeners[type.to_s] << (listener || bl)
|
35
|
-
end
|
36
|
-
|
37
|
-
end # class Sync
|
38
|
-
end # module Memory
|
39
|
-
end # class Bus
|
40
|
-
end # module Klaro
|
@@ -1,30 +0,0 @@
|
|
1
|
-
module Startback
|
2
|
-
class Bus
|
3
|
-
module Memory
|
4
|
-
#
|
5
|
-
# Synchronous implementation of the Bus abstraction, for use between
|
6
|
-
# components sharing the same process.
|
7
|
-
#
|
8
|
-
class Sync
|
9
|
-
include Support::Robustness
|
10
|
-
|
11
|
-
def initialize
|
12
|
-
@listeners = {}
|
13
|
-
end
|
14
|
-
|
15
|
-
def emit(event)
|
16
|
-
(@listeners[event.type.to_s] || []).each do |l|
|
17
|
-
l.call(event)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def listen(type, processor = nil, listener = nil, &bl)
|
22
|
-
raise ArgumentError, "A listener must be provided" unless listener || bl
|
23
|
-
@listeners[type.to_s] ||= []
|
24
|
-
@listeners[type.to_s] << (listener || bl)
|
25
|
-
end
|
26
|
-
|
27
|
-
end # class Sync
|
28
|
-
end # module Memory
|
29
|
-
end # class Bus
|
30
|
-
end # module Klaro
|
data/lib/startback/bus.rb
DELETED
@@ -1,94 +0,0 @@
|
|
1
|
-
require 'startback/event'
|
2
|
-
module Startback
|
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
|
-
# Emits a particular event to the listeners.
|
68
|
-
#
|
69
|
-
# @arg event an event, should be an Event instance (through duck
|
70
|
-
# typing is allowed)
|
71
|
-
def emit(event)
|
72
|
-
monitor({
|
73
|
-
op: "Startback::Bus#emit",
|
74
|
-
op_data: {
|
75
|
-
event: { type: event.type }
|
76
|
-
}
|
77
|
-
}, event.context) do
|
78
|
-
sync.emit(event)
|
79
|
-
async.emit(event) if async
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
# Registers `listener` as being interested in receiving events of
|
84
|
-
# a specific type.
|
85
|
-
#
|
86
|
-
# @arg type: Symbol, the type of event the listener is interested in.
|
87
|
-
# @arg listener: Proc, the listener itself.
|
88
|
-
def listen(type, processor = nil, listener = nil, &bl)
|
89
|
-
sync.listen(type, processor, listener, &bl)
|
90
|
-
end
|
91
|
-
|
92
|
-
end # class Bus
|
93
|
-
end # module Klaro
|
94
|
-
require_relative 'bus/memory'
|
@@ -1,41 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
module Startback
|
3
|
-
describe Bus::Memory do
|
4
|
-
|
5
|
-
subject{
|
6
|
-
Bus::Memory::Async.new
|
7
|
-
}
|
8
|
-
|
9
|
-
it 'allows emiting an receiving' do
|
10
|
-
seen = nil
|
11
|
-
subject.listen("user_changed") do |evt|
|
12
|
-
seen = evt
|
13
|
-
end
|
14
|
-
subject.emit(Event.new("user_changed", {id: 12}))
|
15
|
-
expect(seen).to be_a(Event)
|
16
|
-
expect(seen.type).to eql("user_changed")
|
17
|
-
expect(seen.data.to_h).to eql({id: 12})
|
18
|
-
end
|
19
|
-
|
20
|
-
it 'allows mixin Symbol vs. String for event type' do
|
21
|
-
seen = nil
|
22
|
-
subject.listen(:user_changed) do |evt|
|
23
|
-
seen = evt
|
24
|
-
end
|
25
|
-
subject.emit(Event.new(:user_changed, {id: 12}))
|
26
|
-
expect(seen).to be_a(Event)
|
27
|
-
expect(seen.type).to eql("user_changed")
|
28
|
-
expect(seen.data.to_h).to eql({id: 12})
|
29
|
-
end
|
30
|
-
|
31
|
-
it 'does not raise errors synchronously' do
|
32
|
-
subject.listen("user_changed") do |evt|
|
33
|
-
raise "An error occured"
|
34
|
-
end
|
35
|
-
expect {
|
36
|
-
subject.emit(Event.new("user_changed", {id: 12}))
|
37
|
-
}.not_to raise_error
|
38
|
-
end
|
39
|
-
|
40
|
-
end
|
41
|
-
end
|
@@ -1,41 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
module Startback
|
3
|
-
describe Bus::Memory do
|
4
|
-
|
5
|
-
subject{
|
6
|
-
Bus::Memory::Sync.new
|
7
|
-
}
|
8
|
-
|
9
|
-
it 'allows emiting an receiving' do
|
10
|
-
seen = nil
|
11
|
-
subject.listen("user_changed") do |evt|
|
12
|
-
seen = evt
|
13
|
-
end
|
14
|
-
subject.emit(Event.new("user_changed", {id: 12}))
|
15
|
-
expect(seen).to be_a(Event)
|
16
|
-
expect(seen.type).to eql("user_changed")
|
17
|
-
expect(seen.data.to_h).to eql({id: 12})
|
18
|
-
end
|
19
|
-
|
20
|
-
it 'allows mixin Symbol vs. String for event type' do
|
21
|
-
seen = nil
|
22
|
-
subject.listen(:user_changed) do |evt|
|
23
|
-
seen = evt
|
24
|
-
end
|
25
|
-
subject.emit(Event.new(:user_changed, {id: 12}))
|
26
|
-
expect(seen).to be_a(Event)
|
27
|
-
expect(seen.type).to eql("user_changed")
|
28
|
-
expect(seen.data.to_h).to eql({id: 12})
|
29
|
-
end
|
30
|
-
|
31
|
-
it 'raises emit errors synchronously' do
|
32
|
-
subject.listen("user_changed") do |evt|
|
33
|
-
raise "An error occured"
|
34
|
-
end
|
35
|
-
expect {
|
36
|
-
subject.emit(Event.new("user_changed", {id: 12}))
|
37
|
-
}.to raise_error("An error occured")
|
38
|
-
end
|
39
|
-
|
40
|
-
end
|
41
|
-
end
|