From bd75a65866756eb97ecb785ea91cdf1c1be5d9ee Mon Sep 17 00:00:00 2001 From: Erik Arvstedt Date: Fri, 11 May 2018 14:01:19 +0200 Subject: [PATCH] Refactor: renamings, extract fn 'loop' Renamings: loop -> loop_step delta -> next_mail_time (this variable names a point in time, not a duration) Extracting the 'loop' fn is a preparation for later commits where a second type of loop is added. --- .../management/commands/document_consumer.py | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/documents/management/commands/document_consumer.py b/src/documents/management/commands/document_consumer.py index 4aec489b6..c5fe9baa2 100644 --- a/src/documents/management/commands/document_consumer.py +++ b/src/documents/management/commands/document_consumer.py @@ -74,24 +74,27 @@ class Command(BaseCommand): ) if options["oneshot"]: - self.loop(mail_delta=mail_delta) + self.loop_step(mail_delta) else: try: - while True: - self.loop(mail_delta=mail_delta) - time.sleep(loop_time) - if self.verbosity > 1: - print(".", int(time.time())) + self.loop(loop_time, mail_delta) except KeyboardInterrupt: print("Exiting") - def loop(self, mail_delta): + def loop(self, loop_time, mail_delta): + while True: + self.loop_step(mail_delta) + time.sleep(loop_time) + if self.verbosity > 1: + print(".", int(time.time())) + + def loop_step(self, mail_delta): # Occasionally fetch mail and store it to be consumed on the next loop # We fetch email when we first start up so that it is not necessary to # wait for 10 minutes after making changes to the config file. - delta = self.mail_fetcher.last_checked + mail_delta - if self.first_iteration or delta < datetime.datetime.now(): + next_mail_time = self.mail_fetcher.last_checked + mail_delta + if self.first_iteration or datetime.datetime.now() > next_mail_time: self.first_iteration = False self.mail_fetcher.pull()