From 389f0a3d444e113b3ca36baa72306b404f1ae6f4 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 20 Apr 2013 11:25:03 +0100 Subject: Fix trigger-file --- desktop-cronish | 33 ++++++++++++++++++++++++++------- tests/mtime-new.script | 12 +++++++----- tests/mtime-nofile.script | 11 ++++++----- tests/mtime-nofile.stdout | 1 + tests/mtime-old.script | 11 ++++++----- tests/mtime-old.stdout | 1 + 6 files changed, 47 insertions(+), 22 deletions(-) create mode 100644 tests/mtime-nofile.stdout create mode 100644 tests/mtime-old.stdout 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 < "$DATADIR/foo.tasks" +touch "$DATADIR/mtime-new.trigger" +touch "$DATADIR/mtime-new.out" +cat < "$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 < "$DATADIR/foo.tasks" +rm -f "$DATADIR/mtime-nofile.trigger" +cat < "$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 < "$DATADIR/foo.tasks" +touch -t 197001010000.00 "$DATADIR/mtime-old.trigger" +cat < "$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 -- cgit v1.2.1