tiny_bus 3.1.0 → 3.2.0
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/tiny_bus.rb +32 -12
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e0b4ee66f65bb60557ba29b72f0ade0e840f0db352ac2f1bca0cab5385b97b19
|
4
|
+
data.tar.gz: '089bfc105f34f3538844e5756e3623f8db4f835c58501140cc6d6a02521c17dc'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 58a576c5b7bc7445b263e93b23ba1ba65e6536dfdd38dfe150251401dfc9490776ca6b751ef27c9ed71ce2bea0901d5292b009bed217c7c0a50c3c852f88e748
|
7
|
+
data.tar.gz: 45a52d67eecda3efa9d8e72267413fd0d5adaab5b5f3168278e7938e0edd88f9f29b6e18fb5a98d7058c0a456c3898b83a92549cf7df7a012313bc27b1f8b736
|
data/lib/tiny_bus.rb
CHANGED
@@ -6,7 +6,7 @@ require 'tiny_pipe'
|
|
6
6
|
|
7
7
|
# NOTE: This library depends on the TinyLog library.
|
8
8
|
#
|
9
|
-
# This class implements a very
|
9
|
+
# This class implements a very simple PubSub system where:
|
10
10
|
# - subscribers can subscribe via the #sub method
|
11
11
|
# - subscribers can unsubscribe via the #unsub method
|
12
12
|
# - msgs can enter the TinyBus via the #msg method
|
@@ -28,6 +28,10 @@ require 'tiny_pipe'
|
|
28
28
|
# TinyBus.new(raise_on_dead: true) # strict mode for undeliverable messages, defaults to false
|
29
29
|
class TinyBus
|
30
30
|
ANNOTATION_PREFIX_DEFAULT = '.'
|
31
|
+
LOGGING_LEVELS = {
|
32
|
+
'sent' => 'SENT',
|
33
|
+
'dead' => 'DEAD'
|
34
|
+
}.freeze
|
31
35
|
|
32
36
|
# log:
|
33
37
|
# if specified, it should be a TinyLog instance
|
@@ -52,20 +56,36 @@ class TinyBus
|
|
52
56
|
# default: '.'
|
53
57
|
# if specified, the annotated message attributes ('.time', '.msg_uuid', and
|
54
58
|
# '.trace') will have the dot ('.') replaced with the specified prefix text
|
59
|
+
# logging_overrides:
|
60
|
+
# default: LOGGING_LEVELS
|
61
|
+
# allows you to specify a Hash of overrides of the logging levels for
|
62
|
+
# 'SENT' and 'DEAD' message delivery; useful if the consuming application
|
63
|
+
# wants to use its own logging levels, or you just want the labels changed
|
64
|
+
# for some reason
|
55
65
|
def initialize(log: nil, dead: nil, translator: nil, raise_on_dead: false,
|
56
|
-
annotation_prefix: ANNOTATION_PREFIX_DEFAULT
|
66
|
+
annotation_prefix: ANNOTATION_PREFIX_DEFAULT,
|
67
|
+
logging_overrides: LOGGING_LEVELS)
|
57
68
|
@subs = {}
|
58
69
|
@translator = translator
|
70
|
+
|
71
|
+
@dead_key = "#{annotation_prefix}dead"
|
72
|
+
@topic_key = "#{annotation_prefix}topic"
|
73
|
+
@time_key = "#{annotation_prefix}time"
|
74
|
+
@msg_uuid_key = "#{annotation_prefix}msg_uuid"
|
75
|
+
@trace_key = "#{annotation_prefix}trace"
|
76
|
+
|
59
77
|
@annotator = TinyPipe.new([
|
60
|
-
->(m){ m[
|
61
|
-
->(m){ m[
|
62
|
-
->(m){ m[
|
78
|
+
->(m){ m[@time_key] = (Time.now.to_f * 1000).to_i; m },
|
79
|
+
->(m){ m[@msg_uuid_key] = SecureRandom.uuid; m },
|
80
|
+
->(m){ m[@trace_key] ||= SecureRandom.uuid; m }
|
63
81
|
])
|
64
82
|
|
65
83
|
@stats = { '.dead' => 0 }
|
66
84
|
@log = log || TinyLog.new($stdout)
|
67
85
|
@dead = dead || TinyLog.new($stderr)
|
68
86
|
@raise_on_dead = raise_on_dead
|
87
|
+
@sent_level = logging_overrides['sent'] || LOGGING_LEVELS['sent']
|
88
|
+
@dead_level = logging_overrides['dead'] || LOGGING_LEVELS['dead']
|
69
89
|
end
|
70
90
|
|
71
91
|
# adds a subscriber to a topic
|
@@ -76,14 +96,14 @@ class TinyBus
|
|
76
96
|
@subs[topic] << subber
|
77
97
|
@stats[topic] ||= 0
|
78
98
|
|
79
|
-
msg({
|
99
|
+
msg({ @topic_key => 'sub', 'to_topic' => topic, 'subber' => subber.to_s })
|
80
100
|
end
|
81
101
|
|
82
102
|
# removes a subscriber from a topic
|
83
103
|
def unsub(topic, subber)
|
84
104
|
@subs[topic]&.delete(subber)
|
85
105
|
|
86
|
-
msg({
|
106
|
+
msg({ @topic_key => 'unsub', 'from_topic' => topic, 'subber' => subber.to_s })
|
87
107
|
end
|
88
108
|
|
89
109
|
# takes an incoming message and distributes it to subscribers
|
@@ -101,20 +121,20 @@ class TinyBus
|
|
101
121
|
msg = @annotator.pipe(msg)
|
102
122
|
msg = @translator&.pipe(msg) || msg
|
103
123
|
|
104
|
-
topic = msg[
|
124
|
+
topic = msg[@topic_key]
|
105
125
|
|
106
126
|
subbers = @subs[topic]
|
107
127
|
|
108
|
-
if subbers
|
128
|
+
if (subbers&.length || 0) > 0
|
109
129
|
@stats[topic] += 1
|
110
130
|
subbers.each{|s| s.msg(msg) }
|
111
|
-
@log.
|
131
|
+
@log.send(@sent_level, msg)
|
112
132
|
else
|
113
133
|
if @raise_on_dead
|
114
134
|
raise TinyBus::DeadMsgError.new("Could not deliver message to topic `#{topic}'")
|
115
135
|
else
|
116
|
-
@stats[
|
117
|
-
@dead.
|
136
|
+
@stats[@dead_key] += 1
|
137
|
+
@dead.send(@dead_level, msg)
|
118
138
|
end
|
119
139
|
end
|
120
140
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tiny_bus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeff Lunt
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-12-
|
11
|
+
date: 2022-12-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: tiny_log
|