From 46a598218453787906b14b2239aa002fcadae49b Mon Sep 17 00:00:00 2001 From: Garret Wassermann Date: Mon, 20 Jul 2020 08:05:35 -0400 Subject: [PATCH] ensure log handler is freed, #844 --- helpdesk/management/commands/get_email.py | 29 ++++++++++++++++------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/helpdesk/management/commands/get_email.py b/helpdesk/management/commands/get_email.py index ad127e78..053fceee 100755 --- a/helpdesk/management/commands/get_email.py +++ b/helpdesk/management/commands/get_email.py @@ -101,18 +101,29 @@ def process_email(quiet=False): if quiet: logger.propagate = False # do not propagate to root logger that would log to console logdir = q.logging_dir or '/var/log/helpdesk/' - handler = logging.FileHandler(join(logdir, q.slug + '_get_email.log')) - logger.addHandler(handler) + + try: + handler = logging.FileHandler(join(logdir, q.slug + '_get_email.log')) + logger.addHandler(handler) - if not q.email_box_last_check: - q.email_box_last_check = timezone.now() - timedelta(minutes=30) + if not q.email_box_last_check: + q.email_box_last_check = timezone.now() - timedelta(minutes=30) - queue_time_delta = timedelta(minutes=q.email_box_interval or 0) + queue_time_delta = timedelta(minutes=q.email_box_interval or 0) - if (q.email_box_last_check + queue_time_delta) < timezone.now(): - process_queue(q, logger=logger) - q.email_box_last_check = timezone.now() - q.save() + if (q.email_box_last_check + queue_time_delta) < timezone.now(): + process_queue(q, logger=logger) + q.email_box_last_check = timezone.now() + q.save() + finally: + try: + handler.close() + except Exception as e: + logging.exception(e) + try: + logger.removeHandler(handler) + except Exception as e: + logging.exception(e) def process_queue(q, logger):