ruby_nest_nats 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +8 -0
- data/lib/ruby_nest_nats.rb +52 -14
- data/lib/ruby_nest_nats/version.rb +1 -1
- 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: 0dbcc88e2386b8f1b6408688c8fafba196037fb70e089a6f51fd4fca2f4b06a1
|
4
|
+
data.tar.gz: 96b78dccc0a509f21bd1ed5573940e57b76a51df21e20b6a95fd2f145b067714
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 06db896c2a6e94bac268624a7e75e9e6fa203bc6c6955a6893f661b11c5249faf0b97c397ebc9b646b395b781597b8d4b5cf2d0718ee233014bb62f3ccc27d9f
|
7
|
+
data.tar.gz: 0d5f123eadb77873eeed442162f72b20693696a9f1a57b9759edac7df74ab147df48a8ebe16a7d4f124a897bddc303941f84cac294457a08fcc3bf8ef3199b5f
|
data/README.md
CHANGED
@@ -4,6 +4,14 @@ Welcome to your new gem! In this directory, you'll find the files you need to be
|
|
4
4
|
|
5
5
|
TODO: Delete this and the text above, and describe your gem
|
6
6
|
|
7
|
+
## TODO
|
8
|
+
|
9
|
+
- [ ] docs
|
10
|
+
- [ ] tests
|
11
|
+
- [ ] multiple queues
|
12
|
+
- [ ] `on_error` handler so you can send a response (what's standard?)
|
13
|
+
- [ ] config for restart behavior (default is to restart listening on any `StandardError`)
|
14
|
+
|
7
15
|
## Installation
|
8
16
|
|
9
17
|
Add this line to your application's Gemfile:
|
data/lib/ruby_nest_nats.rb
CHANGED
@@ -8,6 +8,18 @@ module RubyNestNats
|
|
8
8
|
|
9
9
|
class Client
|
10
10
|
class << self
|
11
|
+
def logger=(some_logger)
|
12
|
+
@logger = some_logger
|
13
|
+
end
|
14
|
+
|
15
|
+
def logger
|
16
|
+
@logger
|
17
|
+
end
|
18
|
+
|
19
|
+
def log(text)
|
20
|
+
logger.info("RubyNestNats | #{text}") if logger
|
21
|
+
end
|
22
|
+
|
11
23
|
def queue=(some_queue)
|
12
24
|
@queue = some_queue.to_s
|
13
25
|
end
|
@@ -21,38 +33,64 @@ module RubyNestNats
|
|
21
33
|
end
|
22
34
|
|
23
35
|
def started?
|
24
|
-
|
36
|
+
@started ||= false
|
25
37
|
end
|
26
38
|
|
27
|
-
def reply_to(raw_subject,
|
39
|
+
def reply_to(raw_subject, &block)
|
28
40
|
subject = raw_subject.to_s
|
29
41
|
|
30
42
|
if started?
|
31
43
|
raise StandardError, "NATS already started"
|
32
|
-
elsif !
|
33
|
-
raise ArgumentError, "
|
44
|
+
elsif !block_given?
|
45
|
+
raise ArgumentError, "Response block must be provided"
|
34
46
|
elsif replies.any? { |reply| reply[:subject] == subject }
|
35
47
|
raise ArgumentError, "Already registered a reply to #{subject}"
|
36
48
|
end
|
37
49
|
|
38
|
-
|
50
|
+
log("Registering a reply handler for subject '#{subject}'#{" in queue '#{queue}'" if queue}")
|
51
|
+
replies << { subject: subject, handler: block, queue: queue }
|
52
|
+
end
|
53
|
+
|
54
|
+
def listen
|
55
|
+
NATS.start do
|
56
|
+
replies.each do |replier|
|
57
|
+
NATS.subscribe(replier[:subject], queue: replier[:queue]) do |message, inbox, subject|
|
58
|
+
log("Received the message '#{message}' for subject '#{subject}' with reply inbox '#{inbox}'")
|
59
|
+
response = replier[:handler].call(JSON.parse(message)["data"])
|
60
|
+
log("Responding with '#{response}'")
|
61
|
+
NATS.publish(inbox, response.to_json, queue: replier[:queue])
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def stop!
|
68
|
+
log("Stopping NATS")
|
69
|
+
NATS.stop
|
70
|
+
@started = false
|
71
|
+
end
|
72
|
+
|
73
|
+
def restart!
|
74
|
+
log("Restarting NATS...")
|
75
|
+
stop!
|
76
|
+
start!
|
39
77
|
end
|
40
78
|
|
41
79
|
def start!
|
80
|
+
log("Starting NATS")
|
81
|
+
return log("NATS is already running") if started?
|
82
|
+
|
42
83
|
@started = true
|
43
84
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
end
|
85
|
+
Thread.new do
|
86
|
+
Thread.handle_interrupt(StandardError => :never) do
|
87
|
+
begin
|
88
|
+
Thread.handle_interrupt(StandardError => :immediate) { listen }
|
89
|
+
ensure
|
90
|
+
restart!
|
51
91
|
end
|
52
92
|
end
|
53
93
|
end
|
54
|
-
|
55
|
-
fiber.resume
|
56
94
|
end
|
57
95
|
end
|
58
96
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_nest_nats
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Keegan Leitz
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-05-
|
11
|
+
date: 2021-05-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|