Clear notifications from the database that are more than 3 days in the past

This commit is contained in:
Daniel Schick 2025-01-13 09:45:56 +01:00
parent cda3f231a7
commit 6b173495af

View File

@ -74,6 +74,24 @@ def UpdateNotifications():
except Exception as ex: except Exception as ex:
logging.error(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): def SendEmails(email_dict):
""" """
This function sends emails to all users in the emaildict 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) schedule.every(interval_in_minutes).minutes.do(UpdateNotifications)
return 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): def add_function_to_schedule_send_notifications(interval_in_minutes:int=10):
schedule.every(interval_in_minutes).minutes.do(SendNotifications) schedule.every(interval_in_minutes).minutes.do(SendNotifications)
return return
@ -293,6 +315,8 @@ def setup_schedule(update_shipcalls_interval_in_minutes:int=60):
add_function_to_evaluate_notifications() add_function_to_evaluate_notifications()
add_function_to_clear_notifications()
schedule.every().day.at("09:00").do(eval_next_24_hrs) schedule.every().day.at("09:00").do(eval_next_24_hrs)
add_function_to_schedule_send_notifications(1) add_function_to_schedule_send_notifications(1)