Skip to content

Instantly share code, notes, and snippets.

@xorenio
Last active March 28, 2024 22:43
Show Gist options
  • Save xorenio/44fe3bf337ebeda04cfa733720fc4b28 to your computer and use it in GitHub Desktop.
Save xorenio/44fe3bf337ebeda04cfa733720fc4b28 to your computer and use it in GitHub Desktop.
bash redis queue
# START - QUEUE
# Function: _queue_command
# Description: Adds a command to the Redis command queue with an optional schedule.
# Parameters:
# $1: The command to be added to the queue. Example "echo Hello, World!"
# $2: (Optional) The schedule for the command ("2023-06-30 12:00:00"). Default is "now".
# Returns: None
_queue_command() {
local command="$1"
local schedule="${2:-now}"
local timestamp
if [[ "$schedule" == "now" ]]; then
timestamp=$(date "+%Y-%m-%d_%H-%M-%S")
else
timestamp=$(date -d "$schedule" "+%Y-%m-%d_%H-%M-%S")
fi
redis-cli rpush command_queue "$timestamp:$command"
}
# Function: _process_queue
# Description: Starts a queue worker that processes commands from the Redis queue.
# Parameters: None
# Returns: None
_process_queue() {
local lock_key="worker_lock"
local lock_value="1"
# Get public IP
local public_ip lock_status commands count timestamp cmd current_time output
public_ip=$(_get_public_ip)
while true; do
# Acquire the lock
lock_status=$(redis-cli setnx $lock_key $lock_value)
# If lock is acquired, process the commands
if [[ $lock_status -eq 1 ]]; then
mapfile -t commands < <(redis-cli lrange command_queue 0 -1)
count=${#commands[@]}
if [[ $count -gt 0 ]]; then
echo "Executing $count command(s)"
for command in "${commands[@]}"; do
timestamp=${command%%:*}
cmd=${command#*:}
current_time=$(date "+%Y-%m-%d_%H-%M-%S")
if [[ "$timestamp" == "$current_time" || "$timestamp" == "now" ]]; then
echo "Executing command: $cmd"
output=$(eval "$cmd") # Execute the command and capture the output
if [[ $? -ne 0 ]]; then
echo "Command execution failed. Logging error..."
echo "$output" > error.txt
curl -X POST -H "Content-Type: text/plain" -d @error.txt "${DEPLOYMENT_ERROR_URL}/${public_ip}"
fi
redis-cli lrem command_queue 1 "$command"
fi
done
fi
# Release the lock
redis-cli del $lock_key
else
sleep 1s
fi
done
}
# END - QUEUE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment