nats_messaging 1.3.3 → 1.4.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/nats_messaging/nats_service.rb +34 -23
 - 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: 173adfe7dc254c451bc1528849cca140ab2e58fe8b70fc401e2cc99c63129291
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 319351fff7b6f01eca2f7749be32f4aaef3f85578b090665ac5cca9e8baebac7
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 44357756fdd1b5c61862f7f709a067cf7101f65bdc06ed8d46a480a7db5b8ff8685eb91b2c58cfb53aeba8eceaa3c08c661bd84e128e5873840ad70bea647af8
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: e0f968253ef4467574ef4ea607b36ee0b0fbe39c64a39b0807718fb4e564624dcb7edcbd4c0c340bc931d39300a570bcd89801145032350b47627fa766761ca0
         
     | 
| 
         @@ -12,46 +12,54 @@ module NatsMessaging 
     | 
|
| 
       12 
12 
     | 
    
         
             
                  @subscriptions = {} # Store active subscriptions
         
     | 
| 
       13 
13 
     | 
    
         
             
                end
         
     | 
| 
       14 
14 
     | 
    
         | 
| 
      
 15 
     | 
    
         
            +
                def log_duration(start_time, message_format, *args)
         
     | 
| 
      
 16 
     | 
    
         
            +
                  duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time
         
     | 
| 
      
 17 
     | 
    
         
            +
                  full_message = "NATS [%.2fs]: #{message_format}" % [duration, *args]
         
     | 
| 
      
 18 
     | 
    
         
            +
                  Rails.logger.info full_message
         
     | 
| 
      
 19 
     | 
    
         
            +
                end
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
       15 
21 
     | 
    
         
             
                # Create a stream
         
     | 
| 
       16 
22 
     | 
    
         
             
                def create_stream(stream_name, subjects)
         
     | 
| 
       17 
23 
     | 
    
         
             
                  begin
         
     | 
| 
      
 24 
     | 
    
         
            +
                    start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
         
     | 
| 
       18 
25 
     | 
    
         
             
                    stream = @js.stream_info(stream_name)
         
     | 
| 
       19 
     | 
    
         
            -
                     
     | 
| 
      
 26 
     | 
    
         
            +
                    log_duration(start_time, "Stream %s already exists", stream_name)
         
     | 
| 
       20 
27 
     | 
    
         
             
                  rescue NATS::JetStream::Error::NotFound
         
     | 
| 
       21 
28 
     | 
    
         
             
                    @js.add_stream(name: stream_name, subjects: subjects)
         
     | 
| 
       22 
     | 
    
         
            -
                     
     | 
| 
      
 29 
     | 
    
         
            +
                    log_duration(start_time, "Stream %s created", stream_name)
         
     | 
| 
       23 
30 
     | 
    
         
             
                  rescue => e
         
     | 
| 
       24 
     | 
    
         
            -
                     
     | 
| 
      
 31 
     | 
    
         
            +
                    log_duration(start_time, "Failed to create stream: %s", e.message)
         
     | 
| 
       25 
32 
     | 
    
         
             
                  end
         
     | 
| 
       26 
33 
     | 
    
         
             
                end
         
     | 
| 
       27 
34 
     | 
    
         | 
| 
       28 
35 
     | 
    
         
             
                # Publish a message to a subject using MessagePack
         
     | 
| 
       29 
36 
     | 
    
         
             
                def publish_message(subject, message)
         
     | 
| 
       30 
37 
     | 
    
         
             
                  packed_message = message.to_msgpack # Serializar a MessagePack
         
     | 
| 
       31 
     | 
    
         
            -
             
     | 
| 
       32 
38 
     | 
    
         
             
                  if @js
         
     | 
| 
       33 
39 
     | 
    
         
             
                    begin
         
     | 
| 
      
 40 
     | 
    
         
            +
                      start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
         
     | 
| 
       34 
41 
     | 
    
         
             
                      ack = @js.publish(subject, packed_message)
         
     | 
| 
       35 
     | 
    
         
            -
                       
     | 
| 
      
 42 
     | 
    
         
            +
                      log_duration(start_time, "Message sent to %s: %s, ACK: %s", subject, message, ack.inspect)
         
     | 
| 
       36 
43 
     | 
    
         
             
                    rescue NATS::JetStream::Error::NotFound
         
     | 
| 
       37 
44 
     | 
    
         
             
                      @nats.publish(subject, packed_message)
         
     | 
| 
       38 
     | 
    
         
            -
                       
     | 
| 
      
 45 
     | 
    
         
            +
                      log_duration(start_time, "Stream not found, falling back to regular NATS publish for subject %s", subject)
         
     | 
| 
       39 
46 
     | 
    
         
             
                    rescue => e
         
     | 
| 
       40 
     | 
    
         
            -
                       
     | 
| 
      
 47 
     | 
    
         
            +
                      log_duration(start_time, "Unexpected error while publishing to %s: %s", subject, e.message)
         
     | 
