aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSilvio Rhatto <rhatto@riseup.net>2013-08-25 22:28:21 -0300
committerSilvio Rhatto <rhatto@riseup.net>2013-08-25 22:28:21 -0300
commit18cf5a99d0eee6560ee2826969620bae30b91938 (patch)
tree334094b1aee331623f637abd9fc91b6d861a85aa
parent05ce52fe580b015d5cc21e0c31a999970a34f4d5 (diff)
downloadhttruta-18cf5a99d0eee6560ee2826969620bae30b91938.tar.gz
httruta-18cf5a99d0eee6560ee2826969620bae30b91938.tar.bz2
Initial lockfile support
-rw-r--r--README.mdwn1
-rwxr-xr-xhttracker4
-rw-r--r--lib/httracker/functions52
-rwxr-xr-xscuttler4
4 files changed, 54 insertions, 7 deletions
diff --git a/README.mdwn b/README.mdwn
index 3309f60..169f469 100644
--- a/README.mdwn
+++ b/README.mdwn
@@ -16,4 +16,3 @@ TODO
- Include all sites already donwloaded by scuttler.
- Support for other fetchers like youtube-dl.
-- Lockfile support.
diff --git a/httracker b/httracker
index cd99f60..7fc33c1 100755
--- a/httracker
+++ b/httracker
@@ -18,5 +18,5 @@ fi
# Iterate over all URLs
httracker_iterate
-# Cleanup
-rm -rf $URLS
+# Done
+httracker_teardown
diff --git a/lib/httracker/functions b/lib/httracker/functions
index 8e8964e..e3c81c4 100644
--- a/lib/httracker/functions
+++ b/lib/httracker/functions
@@ -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
diff --git a/scuttler b/scuttler
index 1c64622..5dd383a 100755
--- 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