Skip to content

Instantly share code, notes, and snippets.

@krishamoud
Last active July 16, 2020 14:33
Show Gist options
  • Save krishamoud/76a3d4e76ad7da3204473fc7abe1682b to your computer and use it in GitHub Desktop.
Save krishamoud/76a3d4e76ad7da3204473fc7abe1682b to your computer and use it in GitHub Desktop.
sidekiq:
terminationGracePeriodSeconds: 1200 # Max time to wait for sidekiq jobs to finish is 20 minutes
container:
command:
- bundle
- exec
- sidekiqswarm
preStopCMD: ["/bin/bash","/app/bin/sidekiq_graceful_shutdown.sh"]
#!/bin/bash
# Helper function for printing logs with timestamp
echo_time() {
date +"[%H:%M:%S] [Sidekiq-graceful-shutdown] $*"
}
kill -TSTP 1
SIDEKIQ_BUSY=true
while $SIDEKIQ_BUSY ; do
SIDEKIQ_BUSY=false
sidekiq_pids=$(ps aux | grep '[s]idekiq' | grep 'busy' | awk '{print $2}')
for pid in $sidekiq_pids ; do
IS_SIDEKIQ_DONE=$(ps -f --pid $pid | grep -v PID | grep -E "\[0 of [0-9]+ busy\]" > /dev/null)$?
if [ $IS_SIDEKIQ_DONE -ne 0 ]; then
echo_time "Sidekiq is still busy"
sleep 5
SIDEKIQ_BUSY=true
fi
done
done
echo_time "TERM signal - Shutting down sidekiq"
kill -TERM 1 2>&1 > /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment