Skip to content

Instantly share code, notes, and snippets.

@ndbroadbent
Created June 10, 2019 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ndbroadbent/935b51c8e6c5dffa03769e114df11561 to your computer and use it in GitHub Desktop.
Save ndbroadbent/935b51c8e6c5dffa03769e114df11561 to your computer and use it in GitHub Desktop.
sidekiq_sanity_check_v3 - Revert to using --daemon flag, and use a lightweight Ruby script to push the job to Redis
#!/bin/bash
set -e
if [ -f .env ]; then spring stop; fi
# This script is run in the CI build. It requires a REDIS_URL variable,
# and runs a test job through the WorkerHealthCheckJob.
# I added this check after I accidentally pushed a change that caused the sidekiq
# workers to crash on boot. The jobs were well-tested, but the actual
# sidekiq config had an error inside a #configure_server block, so the code
# only crashed when I started an actual Sidekiq worker process.
SIDEKIQ_PID_FILE="tmp/sidekiq-test-worker.pid"
SIDEKIQ_LOG_FILE="log/sidekiq-test-worker.log"
SIDEKIQ_RESULT_FILE="/tmp/sidekiq-worker-test.txt"
echo "Starting Sidekiq process..."
# pidfile / logfile / daemon options will be removed in Sidekiq 6
# See: https://github.com/mperham/sidekiq/issues/4045
# However, I was having problems running "sidekiq &" in the CI Docker container.
sidekiq --pidfile "$SIDEKIQ_PID_FILE" --logfile "$SIDEKIQ_LOG_FILE" --daemon
rm -rf "$SIDEKIQ_RESULT_FILE"
RANDOM_STRING=$(openssl rand -hex 32)
cat > /tmp/sidekiq-redis-script.rb <<RUBY
require 'redis'
require 'securerandom'
require 'json'
redis = Redis.new(:url => ENV.fetch('REDIS_URL'))
data = { "class" => 'WorkerHealthCheckJob',
"queue" => 'default',
"args" => [ARGV[0]],
'retry' => false,
'jid' => SecureRandom.hex(12),
'created_at' => Time.now.to_f,
'enqueued_at' => Time.now.to_f }
msg = JSON.dump(data)
puts "Pushing WorkerHealthCheckJob to Redis (queue:default) - #{msg}"
redis.lpush("queue:default", msg)
RUBY
ruby /tmp/sidekiq-redis-script.rb "$RANDOM_STRING"
SUCCESS="no"
RETRY_COUNT=0
echo "Waiting for $SIDEKIQ_RESULT_FILE to contain $RANDOM_STRING..."
while [ ! -f "$SIDEKIQ_RESULT_FILE" ]; do
if [ $RETRY_COUNT -gt 60 ]; then
echo "Did not find $SIDEKIQ_RESULT_FILE after 60 seconds!"
break
fi
RETRY_COUNT=$((RETRY_COUNT+1))
sleep 1
done
if [ -f "$SIDEKIQ_RESULT_FILE" ]; then
RESULT_CONTENTS=$(cat "$SIDEKIQ_RESULT_FILE")
if [ "$RESULT_CONTENTS" == "$RANDOM_STRING" ]; then
echo "Success!"
SUCCESS="yes"
else
echo "$SIDEKIQ_RESULT_FILE contents did not match the expected string!"
echo "Contents: '$RESULT_CONTENTS'"
echo "Expected: '$RANDOM_STRING'"
fi
fi
if [ -f "$SIDEKIQ_PID_FILE" ]; then
SIDEKIQ_PID=$(cat "$SIDEKIQ_PID_FILE")
if [ -z "$SIDEKIQ_PID" ]; then
echo "$SIDEKIQ_PID_FILE was empty. Please manually kill Sidekiq."
fi
else
echo "Could not find PID file at $SIDEKIQ_PID_FILE. Please manually kill Sidekiq."
fi
if [ -f "$SIDEKIQ_LOG_FILE" ]; then
echo "Sidekiq Logs:"
cat "$SIDEKIQ_LOG_FILE"
fi
if [ -z "$SIDEKIQ_PID" ]; then exit 1; fi
echo "Killing Sidekiq process ($SIDEKIQ_PID)..."
kill -9 "$SIDEKIQ_PID"
rm -rf "$SIDEKIQ_PID_FILE" "$SIDEKIQ_LOG_FILE" "$SIDEKIQ_RESULT_FILE"
if [ "$SUCCESS" != "yes" ]; then
echo "Sidekiq test failed!"
exit 1
fi
if [ -f .env ]; then spring stop; fi
echo "Sidekiq test passed!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment