Last active
August 29, 2015 14:13
-
-
Save sechiro/1a317775711e2dce8ce0 to your computer and use it in GitHub Desktop.
Redisのロックを取るスクリプト
This file contains hidden or 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 -ue | |
node_id=1 | |
lock_key=lock | |
ttl=10 | |
redis_db=0 | |
redis_host=localhost | |
redis_port=6379 | |
locked=`redis-cli -h $redis_host -p $redis_port -n $redis_db --raw SETNX $lock_key $node_id` | |
if [ "$locked" = 1 ];then | |
echo "Get lock: keyname: $lock_key" | |
result=`redis-cli -h $redis_host -p $redis_port -n $redis_db --raw EXPIRE $lock_key $ttl` | |
if [ "$result" = 1 ];then | |
echo "Set expire: $ttl" | |
exit 0 | |
elif [ "$result" = 0 ];then | |
echo "Couldn't set expire!" | |
exit 1 | |
fi | |
elif [ "$locked" = 0 ];then | |
echo "Couldn't get lock" | |
exit 1 | |
else | |
echo "Error!" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cronのHAなどに使う想定