diff options
| -rw-r--r-- | README.mdwn | 1 | ||||
| -rwxr-xr-x | httracker | 4 | ||||
| -rw-r--r-- | lib/httracker/functions | 52 | ||||
| -rwxr-xr-x | scuttler | 4 | 
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. @@ -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 @@ -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 | 