| 
       41 
48 
     | 
    
         
             
                    end
         
     | 
| 
       42 
49 
     | 
    
         
             
                  else
         
     | 
| 
       43 
50 
     | 
    
         
             
                    begin
         
     | 
| 
      
 51 
     | 
    
         
            +
                      start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
         
     | 
| 
       44 
52 
     | 
    
         
             
                      @nats.publish(subject, packed_message)
         
     | 
| 
       45 
     | 
    
         
            -
                       
     | 
| 
      
 53 
     | 
    
         
            +
                      log_duration(start_time, "Message sent to %s: %s", subject, message)
         
     | 
| 
       46 
54 
     | 
    
         
             
                    rescue => e
         
     | 
| 
       47 
     | 
    
         
            -
                       
     | 
| 
      
 55 
     | 
    
         
            +
                      log_duration(start_time, "Unexpected error: %s", e.message)
         
     | 
| 
       48 
56 
     | 
    
         
             
                    end
         
     | 
| 
       49 
57 
     | 
    
         
             
                  end
         
     | 
| 
       50 
58 
     | 
    
         
             
                end
         
     | 
| 
       51 
59 
     | 
    
         | 
| 
       52 
60 
     | 
    
         
             
                # Subscribe to a subject using MessagePack
         
     | 
| 
       53 
61 
     | 
    
         
             
                def subscribe_to_subject(subject, durable_name = "durable_name")
         
     | 
| 
       54 
     | 
    
         
            -
                   
     | 
| 
      
 62 
     | 
    
         
            +
                  Rails.logger.info "NATS: Subscribing to #{subject}"
         
     | 
| 
       55 
63 
     | 
    
         
             
                  if @js
         
     | 
| 
       56 
64 
     | 
    
         
             
                    @subscriptions[subject] = @js.subscribe(subject, durable: durable_name) do |msg|
         
     | 
| 
       57 
65 
     | 
    
         
             
                                    unpacked_data = process_received_message(msg, subject)
         
     | 
| 
         @@ -70,29 +78,31 @@ module NatsMessaging 
     | 
|
| 
       70 
78 
     | 
    
         
             
                # Process received message 
         
     | 
| 
       71 
79 
     | 
    
         
             
                def process_received_message(msg, subject)
         
     | 
| 
       72 
80 
     | 
    
         
             
                  begin
         
     | 
| 
      
 81 
     | 
    
         
            +
                    start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
         
     | 
| 
       73 
82 
     | 
    
         
             
                    unpacked_data = MessagePack.unpack(msg.data)
         
     | 
| 
       74 
     | 
    
         
            -
                     
     | 
| 
      
 83 
     | 
    
         
            +
                    log_duration(start_time, "Message received on %s: %s", subject, unpacked_data.inspect)
         
     | 
| 
       75 
84 
     | 
    
         
             
                    unpacked_data
         
     | 
| 
       76 
85 
     | 
    
         
             
                  rescue => e
         
     | 
| 
       77 
     | 
    
         
            -
                     
     | 
| 
      
 86 
     | 
    
         
            +
                    log_duration(start_time, "Error while processing message on %s: %s", subject, e.message)
         
     | 
| 
       78 
87 
     | 
    
         
             
                  end
         
     | 
| 
       79 
88 
     | 
    
         
             
                end
         
     | 
| 
       80 
89 
     | 
    
         | 
| 
       81 
90 
     | 
    
         
             
                # Send a request to a subject and wait for response using MessagePack
         
     | 
| 
       82 
91 
     | 
    
         
             
                def send_request(subject, message, timeout = 5)
         
     | 
| 
       83 
     | 
    
         
            -
                   
     | 
| 
      
 92 
     | 
    
         
            +
                  Rails.logger.info "NATS: Sending request to #{subject} with message: #{message}"
         
     | 
| 
      
 93 
     | 
    
         
            +
                  start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
         
     | 
| 
       84 
94 
     | 
    
         
             
                  begin
         
     | 
| 
       85 
95 
     | 
    
         
             
                    packed_message = message.to_msgpack # Serializar el mensaje
         
     | 
| 
       86 
96 
     | 
    
         
             
                    response = @nats.request(subject, packed_message, timeout: timeout) # Timeout of 5 seconds or custom
         
     | 
| 
       87 
97 
     | 
    
         
             
                    unpacked_response = MessagePack.unpack(response.data) # Deserializar respuesta
         
     | 
| 
       88 
     | 
    
         
            -
                     
     | 
| 
      
 98 
     | 
    
         
            +
                    log_duration(start_time, "Received reply: %s", unpacked_response.inspect)
         
     | 
| 
       89 
99 
     | 
    
         
             
                    yield(subject, unpacked_response) if block_given?
         
     | 
| 
       90 
100 
     | 
    
         
             
                    unpacked_response
         
     | 
| 
       91 
101 
     | 
    
         
             
                  rescue NATS::IO::Timeout
         
     | 
| 
       92 
     | 
    
         
            -
                     
     | 
| 
      
 102 
     | 
    
         
            +
                    log_duration(start_time, "Request timed out for subject: %s", subject)
         
     | 
| 
       93 
103 
     | 
    
         
             
                    nil
         
     | 
| 
       94 
104 
     | 
    
         
             
                  rescue => e
         
     | 
| 
       95 
     | 
    
         
            -
                     
     | 
| 
      
 105 
     | 
    
         
            +
                    log_duration(start_time, "Unexpected error for subject %s: %s", subject, e.message)
         
     | 
| 
       96 
106 
     | 
    
         
             
                    nil
         
     | 
| 
       97 
107 
     | 
    
         
             
                  end
         
     | 
| 
       98 
108 
     | 
    
         
             
                end
         
     | 
| 
         @@ -105,30 +115,31 @@ module NatsMessaging 
     | 
|
| 
       105 
115 
     | 
    
         
             
                  end
         
     | 
| 
       106 
116 
     | 
    
         
             
                  begin
         
     | 
| 
       107 
117 
     | 
    
         
             
                    # Create a new subscription
         
     | 
| 
       108 
     | 
    
         
            -
                     
     | 
| 
      
 118 
     | 
    
         
            +
                    Rails.logger.info "NATS: Listening on #{subject} with reply message: #{reply_message}"
         
     | 
| 
      
 119 
     | 
    
         
            +
                    start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) # En caso de que falle antes del segundo begin
         
     | 
| 
       109 
120 
     | 
    
         
             
                    @subscriptions[subject] = @nats.subscribe(subject) do |msg|
         
     | 
| 
       110 
121 
     | 
    
         
             
                      begin
         
     | 
| 
      
 122 
     | 
    
         
            +
                        start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
         
     | 
| 
       111 
123 
     | 
    
         
             
                        unpacked_data = MessagePack.unpack(msg.data) # Deserializar mensaje recibido
         
     | 
| 
       112 
     | 
    
         
            -
                         
     | 
| 
      
 124 
     | 
    
         
            +
                        Rails.logger.info "NATS: Received request on #{subject}: #{unpacked_data}"
         
     | 
| 
       113 
125 
     | 
    
         
             
                        yield_return = yield(subject, unpacked_data) if block_given?
         
     | 
| 
       114 
126 
     | 
    
         
             
                        reply = reply_message.nil? ? yield_return : reply_message
         
     | 
| 
       115 
127 
     | 
    
         
             
                        packed_reply = reply.to_msgpack # Serializar respuesta
         
     | 
| 
       116 
128 
     | 
    
         
             
                        # Asegurar que msg.reply existe antes de responder
         
     | 
| 
       117 
129 
     | 
    
         
             
                        if msg.reply
         
     | 
| 
       118 
130 
     | 
    
         
             
                          @nats.publish(msg.reply, packed_reply)
         
     | 
| 
       119 
     | 
    
         
            -
                           
     | 
| 
      
 131 
     | 
    
         
            +
                          log_duration(start_time, "Replied to %s with message: %s", subject, reply.inspect)
         
     | 
| 
       120 
132 
     | 
    
         
             
                        else
         
     | 
| 
       121 
     | 
    
         
            -
                           
     | 
| 
      
 133 
     | 
    
         
            +
                          log_duration(start_time, "No reply subject for %s, cannot respond", subject)
         
     | 
| 
       122 
134 
     | 
    
         
             
                        end
         
     | 
| 
       123 
135 
     | 
    
         
             
                        unpacked_data
         
     | 
| 
       124 
136 
     | 
    
         
             
                      rescue => e
         
     | 
| 
       125 
     | 
    
         
            -
                         
     | 
| 
      
 137 
     | 
    
         
            +
                        log_duration(start_time, "Error while processing message on %s: %s", subject, e.message)
         
     | 
| 
       126 
138 
     | 
    
         
             
                      end
         
     | 
| 
       127 
139 
     | 
    
         
             
                    end
         
     | 
| 
       128 
140 
     | 
    
         
             
                  rescue => e
         
     | 
| 
       129 
     | 
    
         
            -
                     
     | 
| 
      
 141 
     | 
    
         
            +
                    log_duration(start_time, "Error while replying: %s", e.message)
         
     | 
| 
       130 
142 
     | 
    
         
             
                  end
         
     | 
| 
       131 
143 
     | 
    
         
             
                end
         
     | 
| 
       132 
     | 
    
         
            -
                
         
     | 
| 
       133 
144 
     | 
    
         
             
              end
         
     | 
| 
       134 
145 
     | 
    
         
             
            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: 1. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 1.4.0
         
     | 
| 
       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-07- 
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2025-07-11 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
       13 
13 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
14 
     | 
    
         
             
              name: nats-pure
         
     |