Module: Rubyists::Leopard::NatsApiServer::ClassMethods

Includes:
MetricsServer
Defined in:
lib/leopard/nats_api_server.rb

Instance Method Summary collapse

Instance Method Details

#endpoint(name, subject: nil, queue: nil, group: nil, &handler) ⇒ void

This method returns an undefined value.

Define an endpoint for the NATS API server.

Parameters:

  • name (String)

    The name of the endpoint.

  • subject (String, nil) (defaults to: nil)

    The NATS subject to listen on. Defaults to the endpoint name.

  • queue (String, nil) (defaults to: nil)

    The NATS queue group to use. Defaults to nil.

  • group (String, nil) (defaults to: nil)

    The group this endpoint belongs to. Defaults to nil.

  • handler (Proc)

    The block that will handle incoming messages.



43
44
45
# File 'lib/leopard/nats_api_server.rb', line 43

def endpoint(name, subject: nil, queue: nil, group: nil, &handler)
  endpoints << Endpoint.new(name:, subject: subject || name, queue:, group:, handler:)
end

#endpointsObject



30
# File 'lib/leopard/nats_api_server.rb', line 30

def endpoints = @endpoints ||= []

#group(name, group: nil, queue: nil) ⇒ void

This method returns an undefined value.

Define a group for organizing endpoints.

Parameters:

  • name (String)

    The name of the group.

  • group (String, nil) (defaults to: nil)

    The parent group this group belongs to. Defaults to nil.

  • queue (String, nil) (defaults to: nil)

    The NATS queue group to use for this group. Defaults to nil.



54
55
56
# File 'lib/leopard/nats_api_server.rb', line 54

def group(name, group: nil, queue: nil)
  groups[name] = { name:, parent: group, queue: }
end

#groupsObject



31
# File 'lib/leopard/nats_api_server.rb', line 31

def groups = @groups ||= {}

#middlewareObject



32
# File 'lib/leopard/nats_api_server.rb', line 32

def middleware = @middleware ||= []

#run(nats_url:, service_opts:, instances: 1, blocking: true) ⇒ void

This method returns an undefined value.

Start the NATS API server. This method connects to the NATS server and spawns multiple instances of the API server.

Parameters:

  • nats_url (String)

    The URL of the NATS server to connect to.

  • service_opts (Hash)

    Options for the NATS service.

  • instances (Integer) (defaults to: 1)

    The number of instances to spawn. Defaults to 1.

  • blocking (Boolean) (defaults to: true)

    If false, does not block current thread after starting the server. Defaults to true.



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/leopard/nats_api_server.rb', line 78

def run(nats_url:, service_opts:, instances: 1, blocking: true)
  logger.info 'Booting NATS API server...'
  workers = Concurrent::Array.new
  pool = spawn_instances(nats_url, service_opts, instances, workers, blocking)
  logger.info 'Setting up signal trap...'
  trap_signals(workers, pool)
  start_metrics_server(workers) if ENV['LEOPARD_METRICS_PORT']
  return pool unless blocking

  sleep
end

#use(klass, *args, &block) ⇒ void

This method returns an undefined value.

Use a middleware class for processing messages.

Parameters:

  • klass (Class)

    The middleware class to use.

  • args (Array)

    Optional arguments to pass to the middleware class.

  • block (Proc)

    Optional block to pass to the middleware class.



65
66
67
# File 'lib/leopard/nats_api_server.rb', line 65

def use(klass, *args, &block)
  middleware << [klass, args, block]
end