diff --git a/Gemfile.lock b/Gemfile.lock index 104afbd..96ee7e6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - leopard (0.1.6) + leopard (0.1.7) concurrent-ruby (~> 1.1) dry-configurable (~> 1.3) dry-monads (~> 1.9) @@ -28,7 +28,7 @@ GEM dry-core (~> 1.1) zeitwerk (~> 2.6) io-console (0.8.1) - json (2.13.1) + json (2.13.2) language_server-protocol (3.17.0.5) lint_roller (1.1.0) logger (1.7.0) @@ -53,10 +53,10 @@ GEM racc (1.8.1) rainbow (3.1.1) rake (13.3.0) - regexp_parser (2.10.0) + regexp_parser (2.11.0) reline (0.6.2) io-console (~> 0.5) - rubocop (1.79.0) + rubocop (1.79.2) json (~> 2.3) language_server-protocol (~> 3.17.0.2) lint_roller (~> 1.1.0) @@ -66,7 +66,6 @@ GEM regexp_parser (>= 2.9.3, < 3.0) rubocop-ast (>= 1.46.0, < 2.0) ruby-progressbar (~> 1.7) - tsort (>= 0.2.0) unicode-display_width (>= 2.4.0, < 4.0) rubocop-ast (1.46.0) parser (>= 3.3.7.2) @@ -92,7 +91,6 @@ GEM simplecov_json_formatter (~> 0.1) simplecov-html (0.13.2) simplecov_json_formatter (0.1.4) - tsort (0.2.0) unicode-display_width (3.1.4) unicode-emoji (~> 4.0, >= 4.0.4) unicode-emoji (4.0.4) diff --git a/examples/echo_endpoint.rb b/examples/echo_endpoint.rb index 9774389..8d605ce 100755 --- a/examples/echo_endpoint.rb +++ b/examples/echo_endpoint.rb @@ -7,7 +7,7 @@ class EchoService include Rubyists::Leopard::NatsApiServer - def initialize(a_var = 1) + def initialize(a_var: 1) logger.info "EchoService initialized with a_var: #{a_var}" end @@ -21,7 +21,7 @@ def initialize(a_var = 1) service_opts: { name: 'example.echo', version: '1.0.0', - instance_args: [2], + instance_args: { a_var: 2 }, }, instances: 1, ) diff --git a/lib/leopard/message_wrapper.rb b/lib/leopard/message_wrapper.rb index e1638ff..3f1279b 100644 --- a/lib/leopard/message_wrapper.rb +++ b/lib/leopard/message_wrapper.rb @@ -10,10 +10,11 @@ class MessageWrapper # # @!attribute [r] data # @return [Object] The parsed data from the NATS message. + attr_reader :raw, :data # - # @!attribute [r] headers + # @!attribute [w] headers # @return [Hash] The headers from the NATS message. - attr_reader :raw, :data, :headers + attr_accessor :headers # @param nats_msg [NATS::Message] The NATS message to wrap. def initialize(nats_msg) @@ -26,6 +27,7 @@ def initialize(nats_msg) # # @return [void] def respond(payload) + raw.header = headers unless headers.empty? raw.respond(serialize(payload)) end diff --git a/lib/leopard/nats_api_server.rb b/lib/leopard/nats_api_server.rb index 582ccdc..b165dd1 100644 --- a/lib/leopard/nats_api_server.rb +++ b/lib/leopard/nats_api_server.rb @@ -98,6 +98,8 @@ def spawn_instances(url, opts, count, workers, blocking) pool = Concurrent::FixedThreadPool.new(count) @instance_args = opts.delete(:instance_args) || nil logger.info "Building #{count} workers with options: #{opts.inspect}, instance_args: #{@instance_args}" + raise ArgumentError, 'instance_args must be a Hash' if @instance_args && !@instance_args.is_a?(Hash) + count.times do pool.post { build_worker(url, opts, workers, blocking) } end @@ -106,18 +108,19 @@ def spawn_instances(url, opts, count, workers, blocking) # Builds a worker instance and sets it up with the NATS server. # - # @param url [String] The URL of the NATS server. - # @param opts [Hash] Options for the NATS service. + # @param nats_url [String] The URL of the NATS server. + # @param service_opts [Hash] Options for the NATS service. # @param workers [Array] The array to store worker instances. # @param blocking [Boolean] If true, blocks the current thread until the worker is set up. # # @return [void] - def build_worker(url, opts, workers, blocking) - worker = @instance_args ? new(*@instance_args) : new + def build_worker(nats_url, service_opts, workers, blocking) + worker = @instance_args ? new(**@instance_args) : new workers << worker - return worker.setup_worker!(nats_url: url, service_opts: opts) if blocking + args = { nats_url:, service_opts: } + return worker.setup_worker!(**args) if blocking - worker.setup_worker(nats_url: url, service_opts: opts) + worker.setup_worker(**args) end # Shuts down the NATS API server gracefully. diff --git a/test/lib/message_wrapper.rb b/test/lib/message_wrapper.rb index d4c85c9..7c94d88 100644 --- a/test/lib/message_wrapper.rb +++ b/test/lib/message_wrapper.rb @@ -4,7 +4,8 @@ require 'leopard/message_wrapper' class FakeMsg - attr_reader :data, :header, :responded_payload, :error_args + attr_reader :data, :responded_payload, :error_args + attr_accessor :header def initialize(data, header = {}) @data = data