diff --git a/src/server/BreCal/services/schedule_routines.py b/src/server/BreCal/services/schedule_routines.py index cee2e54..de6ec3b 100644 --- a/src/server/BreCal/services/schedule_routines.py +++ b/src/server/BreCal/services/schedule_routines.py @@ -74,6 +74,24 @@ def UpdateNotifications(): except Exception as ex: logging.error(ex) +def ClearNotifications(): + """ + This function clears all notifications in state ("level") 2 that are older than 3 days + """ + + try: + pooledConnection = getPoolConnection() + commands = pydapper.using(pooledConnection) + + query = "DELETE FROM notification WHERE level = 2 and created < TIMESTAMP(NOW() - INTERVAL 3 DAY)" + result = commands.execute(query) + pooledConnection.close() + if(result > 0): + logging.info(f"Deleted {result} notifications") + + except Exception as ex: + logging.error(ex) + def SendEmails(email_dict): """ This function sends emails to all users in the emaildict @@ -244,6 +262,10 @@ def add_function_to_evaluate_notifications(interval_in_minutes:int=1): schedule.every(interval_in_minutes).minutes.do(UpdateNotifications) return +def add_function_to_clear_notifications(interval_in_minutes:int=60): + schedule.every(interval_in_minutes).minutes.do(ClearNotifications) + return + def add_function_to_schedule_send_notifications(interval_in_minutes:int=10): schedule.every(interval_in_minutes).minutes.do(SendNotifications) return @@ -293,6 +315,8 @@ def setup_schedule(update_shipcalls_interval_in_minutes:int=60): add_function_to_evaluate_notifications() + add_function_to_clear_notifications() + schedule.every().day.at("09:00").do(eval_next_24_hrs) add_function_to_schedule_send_notifications(1)