nats_messaging 0.1.6 → 0.1.7
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/nats_messaging/nats_service.rb +20 -41
- 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: a3e8eef7b8b10d8b83e5ce8a0236def7eb58d9d99325e2c2d64871554c470c0c
|
4
|
+
data.tar.gz: 443547eff24fbbf7381e0bc9234bc56531e97bc10052b227d3372cc0f4ae7825
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bfd3f40b4eb214e167bf528718a8dbc8cb0c8d29e3ccb2281d6152e486a8f8dff9a3ecd67d42f7eeab4605b5947f77318f039cfb63ece5bca9f31d0e4e20773a
|
7
|
+
data.tar.gz: c3e5d781c9941f53c66185cb5738d66a4707bc42ef105896c09239ba4f058129cb17b4d38539774847773753eadecb71bd80a3850e4de0d506a343905862fa96
|
@@ -14,15 +14,12 @@ module NatsMessaging
|
|
14
14
|
def create_stream(stream_name)
|
15
15
|
begin
|
16
16
|
stream = @js.stream_info(stream_name)
|
17
|
-
|
18
|
-
puts "Stream #{stream_name} already exists"
|
17
|
+
puts "NATS: Stream #{stream_name} already exists"
|
19
18
|
rescue NATS::JetStream::Error::NotFound
|
20
19
|
@js.add_stream(name: stream_name, subjects: ["subject1", "subject2"])
|
21
|
-
|
22
|
-
puts "Stream #{stream_name} created"
|
20
|
+
puts "NATS: Stream #{stream_name} created"
|
23
21
|
rescue => e
|
24
|
-
|
25
|
-
puts "Failed to create stream: #{e.message}"
|
22
|
+
puts "NATS: Failed to create stream: #{e.message}"
|
26
23
|
end
|
27
24
|
end
|
28
25
|
|
@@ -31,44 +28,41 @@ module NatsMessaging
|
|
31
28
|
begin
|
32
29
|
packed_message = message.to_msgpack # Serializar a MessagePack
|
33
30
|
ack = @js.publish(subject, packed_message)
|
34
|
-
|
31
|
+
puts "NATS: Message sent to #{subject}: #{message}, ACK: #{ack.inspect}"
|
35
32
|
rescue => e
|
36
|
-
|
33
|
+
puts "NATS: Unexpected error: #{e.message}"
|
37
34
|
end
|
38
35
|
end
|
39
36
|
|
40
37
|
# Subscribe to a subject using MessagePack
|
41
38
|
def subscribe_to_subject(subject, durable_name = "durable_name")
|
42
|
-
puts "Subscribing to #{subject}
|
39
|
+
puts "NATS: Subscribing to #{subject}"
|
43
40
|
@js.subscribe(subject, durable: durable_name) do |msg|
|
44
41
|
begin
|
45
42
|
unpacked_data = MessagePack.unpack(msg.data) # Deserializar el mensaje recibido
|
46
|
-
puts "Message received on #{subject}: #{unpacked_data}"
|
47
|
-
Rails.logger.info "Message received on #{subject}: #{unpacked_data} keyword"
|
43
|
+
puts "NATS: Message received on #{subject}: #{unpacked_data}"
|
48
44
|
msg.ack
|
49
45
|
unpacked_data
|
50
46
|
rescue => e
|
51
|
-
|
52
|
-
puts "Error while processing message: #{e.message}"
|
47
|
+
puts "NATS: Error while processing message: #{e.message}"
|
53
48
|
end
|
54
49
|
end
|
55
50
|
end
|
56
51
|
|
57
52
|
# Send a request to a subject and wait for response using MessagePack
|
58
53
|
def send_request(subject, message)
|
59
|
-
|
60
|
-
puts "Sending request to #{subject} with message: #{message} puts"
|
54
|
+
puts "NATS: Sending request to #{subject} with message: #{message}"
|
61
55
|
begin
|
62
56
|
packed_message = message.to_msgpack # Serializar el mensaje
|
63
57
|
response = @nats.request(subject, packed_message, timeout: 5) # Timeout of 5 seconds
|
64
58
|
unpacked_response = MessagePack.unpack(response.data) # Deserializar respuesta
|
65
|
-
|
59
|
+
puts "NATS: Received reply: #{unpacked_response}"
|
66
60
|
unpacked_response
|
67
61
|
rescue NATS::IO::Timeout
|
68
|
-
|
62
|
+
puts "NATS: Request timed out for subject: #{subject}"
|
69
63
|
nil
|
70
64
|
rescue => e
|
71
|
-
|
65
|
+
puts "NATS: Unexpected error: #{e.message}"
|
72
66
|
nil
|
73
67
|
end
|
74
68
|
end
|
@@ -76,51 +70,36 @@ module NatsMessaging
|
|
76
70
|
# Listen to a subject and reply with a message using MessagePack
|
77
71
|
def listen_and_reply(subject, reply_message)
|
78
72
|
# Cancel active subscription if it already exists for this subject
|
79
|
-
Rails.logger.info "keyword listen and reply rails"
|
80
|
-
puts "keyword listen and reply puts"
|
81
73
|
if @subscriptions[subject]
|
82
74
|
@subscriptions[subject].unsubscribe
|
83
|
-
Rails.logger.info "Unsubscribed from #{subject}"
|
84
|
-
puts "Unsubscribed from #{subject}"
|
85
75
|
end
|
86
|
-
Rails.logger.info "keyword #{@subscriptions[subject].inspect}"
|
87
|
-
puts "keyword #{@subscriptions[subject].inspect}"
|
88
|
-
|
89
76
|
begin
|
90
77
|
# Create a new subscription
|
91
|
-
|
92
|
-
puts "Suscribing a #{subject}"
|
78
|
+
puts "NATS: Suscribing to #{subject}"
|
93
79
|
subscription = @nats.subscribe(subject) do |msg|
|
94
80
|
begin
|
95
81
|
received_data = MessagePack.unpack(msg.data) # Deserializar mensaje recibido
|
96
|
-
|
97
|
-
puts "Received request on #{subject}: #{received_data}"
|
82
|
+
puts "NATS: Received request on #{subject}: #{received_data}"
|
98
83
|
packed_reply = reply_message.to_msgpack # Serializar respuesta
|
99
84
|
# Asegurar que msg.reply existe antes de responder
|
100
85
|
if msg.reply
|
101
86
|
@nats.publish(msg.reply, packed_reply)
|
102
|
-
|
103
|
-
puts "Replied to #{subject} with message: #{reply_message} new"
|
87
|
+
puts "NATS: Replied to #{subject} with message: #{reply_message} new"
|
104
88
|
else
|
105
|
-
|
106
|
-
puts "No reply subject for #{subject}, cannot respond"
|
89
|
+
puts "NATS: No reply subject for #{subject}, cannot respond"
|
107
90
|
end
|
108
91
|
rescue => e
|
109
|
-
|
110
|
-
puts "Error while processing message: #{e.message}"
|
92
|
+
puts "NATS: Error while processing message: #{e.message}"
|
111
93
|
end
|
112
94
|
end
|
113
95
|
rescue => e
|
114
|
-
|
115
|
-
puts "Error while replying: #{e.message}"
|
96
|
+
puts "NATS: Error while replying: #{e.message}"
|
116
97
|
end
|
117
98
|
|
118
99
|
# Store the new subscription in the hash
|
119
100
|
@subscriptions[subject] = subscription
|
120
|
-
|
121
|
-
puts "
|
122
|
-
Rails.logger.info "Listening on #{subject} with reply message: #{reply_message}"
|
123
|
-
puts "Listening on #{subject} with reply message: #{reply_message}"
|
101
|
+
puts "NATS: Stored subscription for #{subject}: #{@subscriptions[subject].inspect}"
|
102
|
+
puts "NATS: Listening on #{subject} with reply message: #{reply_message}"
|
124
103
|
end
|
125
104
|
end
|
126
105
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nats_messaging
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bea Graboloza
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-01-
|
11
|
+
date: 2025-01-31 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A service to send and receive messages on a Rails application using NATS
|
14
14
|
email: beatriz.graboloza@bpo-advisors.net
|