From 8db90e9a76eed925bf93f8ba86c4e20afc9ae49e Mon Sep 17 00:00:00 2001 From: Karol Gugala Date: Tue, 5 Mar 2019 16:35:40 +0100 Subject: [PATCH] utils: mergedb: lock database accesses Signed-off-by: Karol Gugala --- utils/mergedb.sh | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/utils/mergedb.sh b/utils/mergedb.sh index da1cbbea..3ec63507 100755 --- a/utils/mergedb.sh +++ b/utils/mergedb.sh @@ -9,6 +9,9 @@ test -e "$2" tmp1=`mktemp -p .` tmp2=`mktemp -p .` +LOCKFILE="/tmp/segbits_$1.db.lock" +LOCKTIMEOUT=30 # 30s timeout +LOCKID=222 function finish { echo "Cleaning up temp files" @@ -18,10 +21,24 @@ function finish { trap finish EXIT db=$XRAY_DATABASE_DIR/$XRAY_DATABASE/segbits_$1.db + +# if the DB exists acquire a lock +if [ -f $db ]; then + eval "exec $LOCKID>$LOCKFILE" + # the lock is automatically released on script exit + flock --timeout $LOCKTIMEOUT $LOCKID + if [ ! $? -eq 0 ]; then + echo "Timeout while waiting for lock" + finish + exit 1 + fi +fi + # Check existing DB if [ -f $db ] ; then ${XRAY_PARSEDB} --strict "$db" fi + # Check new DB ${XRAY_PARSEDB} --strict "$2" @@ -112,4 +129,3 @@ else fi # Check aggregate db for consistency and make canonical ${XRAY_PARSEDB} --strict "$tmp2" "$db" -