Recently, working on a migration project where a portion of the stack was built in ruby, I needed to determine a small and dependency-less way of implementing a liveness probe/health check for sidekiq. After coming across a number of posts detailing a gem I could include as a dependency, I decided to take a stab at creating something a bit simpler. For a number of reasons, I don’t love introducing new dependencies to a stack, especially when it comes to production-grade code. Accepting that we do already have instrumented monitoring once these services are actually ready and taking requests, my main need was to provide some simple way to just test if the sidekiq pods were alive and not in a failed state. Enter sidekiqmon. Since sidekiq version 6, the gem has shipped with an executable in the bin/ directory called sidekiqmon, which provides some basic stats about the state of the sidekiq process running on an instance. Upon learning this, I realized I could accomplish my task with a fairly simple, if not entirely pretty bash script.

#!/bin/bash
#sidekiqmon.sh
checkSidekiq() {
  if [ $(bundle exec sidekiqmon processes | grep -Po '\d' | awk '{s+=$1} END {print s}') -gt 0 ]; then
    exit 0
  else
    exit 1
  fi
}

checkSidekiq

It’s dead simple. We make a call to sidekiqmon, parse the output for the number of processes, and if it’s greater than 0, we know sidekiq isn’t in a failed state and this is at the very least alive. To implement it in docker, one can simply add the following to a docker-compose file:

#docker-compose.yml
services:
  sidekiq:
    #[...] your sidekiq container build and other config stuff
    healthcheck:
      #in this example, we copied sidekiqmon.sh to /opt/ in our Dockerfile
      test: ["CMD", "bash", "/opt/sidekiqmon.sh"]
      interval: 5s
      timeout: 5s
      retries: 20
      start_period: 20 #give our containers plenty of time to start up before we do healthchecks

And for k8s, it’s similarly simple:

#[...]
livenessProbe:
  exec:
    command:
      - /bin/sh
      - -c
      - bash /opt/sidekiqmon.sh
  failureThreshold: 1
  initialDelaySeconds: 30
  periodSeconds: 10
  successThreshold: 1
  timeoutSeconds: 4

There’s certainly a lot here that could be improved and made more resilient, but for the purposes of a dead-simple, dependency-less sidekiq liveness/health probe for k8s/docker, it does the trick.