~ubuntu-branches/ubuntu/trusty/run-one/trusty-proposed

« back to all changes in this revision

Viewing changes to run-one

  • Committer: Package Import Robot
  • Author(s): Dustin Kirkland
  • Date: 2013-07-17 09:42:29 UTC
  • mfrom: (1.1.8)
  • Revision ID: package-import@ubuntu.com-20130717094229-ms3uw0z4xk9852xp
Tags: 1.11-0ubuntu1
* === added symlink run-until-failure, === added symlink run-until-
  success, debian/links, run-one, run-one.1, === target is urun-one:
  - add two new twists on keep-one-running, enabling a user to keep
    retrying a command until it succeeds or fails
* keep-one-running:
  - sleep a maximum of 60 seconds
  - back down a little more slowly

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
CMDHASH=$(echo "$CMD" | md5sum | awk '{print $1}')
42
42
FLAG="$DIR/$CMDHASH"
43
43
 
44
 
# Handle run-this-one invocation, by killing matching process first
45
 
case "$(basename $0)" in
 
44
base="$(basename $0)"
 
45
case "$base" in
46
46
        run-one)
47
47
                # Run the specified command, assuming we can flock this command string's hash
48
48
                flock -xn "$FLAG" -c "$CMD"
67
67
                # Run the specified command, assuming we can flock this command string's hash
68
68
                flock -xn "$FLAG" -c "$CMD"
69
69
        ;;
70
 
        keep-one-running)
71
 
                backoff=1
 
70
        keep-one-running|run-until-success|run-until-failure)
 
71
                backoff=0
 
72
                retries=0
72
73
                while true; do
73
74
                        # Run the specified command, assuming we can flock this command string's hash
74
75
                        set +e
75
76
                        flock -xn "$FLAG" -c "$CMD"
76
77
                        if [ "$?" = 0 ]; then
 
78
                                # If we were waiting for success, we're done
 
79
                                [ "$base" = "run-until-success" ] && exit $?
77
80
                                # Last run finished successfully, reset to minimum back-off of 1 second
78
 
                                backoff=1
 
81
                                backoff=0
 
82
                                backoff=0
79
83
                        else
80
 
                                # Last run failed, so slow down the retries (up to 5 minutes)
81
 
                                backoff=$((backoff + 1))
82
 
                                [ $backoff -gt 300 ] && backoff=300
83
 
                                logger -t "$(basename $0)[$$]" "last run failed; sleeping [$backoff] seconds before next run"
 
84
                                # If we were waiting for failure, we're done
 
85
                                [ "$base" = "run-until-failure" ] && exit $?
 
86
                                # Last run failed, so slow down the retries
 
87
                                retries=$((retries + 1))
 
88
                                backoff=$((retries / 10))
 
89
                                logger -t "${base}[$$]" "last run failed; sleeping [$backoff] seconds before next run"
84
90
                        fi
 
91
                        # Don't sleep more than 60 seconds
 
92
                        [ $backoff -gt 60 ] && backoff=60
85
93
                        sleep $backoff
86
94
                done
87
95
        ;;