class Eventboss::Worker

Worker is part of a pool of workers, handles UnitOfWork lifecycle

Attributes

id[R]

Public Class Methods

new(launcher, id, bus, restart_on: [Exception]) click to toggle source
# File lib/eventboss/worker.rb, line 9
def initialize(launcher, id, bus, restart_on: [Exception])
  @id = id
  @launcher = launcher
  @bus = bus
  @thread = nil
  @restart_on = restart_on
end

Public Instance Methods

kill(wait = false) click to toggle source
# File lib/eventboss/worker.rb, line 47
def kill(wait = false)
  return unless @thread
  @thread.raise Eventboss::Shutdown
  @thread.value if wait
end
run() click to toggle source
# File lib/eventboss/worker.rb, line 21
def run
  while (work = @bus.pop)
    run_work(work)
  end
  @launcher.worker_stopped(self)
rescue Eventboss::Shutdown
  @launcher.worker_stopped(self)
rescue *@restart_on => exception
  handle_exception(exception, worker_id: id)
  # Restart the worker in case of hard exception
  # Message won't be delete from SQS and will be visible later
  @launcher.worker_stopped(self, restart: true)
end
run_work(work) click to toggle source
# File lib/eventboss/worker.rb, line 35
def run_work(work)
  server_middleware.invoke(work) do
    work.run
  end
end
start() click to toggle source
# File lib/eventboss/worker.rb, line 17
def start
  @thread = safe_thread(id, &method(:run))
end
terminate(wait = false) click to toggle source
# File lib/eventboss/worker.rb, line 41
def terminate(wait = false)
  stop_token
  return unless @thread
  @thread.value if wait
end

Private Instance Methods

server_middleware() click to toggle source
# File lib/eventboss/worker.rb, line 60
def server_middleware
  Eventboss.configuration.server_middleware
end
stop_token() click to toggle source

stops the loop, by enqueuing falsey value

# File lib/eventboss/worker.rb, line 56
def stop_token
  @bus << nil
end