Class: NatsWorker::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/nats_worker/runner.rb

Overview

Runs one or more worker classes: opens a NATS connection, creates JetStream pull subscriptions and dispatches incoming messages to each worker’s #work method.

Instance Method Summary collapse

Constructor Details

#initialize(worker_classes, config: NatsWorker.configuration) ⇒ Runner

Returns a new instance of Runner.



8
9
10
11
12
13
14
15
# File 'lib/nats_worker/runner.rb', line 8

def initialize(worker_classes, config: NatsWorker.configuration)
  @worker_classes = Array(worker_classes)
  @config         = config
  @logger         = config.logger
  @threads        = []
  @stopping       = false
  @nats           = nil
end

Instance Method Details

#runObject

Raises:



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/nats_worker/runner.rb', line 17

def run
  raise NatsWorker::Error, "no workers to run" if @worker_classes.empty?

  install_signal_handlers
  connect!

  @worker_classes.each { |klass| start_worker(klass) }

  @logger.info("[nats_worker] running #{@worker_classes.size} worker(s); pid=#{Process.pid}")
  wait_for_shutdown
end

#stopObject



29
30
31
32
33
34
# File 'lib/nats_worker/runner.rb', line 29

def stop
  return if @stopping

  @stopping = true
  @logger.info("[nats_worker] shutting down...")
end