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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 655b85cbd8f941a46595d30adde13826cf69e8978a829653596aaa1ebd4d6437
4
- data.tar.gz: 1d72decb7def437e62a6302d1885ef18e66940285ccd10abc3bb34b34e44bbd3
3
+ metadata.gz: 0dbcc88e2386b8f1b6408688c8fafba196037fb70e089a6f51fd4fca2f4b06a1
4
+ data.tar.gz: 96b78dccc0a509f21bd1ed5573940e57b76a51df21e20b6a95fd2f145b067714
5
5
  SHA512:
6
- metadata.gz: 17de52f910afb1eb74da267e29f80cbcfdfb0e2f929c4cc8912b4d7dcb8c445c15a063eb30c11a74b2504ec51196a1af72f141b05a9f96ada6103e8cb519c617
7
- data.tar.gz: ffb654a55f20b835fb7e516af5425c192627146a3be9e2fb61cb07f4de79357f7ee6f7eeb2a1f4a36642fb719270ec30005eef39fe2961b65813157c26d49b4d
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:
@@ -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
- !!@started
36
+ @started ||= false
25
37
  end
26
38
 
27
- def reply_to(raw_subject, with:)
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 !with.respond_to?(:call)
33
- raise ArgumentError, "Option `:with` must be callable"
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
- replies << { subject: subject, handler: with, queue: queue }
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
- fiber = Fiber.new do
45
- NATS.start do
46
- replies.each do |replier|
47
- NATS.subscribe(replier[:subject], queue: replier[:queue]) do |message, reply, _subject|
48
- response = replier[:handler].call(JSON.parse(message)["data"])
49
- NATS.publish(reply, response.to_json, queue: reply[:queue])
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyNestNats
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  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.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 00:00:00.000000000 Z
11
+ date: 2021-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler