summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2013-04-20 11:25:03 +0100
committerLars Wirzenius <liw@liw.fi>2013-04-20 11:25:03 +0100
commit389f0a3d444e113b3ca36baa72306b404f1ae6f4 (patch)
tree29e777706f11532339baab061c03e689067974f8
parent963664a6d980a4e425e557f766408d23f0de4a32 (diff)
downloaddesktop-cronish-389f0a3d444e113b3ca36baa72306b404f1ae6f4.tar.gz
Fix trigger-file
-rwxr-xr-xdesktop-cronish33
-rwxr-xr-xtests/mtime-new.script12
-rwxr-xr-xtests/mtime-nofile.script11
-rw-r--r--tests/mtime-nofile.stdout1
-rwxr-xr-xtests/mtime-old.script11
-rw-r--r--tests/mtime-old.stdout1
6 files changed, 47 insertions, 22 deletions
diff --git a/desktop-cronish b/desktop-cronish
index 70f2e40..98e78ca 100755
--- a/desktop-cronish
+++ b/desktop-cronish
@@ -39,6 +39,11 @@ class DesktopCronish(cliapp.Application):
self.settings.boolean(
['quiet', 'q'],
'no status messaging to terminal')
+
+ self.settings.integer(
+ ['sleep'],
+ 'if there is no known job, sleep for SECONDS',
+ default=1)
def process_args(self, args):
self.ts = ttystatus.TerminalStatus(period=0.1)
@@ -68,8 +73,14 @@ class DesktopCronish(cliapp.Application):
max_jobs = self.settings['max-jobs']
while max_jobs == 0 or n < max_jobs:
job_name, when = self.choose_job()
- self.wait_until(when, job_name)
- self.execute_job(job_name)
+ if job_name is not None:
+ self.wait_until(when, job_name)
+ self.execute_job(job_name)
+ else:
+ self.status(
+ 'No idea what to do, sleeping for %d seconds' %
+ self.settings['sleep'])
+ time.sleep(self.settings['sleep'])
n += 1
self.status('Stopped executing after %d jobs' % n)
@@ -84,9 +95,10 @@ class DesktopCronish(cliapp.Application):
else:
raise cliapp.AppException(
'Unknown job trigger for %s' % job_name)
- if next_job_name is None or job_when <= next_when:
- next_job_name = job_name
- next_when = job_when
+ if job_when is not None:
+ if next_job_name is None or job_when <= next_when:
+ next_job_name = job_name
+ next_when = job_when
return next_job_name, next_when
def when_interval_job(self, job_name, job):
@@ -101,10 +113,17 @@ class DesktopCronish(cliapp.Application):
# If there is no max age for file, but it exists, never trigger.
if 'trigger-age' not in job:
- return -1
+ return None
+ # If the file exists and is too old, trigger now.
mtime = os.path.getmtime(filename)
- return mtime + job['trigger-age']
+ if mtime + job['trigger-age'] <= self.now():
+ return self.now()
+
+ # Do not trigger now. We can't compute the next time to trigger:
+ # the file might go missing, or get updated, so we need to test
+ # it every iteration.
+ return None
def wait_until(self, when, for_what):
while self.now() < when:
diff --git a/tests/mtime-new.script b/tests/mtime-new.script
index 2d1f28e..ae1338c 100755
--- a/tests/mtime-new.script
+++ b/tests/mtime-new.script
@@ -2,13 +2,15 @@
set -eu
-touch "$DATADIR/foo.trigger"
-cat <<EOF > "$DATADIR/foo.tasks"
+touch "$DATADIR/mtime-new.trigger"
+touch "$DATADIR/mtime-new.out"
+cat <<EOF > "$DATADIR/mtime-new.tasks"
date:
- trigger-file: $DATADIR/foo.trigger
+ trigger-file: $DATADIR/mtime-new.trigger
trigger-age: 10
- command: echo triggered > $DATADIR/foo.out
+ command: echo triggered > $DATADIR/mtime-new.out
EOF
-./desktop-cronish --quiet --max-jobs 5 "$DATADIR/foo.tasks"
+./desktop-cronish --quiet --max-jobs 5 "$DATADIR/mtime-new.tasks"
+cat "$DATADIR/mtime-new.out"
diff --git a/tests/mtime-nofile.script b/tests/mtime-nofile.script
index 9a04a71..772becc 100755
--- a/tests/mtime-nofile.script
+++ b/tests/mtime-nofile.script
@@ -2,13 +2,14 @@
set -eu
-rm -f "$DATADIR/foo.trigger"
-cat <<EOF > "$DATADIR/foo.tasks"
+rm -f "$DATADIR/mtime-nofile.trigger"
+cat <<EOF > "$DATADIR/mtime-nofile.tasks"
date:
- trigger-file: $DATADIR/foo.trigger
+ trigger-file: $DATADIR/mtime-nofile.trigger
trigger-age: 10
- command: echo triggered > $DATADIR/foo.out
+ command: echo triggered > $DATADIR/mtime-nofile.out
EOF
-./desktop-cronish --quiet --max-jobs 1 "$DATADIR/foo.tasks"
+./desktop-cronish --quiet --max-jobs 1 "$DATADIR/mtime-nofile.tasks"
+cat "$DATADIR/mtime-nofile.out"
diff --git a/tests/mtime-nofile.stdout b/tests/mtime-nofile.stdout
new file mode 100644
index 0000000..bd20802
--- /dev/null
+++ b/tests/mtime-nofile.stdout
@@ -0,0 +1 @@
+triggered
diff --git a/tests/mtime-old.script b/tests/mtime-old.script
index a14ad25..b58b53e 100755
--- a/tests/mtime-old.script
+++ b/tests/mtime-old.script
@@ -2,13 +2,14 @@
set -eu
-touch -t 197001010000.00 "$DATADIR/foo.trigger"
-cat <<EOF > "$DATADIR/foo.tasks"
+touch -t 197001010000.00 "$DATADIR/mtime-old.trigger"
+cat <<EOF > "$DATADIR/mtime-old.tasks"
date:
- trigger-file: $DATADIR/foo.trigger
+ trigger-file: $DATADIR/mtime-old.trigger
trigger-age: 10
- command: echo triggered > $DATADIR/foo.out
+ command: echo triggered > $DATADIR/mtime-old.out
EOF
-./desktop-cronish --quiet --max-jobs 5 "$DATADIR/foo.tasks"
+./desktop-cronish --quiet --max-jobs 5 "$DATADIR/mtime-old.tasks"
+cat "$DATADIR/mtime-old.out"
diff --git a/tests/mtime-old.stdout b/tests/mtime-old.stdout
new file mode 100644
index 0000000..bd20802
--- /dev/null
+++ b/tests/mtime-old.stdout
@@ -0,0 +1 @@
+triggered