Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions examples/echo_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,
)
Expand Down
6 changes: 4 additions & 2 deletions lib/leopard/message_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -26,6 +27,7 @@ def initialize(nats_msg)
#
# @return [void]
def respond(payload)
raw.header = headers unless headers.empty?
raw.respond(serialize(payload))
end

Expand Down
15 changes: 9 additions & 6 deletions lib/leopard/nats_api_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion test/lib/message_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down