Running Sneakers workers on Kubernetes

Allow Sneakers to receive SIGTERM

Sneakers has good graceful shutdown handling built-in. When it receives SIGTERM, Sneakers will:

  1. Stop consuming the queue
  2. Finish any work that’s in progress on messages that have already been pulled from the queue.
  3. Terminate
command: ["bundle", "exec", "rake", "sneakers:run"]
command: ["/bin/sh", "-c", "WORKERS=MyWorker rake sneakers:run"]
env:
- name: WORKERS
value: MyWorker

Increase the grace period

The grace period is how long Kube will wait for the process on PID 1 to finish before sending SIGKILL (immediate termination).

spec:
template:
spec:
terminationGracePeriodSeconds: 180

Application level shutdown handling

You may want your Sneakers worker to do something special when the SIGTERM is received.

class MyWorker
include Sneakers::Worker
from_queue :my_queue,
durable: true,
amqp: ENV['AMQP_CONNECTION'],
exchange: 'domain_events',
exchange_options: { type: :topic },
prefetch: 100,
threads: 1,
routing_key: [:EVENT_HAPPENED]
def work(message)
@counter += 1

return ack! unless @counter >= 10_000

Updater.increment(@counter)
@counter = 0
ack!
end
def stop
Updater.increment(@counter)
end
end

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store