From 132ffaf4653523ca5f05e1ffbc64ed61d48c3f1f Mon Sep 17 00:00:00 2001 From: Steve Langasek Date: Thu, 6 Jul 2017 21:14:19 -0700 Subject: [PATCH] Refine the logic for recording the nominal sent age of the mail In the previous iteration, if we were ever down/frozen/disabled long enough to miss sending two mails in a row, we would see unintended "catch-up" behavior where each subsequent run of britney would send a mail until the right total number of mails had been sent. Don't do this; instead, catch us up in one go to the most recent mail that should have been sent, avoiding bunching of notifications. This changes one of the tests also to match. --- britney2/policies/email.py | 22 +++++++++++----------- tests/test_email.py | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/britney2/policies/email.py b/britney2/policies/email.py index cc953c1..5be71d2 100644 --- a/britney2/policies/email.py +++ b/britney2/policies/email.py @@ -190,25 +190,25 @@ class EmailPolicy(BasePolicy, Rest): emails, last_sent = cached # migration of older data last_sent = int(last_sent) - if last_sent < max_age: - next_due = max_age - else: - next_due = int(math.pow(2, - int(math.log(last_sent - max_age + 1, - 2))+1) + last_sent) - if next_due - last_sent > MAX_INTERVAL: - next_due = last_sent + MAX_INTERVAL + last_due = int(math.pow(2, int(math.log(age + 2 - max_age, 2))) + + max_age - 2) + if last_due - max_age >= MAX_INTERVAL: + last_due = int((age - max_age - MAX_INTERVAL) / MAX_INTERVAL) \ + * MAX_INTERVAL + max_age + MAX_INTERVAL + if last_due < max_age: + last_due = max_age + except TypeError: # This exception happens when source_name, version never seen before emails = [] last_sent = 0 - next_due = max_age + last_due = max_age if self.dry_run: self.log("[email dry run] Age %d >= threshold %d: would email: %s" % (age, max_age, self.lp_get_emails(source_name, version))) # don't update the cache file in dry run mode; we'll see all output each time return PolicyVerdict.PASS - if age >= next_due: + if last_sent < last_due: if not emails: emails = self.lp_get_emails(source_name, version) if emails: @@ -221,7 +221,7 @@ class EmailPolicy(BasePolicy, Rest): server.sendmail('noreply@canonical.com', emails, msg) server.quit() # record the age at which the mail should have been sent - last_sent = next_due + last_sent = last_due except socket.error as err: self.log("Failed to send mail! Is SMTP server running?") self.log(err) diff --git a/tests/test_email.py b/tests/test_email.py index 15f6751..f971cf8 100755 --- a/tests/test_email.py +++ b/tests/test_email.py @@ -266,7 +266,7 @@ class T(unittest.TestCase): 1, 3, 5, 7, 11, 19, 35, 65, 95, 125, 155, 185 ]) self.smtp_repetition(valid=[False, False, True, False, True], expected=[ - 1, 3, 5, 13, 29, 59, 89, 119, 149, 179 + 1, 3, 5, 7, 11, 19, 35, 65, 95, 125, 155, 185 ])