diff options
author | Ben Hutchings <ben.hutchings@codethink.co.uk> | 2020-09-15 01:57:49 +0100 |
---|---|---|
committer | Ben Hutchings <ben.hutchings@codethink.co.uk> | 2020-09-15 01:59:50 +0100 |
commit | 0849d45a489149e98c58b77be9f2f2db456ee6bb (patch) | |
tree | b56097ae1693a152f5efda9e13a18e5b48832271 | |
parent | a42dda78aeb266f9f75a2303e6113c707f78907b (diff) | |
download | lorry-controller-0849d45a489149e98c58b77be9f2f2db456ee6bb.tar.gz |
lorry-controller-remove-old-jobs: Filter out jobs started too recently
Job IDs are assigned sequentially, and jobs cannot finish before they
start. Therefore we can iterate over jobs in order of ID and stop
when we find a job that started more recently than max-age-seconds
ago.
Related to #18.
-rwxr-xr-x | lorry-controller-remove-old-jobs | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/lorry-controller-remove-old-jobs b/lorry-controller-remove-old-jobs index 94ad71b..d6dca38 100755 --- a/lorry-controller-remove-old-jobs +++ b/lorry-controller-remove-old-jobs @@ -71,7 +71,7 @@ class OldJobRemover(cliapp.Application): def process_args(self, args): logging.info('Removing old jobs from Lorry Controller STATEDB') - for job_id in self.list_jobs(): + for job_id in sorted(self.list_jobs()): try: job_info = self.get_job_info(job_id) except urllib.error.HTTPError as e: @@ -80,6 +80,13 @@ class OldJobRemover(cliapp.Application): (job_id, str(e))) continue + # If the start time of this job is less than max-age ago, + # then the finish time of this job, and every job with a + # higher ID, must be less than max-age ago. So we can + # stop now. + if self.was_started_too_recently(job_info): + break + if self.is_old(job_info): self.remove_job(job_info.job_id) @@ -123,6 +130,11 @@ class OldJobRemover(cliapp.Application): age_in_seconds = current_time - job_info.exit_timestamp return age_in_seconds >= self.settings['max-age-in-seconds'] + def was_started_too_recently(self, job_info): + current_time = self.get_current_time() + age_in_seconds = current_time - job_info.start_timestamp + return age_in_seconds < self.settings['max-age-in-seconds'] + def get_current_time(self): if self.settings['debug-now']: return self.settings['debug-now'] |