From 0849d45a489149e98c58b77be9f2f2db456ee6bb Mon Sep 17 00:00:00 2001 From: Ben Hutchings Date: Tue, 15 Sep 2020 01:57:49 +0100 Subject: 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. --- lorry-controller-remove-old-jobs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) 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'] -- cgit v1.2.1