From 1c1ebe118b7484c630b1c982246ef4550b1c4780 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Ja=C5=A1ek?= Date: Mon, 20 Nov 2023 09:58:02 +0100 Subject: [PATCH] avoid unique error messages (#658) * avoid unique error messages so it won't create a new error on sentry every time but can aggregate those together. * add user id to notification --- newsroom/company_expiry_alerts.py | 9 ++++----- newsroom/monitoring/email_alerts.py | 9 ++++----- .../send_scheduled_notifications.py | 16 ++++++++-------- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/newsroom/company_expiry_alerts.py b/newsroom/company_expiry_alerts.py index 5ed287a04..a4d931e61 100644 --- a/newsroom/company_expiry_alerts.py +++ b/newsroom/company_expiry_alerts.py @@ -15,7 +15,7 @@ from superdesk import get_resource_service from superdesk.utc import utcnow from superdesk.celery_task_utils import get_lock_id -from superdesk.lock import lock, unlock, remove_locks +from superdesk.lock import lock, unlock from newsroom.celery_app import celery from newsroom.settings import get_settings_collection, GENERAL_SETTINGS_LOOKUP @@ -31,16 +31,15 @@ def send_alerts(self): lock_name = get_lock_id("newsroom", "company_expiry") if not lock(lock_name, expire=610): - logger.error("{} Job already running".format(self.log_msg)) + logger.error("Company expiry alert task already running") return try: self.worker() except Exception as e: logger.exception(e) - - unlock(lock_name) - remove_locks() + finally: + unlock(lock_name) logger.info("{} Completed sending alerts.".format(self.log_msg)) diff --git a/newsroom/monitoring/email_alerts.py b/newsroom/monitoring/email_alerts.py index fbec0385e..d4490ef1e 100644 --- a/newsroom/monitoring/email_alerts.py +++ b/newsroom/monitoring/email_alerts.py @@ -19,7 +19,7 @@ from superdesk import get_resource_service, Command from superdesk.utc import utcnow, utc_to_local, local_to_utc from superdesk.celery_task_utils import get_lock_id -from superdesk.lock import lock, unlock, remove_locks +from superdesk.lock import lock, unlock from newsroom.celery_app import celery from newsroom.settings import get_settings_collection, GENERAL_SETTINGS_LOOKUP @@ -48,7 +48,7 @@ def run(self, immediate=False): "monitoring_{0}".format("scheduled" if not immediate else "immediate"), ) if not lock(lock_name, expire=610): - logger.error("{} Job already running".format(self.log_msg)) + logger.error("Monitoring email alerts task already running") return try: @@ -64,9 +64,8 @@ def run(self, immediate=False): self.scheduled_worker(now_to_minute) except Exception as e: logger.exception(e) - - unlock(lock_name) - remove_locks() + finally: + unlock(lock_name) logger.info("{} Completed sending Monitoring Scheduled Alerts.".format(self.log_msg)) diff --git a/newsroom/notifications/send_scheduled_notifications.py b/newsroom/notifications/send_scheduled_notifications.py index 10164200f..e21ee7713 100644 --- a/newsroom/notifications/send_scheduled_notifications.py +++ b/newsroom/notifications/send_scheduled_notifications.py @@ -8,7 +8,7 @@ from superdesk import get_resource_service, Command from superdesk.utc import utcnow, utc_to_local from superdesk.celery_task_utils import get_lock_id -from superdesk.lock import lock, unlock, remove_locks +from superdesk.lock import lock, unlock from newsroom.types import User, NotificationSchedule, Company, NotificationQueue, NotificationQueueTopic, Topic from newsroom.utils import get_user_dict, get_company_dict @@ -32,13 +32,13 @@ def run(self, force: bool = False): lock_name = get_lock_id("newsroom", "send_scheduled_notifications") if not lock(lock_name, expire=610): - logger.error(f"{self.log_msg} Job already running") + logger.error("Send scheduled notifications task already running") return - self.run_schedules(force) - - unlock(lock_name) - remove_locks() + try: + self.run_schedules(force) + finally: + unlock(lock_name) logger.info(f"{self.log_msg} Completed sending scheduled notifications") @@ -51,8 +51,8 @@ def run_schedules(self, force: bool): schedules: List[NotificationQueue] = get_resource_service("notification_queue").get(req=None, lookup={}) except Exception as e: - logger.error(f"{self.log_msg} Failed to retrieve data to run schedules") logger.exception(e) + logger.error("Failed to retrieve data to run schedules") return for schedule in schedules: @@ -76,7 +76,7 @@ def run_schedules(self, force: bool): self.process_schedule(schedule, user, company, now_utc, user_topic_map.get(user["_id"]) or {}, force) except Exception as e: logger.exception(e) - logger.error(f"{self.log_msg} Failed to run schedule for user {user_id}") + logger.error("Failed to run schedule for user %s", user_id) def process_schedule( self,