Last active
June 10, 2019 13:46
-
-
Save ndbroadbent/4abcaf908463da1c241fbe8e8dec2456 to your computer and use it in GitHub Desktop.
Sidekiq test that I run on CI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
# 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..." | |
sidekiq --pidfile "$SIDEKIQ_PID_FILE" --logfile "$SIDEKIQ_LOG_FILE" --daemon | |
rm -rf "$SIDEKIQ_RESULT_FILE" | |
RANDOM_STRING=$(openssl rand -hex 32) | |
echo "Creating a WorkerHealthCheckJob job via rails runner..." | |
./bin/rails runner "WorkerHealthCheckJob.perform_async('$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 20 ]; then | |
echo "Did not find $SIDEKIQ_RESULT_FILE after 20 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 [ -z "$SIDEKIQ_PID" ]; then exit 1; fi | |
echo "Shutting down Sidekiq process ($SIDEKIQ_PID)..." | |
kill -HUP "$SIDEKIQ_PID" | |
rm -rf "$SIDEKIQ_PID_FILE" "$SIDEKIQ_LOG_FILE" "$SIDEKIQ_RESULT_FILE" | |
if [ "$SUCCESS" != "yes" ]; then exit 1; fi | |
if [ -f .env ]; then spring stop; fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
class WorkerHealthCheckJob | |
include Sidekiq::Worker | |
def perform(file_contents) | |
File.open('/tmp/sidekiq-worker-test.txt', 'w') { |f| f.write(file_contents) } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
RSpec.describe WorkerHealthCheckJob do | |
it 'writes some data to a temporary file' do | |
file_mock = double | |
expect(file_mock).to receive(:write).with('12345') | |
expect(File).to receive(:open).with('/tmp/sidekiq-worker-test.txt', 'w').and_yield(file_mock) | |
WorkerHealthCheckJob.new.perform('12345') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment