]> gitweb.fluxo.info Git - httruta.git/commitdiff
Initial lockfile support
authorSilvio Rhatto <rhatto@riseup.net>
Mon, 26 Aug 2013 01:28:21 +0000 (22:28 -0300)
committerSilvio Rhatto <rhatto@riseup.net>
Mon, 26 Aug 2013 01:28:21 +0000 (22:28 -0300)
README.mdwn
httracker
lib/httracker/functions
scuttler

index 3309f60cdb589f68587baddd731a618cc054b775..169f469aacdbce3eab9d87eced22fdefb142318f 100644 (file)
@@ -16,4 +16,3 @@ TODO
 
 - Include all sites already donwloaded by scuttler.
 - Support for other fetchers like youtube-dl.
-- Lockfile support.
index cd99f601078adc352b39bccb248eb39b836f78cd..7fc33c17b656d7f0e29f4987cc489afced022e54 100755 (executable)
--- a/httracker
+++ b/httracker
@@ -18,5 +18,5 @@ fi
 # Iterate over all URLs
 httracker_iterate
 
-# Cleanup
-rm -rf $URLS
+# Done
+httracker_teardown
index 8e8964e4db6793abf5501d9627441f631e0240d3..e3c81c445d701ea0f02a554b7cc05608cc5d4553 100644 (file)
@@ -109,12 +109,60 @@ function httracker_setup_folders {
 }
 
 # Set basic environment
-function httracker_set_env {
+function httracker_initialize {
   source `dirname $0`/config || exit 1
 
   # Create folders
   httracker_setup_folders
+
+  # Lockfile
+  LOCKFILE="${TMP}/`basename $0`.lock"
+  httracker_check_lockfile
+  httracker_set_lockfile
+}
+
+# Cleanup environment
+function httracker_teardown {
+  httracker_unset_lockfile
+  rm -rf $URLS
+}
+
+# Create lockfile
+function httracker_set_lockfile {
+  if [ ! -z "$LOCKFILE" ]; then
+    mkdir -p `dirname $LOCKFILE`
+    if ( set -o noclobber; echo "$$" > "$LOCKFILE" ) &> /dev/null; then
+      trap 'httracker_unset_lockfile' INT TERM EXIT
+    else
+      echo "Could not create lockfile $LOCKFILE, exiting"
+      exit 1
+    fi
+  fi
+}
+
+# Remove lockfile
+function httracker_unset_lockfile {
+  if [ ! -z "$LOCKFILE" ]; then
+    $rm -f $LOCKFILE || echo "Could not remove lockfile $LOCKFILE"
+  fi
+}
+
+# Check lockfile
+function httracker_check_lockfile {
+  local pid process
+
+  if [ ! -z "$LOCKFILE" ] && [ -f "$LOCKFILE" ]; then
+    pid="`cat $LOCKFILE`"
+    process="`ps --no-headers -o comm $pid`"
+    if [ "$?" == "0" ] && [ "`ps --no-headers -o comm $$`" == "$process" ]; then
+      echo "Another program is running for $LOCKFILE, skipping run"
+      exit
+    else
+      echo "Found old lockfile $LOCKFILE, removing it"
+      unset_LOCKFILE
+    fi
+  fi
 }
 
 # Initialize
-httracker_set_env
+httracker_initialize
index 1c6462292642c85955e4eda3aee03c865564b75e..5dd383a6236bd197f5916198554c91903d108310 100755 (executable)
--- a/scuttler
+++ b/scuttler
@@ -26,5 +26,5 @@ httracker_sqlquery "select bAddress from sc_bookmarks;" > ${URLS}
 # Download in mirror mode
 httracker_iterate
 
-# Cleanup
-rm -rf $URLS
+# Done
+httracker_teardown