Author: tmckay
Date: 2011-03-29 20:47:56 +0000 (Tue, 29 Mar 2011)
New Revision: 4672
Modified:
trunk/cumin/bin/cumin-database
Log:
Add check-created-wait function that looks at output from psql command on failure to
determine if the problem is a connection issue.
If so, retries up to 30 times with 1 second delay.
BZ681124
Modified: trunk/cumin/bin/cumin-database
===================================================================
--- trunk/cumin/bin/cumin-database 2011-03-29 17:08:41 UTC (rev 4671)
+++ trunk/cumin/bin/cumin-database 2011-03-29 20:47:56 UTC (rev 4672)
@@ -34,6 +34,19 @@
return ${PIPESTATUS[0]}
}
+function retry {
+ completed=1
+ for ((c=0; c<=$2; c++))
+ do
+ $1 && completed=0
+ if [ $completed -eq 0 ]; then
+ break
+ fi
+ sleep 1
+ done
+ return $completed
+}
+
function check-environment {
which rpm > /dev/null
@@ -86,6 +99,34 @@
}
}
+function check-created-wait {
+ for ((c=0; c<=30; c++))
+ do
+ res="$(psql -d cumin -U cumin -h localhost -c '\q'
2>&1)"
+ case $res in
+ *"could not connect"*)
+ if [ $c -eq 0 ] ; then
+ echo
+ fi
+ echo could not connect to server, retry...
+ sleep 1
+ ;;
+ "")
+ return 0
+ ;;
+ *)
+ echo "Error: The database is not created"
+ if [ "$1" != noadvice ]; then
+ echo "Hint: Run 'cumin-database create'"
+ fi
+ return 1
+ ;;
+ esac
+ done
+ echo "Error: Could not connect to server"
+ return 1
+}
+
function confirm-install {
cat <<EOF
WARNING
@@ -140,7 +181,7 @@
check-started &> /dev/null || start > /dev/null
- check-created &> /dev/null || create
+ check-created-wait &> /dev/null || create
}
function start {
@@ -166,19 +207,10 @@
# proper restart is thus simulated with condstop and start.
# Apparently condstop sometimes fails. Try up to 5 times.
- stopped=1
- for i in 1 2 3 4 5
- do
- run "/sbin/service postgresql condstop" && stopped=0
- if [ $stopped -eq 0 ] ; then
- break
- fi
- sleep 1
- done
- if [ $stopped -eq 1 ] ; then
+ retry "/sbin/service postgresql condstop" 5 || {
echo "postgres restart failed"
exit 1
- fi
+ }
/sbin/service postgresql start
}
@@ -332,7 +364,7 @@
echo "OK"
echo -n "Checking database 'cumin' ... "
- check-created $2 || exit 1
+ check-created-wait $2 || exit 1
echo "OK"
echo "The database is ready"
Show replies by date