Last active
December 4, 2015 03:01
-
-
Save J-Swift/9f39f55cc0e9ba592b78 to your computer and use it in GitHub Desktop.
Resque status CLI script
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
# Turns the number of seconds elapsed into an appropriate stringified description | |
function seconds_to_human_readable() | |
{ | |
local SECONDS=$1 | |
if [ ${SECONDS} -lt 1 ]; then | |
RESULT="just now" | |
elif [ ${SECONDS} -lt 60 ]; then | |
RESULT="${SECONDS} seconds" | |
elif [ ${SECONDS} -lt 3600 ]; then | |
let MINUTES=(${SECONDS} / 60) | |
RESULT="${MINUTES} minutes" | |
unset MINUTES | |
elif [ ${SECONDS} -lt 86400 ]; then | |
let HOURS=(${SECONDS} / 3600) | |
RESULT="${HOURS} hours" | |
unset HOURS | |
else | |
let DAYS=(${SECONDS} / 86400) | |
RESULT="${DAYS} days" | |
unset DAYS | |
fi | |
return 0 | |
} | |
# Generates a Resque status report for given server | |
# | |
# <-> Lists the current number of backlogged items. If this is a high number | |
# (> 50 or so, depending on the server) then there is likely a problem. | |
# <-> Lists the current worker status. Idle is good. If a worker has been working | |
# it's current task for a while (> 5 mins depending on the server) then there | |
# is likely a problem. | |
# | |
# Requires: redis-cli, python | |
function resque_check() | |
{ | |
################# | |
# Configuration # | |
################# | |
local REALM_NAME_COLOR='\e[01;34m' # Light blue | |
local HEADING_COLOR='\e[00;35m' # Dark purple | |
local WORKER_FOCUS_COLOR_1='\e[01;31m' # Pink | |
local WORKER_FOCUS_COLOR_2='\e[01;32m' # Green | |
local SPACER=' ' | |
local RESET_COLOR='\e[00m' # ANSI code to reset color settings | |
######### | |
# Setup # | |
######### | |
local NOW_TIMESTAMP=`date -u +'%FT%TZ'` | |
local NOW_UNIX=`date -j -f "%FT%TZ" "${NOW_TIMESTAMP}" +"%s"` | |
local REMOTE_HOST=$1 | |
local SUPPORTED_HOSTS=("host1" "host2") | |
local REDIS_HOST | |
local REDIS_NAMESPACE | |
local REDIS_DB_NUMBER | |
local DASHBOARD_URL | |
local RESQUE_RUN_AT_REGEX | |
local STRFTIME_FORMAT_STRING | |
case ${REMOTE_HOST} in | |
host1) | |
REDIS_HOST="host1.com" | |
REDIS_NAMESPACE="resque:host1" | |
REDIS_DB_NUMBER=0 | |
DASHBOARD_URL="http://host1.com/resque/overview" | |
# e.g. 2014-09-04T21:44:25Z | |
RESQUE_RUN_AT_REGEX='\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z' | |
STRFTIME_FORMAT_STRING='%FT%TZ' | |
;; | |
host2) | |
REDIS_HOST="host2.com" | |
REDIS_NAMESPACE="resque" | |
REDIS_DB_NUMBER=0 | |
DASHBOARD_URL="http://host2.com/resque/overview" | |
# e.g. 2014/09/04 21:44:35 UTC | |
RESQUE_RUN_AT_REGEX='\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} UTC' | |
STRFTIME_FORMAT_STRING='%Y/%m/%d %T UTC' | |
;; | |
all) | |
local SUPPORTED_HOST | |
for SUPPORTED_HOST in "${SUPPORTED_HOSTS[@]}"; do | |
resque_check ${SUPPORTED_HOST} | |
echo | |
echo '--------------------------------------------------------------------------------' | |
echo | |
done | |
return 0 | |
;; | |
*) | |
echo "Unsupported host: '${REMOTE_HOST}'" | |
echo | |
echo "Please use one of the following:" | |
local SUPPORTED_HOST | |
for SUPPORTED_HOST in "${SUPPORTED_HOSTS[@]}"; do | |
echo "${SPACER}${SUPPORTED_HOST}" | |
done | |
echo "${SPACER}all" | |
return 1 | |
;; | |
esac | |
############### | |
# DO WORK SON # | |
############### | |
local QUEUES=`redis-cli -h ${REDIS_HOST} -n ${REDIS_DB_NUMBER} smembers ${REDIS_NAMESPACE}:queues` | |
local WORKERS=`redis-cli -h ${REDIS_HOST} -n ${REDIS_DB_NUMBER} smembers ${REDIS_NAMESPACE}:workers` | |
printf "[${REALM_NAME_COLOR}%s${RESET_COLOR}]\n" "${REMOTE_HOST}" | |
printf "\n" | |
printf "${HEADING_COLOR}Queue lengths:${RESET_COLOR}\n" | |
printf "${SPACER}%6s failed\n" `redis-cli -h ${REDIS_HOST} -n ${REDIS_DB_NUMBER} llen ${REDIS_NAMESPACE}:failed` | |
local QUEUE | |
for QUEUE in ${QUEUES}; do | |
printf "${SPACER}%6s %s\n" `redis-cli -h ${REDIS_HOST} -n ${REDIS_DB_NUMBER} llen ${REDIS_NAMESPACE}:queue:${QUEUE}` "${QUEUE}" | |
done | |
printf "${HEADING_COLOR}Worker statuses:${RESET_COLOR}\n" | |
local CURRENT_NUMBER=1 | |
local WORKER | |
for WORKER in ${WORKERS}; do | |
WORKER="${REDIS_NAMESPACE}:worker:${WORKER}" | |
local WORKER_PID=`echo ${WORKER} | grep -oE ":\d+:" | sed 's/://g'` | |
local WORKER_DESCRIPTION=`redis-cli -h ${REDIS_HOST} -n ${REDIS_DB_NUMBER} get ${WORKER}` | |
printf "${SPACER}%s) PID ${WORKER_FOCUS_COLOR_1}%s${RESET_COLOR} " ${CURRENT_NUMBER} ${WORKER_PID} | |
if [ -z "${WORKER_DESCRIPTION}" ]; then | |
printf "is idle\n" | |
else | |
local STARTED_AT_TIMESTAMP=`echo ${WORKER_DESCRIPTION} | python -m json.tool | grep -oE "${RESQUE_RUN_AT_REGEX}"` | |
local STARTED_AT_UNIX=`date -j -f "${STRFTIME_FORMAT_STRING}" "${STARTED_AT_TIMESTAMP}" +"%s"` | |
seconds_to_human_readable $((NOW_UNIX - STARTED_AT_UNIX)) | |
printf "is working [${WORKER_FOCUS_COLOR_2}%s${RESET_COLOR}]\n" "${RESULT}" | |
fi | |
((CURRENT_NUMBER++)) | |
done | |
printf "\n" | |
printf "${HEADING_COLOR}Current Datetime:${RESET_COLOR} %s\n" "${NOW_TIMESTAMP}" | |
printf "${HEADING_COLOR}Dashboard URL:${RESET_COLOR} %s\n" "${DASHBOARD_URL}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment