Module: NatsWorker::Worker

Defined in:
lib/nats_worker/worker.rb

Overview

Mix in to make a class a NATS JetStream worker.

Example:

class OrderWorker
  include NatsWorker::Worker

  from_stream "ORDERS",
    subject:  "orders.>",
    durable:  "order_worker",
    threads:  2,
    fetch:    10,
    ack_wait: 30

  def work(msg)
    payload = JSON.parse(msg.data)
    # ... do something ...
  end
end

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



21
22
23
24
# File 'lib/nats_worker/worker.rb', line 21

def self.included(base)
  base.extend(ClassMethods)
  NatsWorker.register(base)
end

Instance Method Details

#ack!(msg) ⇒ Object

The framework auto-acks on success and nacks on exception, but workers may call these explicitly if they want finer control.



28
# File 'lib/nats_worker/worker.rb', line 28

def ack!(msg);  msg.ack;  end

#loggerObject



32
33
34
# File 'lib/nats_worker/worker.rb', line 32

def logger
  NatsWorker.logger
end

#nack!(msg) ⇒ Object



29
# File 'lib/nats_worker/worker.rb', line 29

def nack!(msg); msg.nak;  end

#term!(msg) ⇒ Object



30
# File 'lib/nats_worker/worker.rb', line 30

def term!(msg); msg.term; end