class
NATS::Service
- NATS::Service
- Reference
- Object
Overview
The Service
represents a NATS service that was created by the Services
API. It exists to hang your endpoints on:
inventory = nats.services.add "inventory",
version: "0.1.0",
description: "Manage inventory"
inventory.add_endpoint "inventory.products.get", subject: "inventory.products.get.*" do |request|
_, _, _, id
nats.reply request, ProductQuery.new.find(id).to_json
end
Defined in:
services.crInstance Method Summary
-
#add_endpoint(name : String, *, subject : String = name, queue_group : String = "q", &block : Message -> ) : Endpoint
service.add_endpoint "endpoint-name", subject: "endpoint.subject" do |request| nats.reply request, "reply goes here" end
-
#add_group(name : String, &)
Add a
Group
and yield it to the block. -
#add_group(name : String)
Add a
Group
of endpoints. - #description : String
- #endpoints : Array(Endpoint)
- #id : String
- #metadata : Hash(String, String)
- #name : String
-
#on_error(&error_handler : ErrorHandler)
Execute the given block for all exceptions raised in this service's endpoints.
- #subscriptions : Array(Subscription)
- #version : String
Instance Method Detail
def add_endpoint(name : String, *, subject : String = name, queue_group : String = "q", &block : Message -> ) : Endpoint
#
service.add_endpoint "endpoint-name", subject: "endpoint.subject" do |request|
nats.reply request, "reply goes here"
end
def add_group(name : String, &)
#
Add a Group
and yield it to the block.
service.add_group "products" do |inventory|
inventory.add_endpoint "create" do |request|
nats.reply request, "reply goes here"
end
end
def on_error(&error_handler : ErrorHandler)
#
Execute the given block for all exceptions raised in this service's endpoints. This is useful for returning a common error response.
service.on_error do |exception, message|
nats.reply message, "", headers: NATS::Headers{
"Nats-Service-Error-Code" => "500",
"Nats-Service-Error" => "Unexpected error: #{exception.message}",
}
end