fixed interval settings

This commit is contained in:
Daniel Schick 2025-01-13 10:44:37 +01:00
parent 6b173495af
commit ba8778cc3f
2 changed files with 12 additions and 12 deletions

View File

@ -1,7 +1,7 @@
# Version: 1.7.0 # Version: 1.7.0
# Constants for the notification system # Constants for the notification system
NOTIFICATION_COOLDOWN = 600 # 10 minutes until a notification gets real NOTIFICATION_COOLDOWN_MINS = 10 # until a notification gets real and cannot be deleted anymore
NOTIFICATION_MAX_AGE_DAYS = 3 # 3 days until a notification gets deleted NOTIFICATION_MAX_AGE_DAYS = 3 # 3 days until a notification gets deleted
# Placeholder for the email credentials filled by startup logic # Placeholder for the email credentials filled by startup logic

View File

@ -55,7 +55,7 @@ def UpdateShipcalls(options:dict = {'past_days':2}):
logging.error(ex) logging.error(ex)
return return
def UpdateNotifications(): def UpdateNotifications(cooldown_in_mins:int=10):
""" """
This function evaluates all notifications in state ("level") 0 which have been recently created. If a specified amount of time has passed the This function evaluates all notifications in state ("level") 0 which have been recently created. If a specified amount of time has passed the
notification is updated to state 1 and a notification is received by the user notification is updated to state 1 and a notification is received by the user
@ -65,7 +65,7 @@ def UpdateNotifications():
pooledConnection = getPoolConnection() pooledConnection = getPoolConnection()
commands = pydapper.using(pooledConnection) commands = pydapper.using(pooledConnection)
query = "SELECT * FROM notification WHERE level = 0 AND created < TIMESTAMP(NOW() - INTERVAL 10 MINUTE)" query = f"SELECT * FROM notification WHERE level = 0 AND created < TIMESTAMP(NOW() - INTERVAL {cooldown_in_mins} MINUTE)"
data = commands.query(query, model=model.Notification) data = commands.query(query, model=model.Notification)
for notification in data: for notification in data:
commands.execute("UPDATE notification SET level = 1 WHERE id = ?id?", param={"id":notification.id}) commands.execute("UPDATE notification SET level = 1 WHERE id = ?id?", param={"id":notification.id})
@ -74,16 +74,16 @@ def UpdateNotifications():
except Exception as ex: except Exception as ex:
logging.error(ex) logging.error(ex)
def ClearNotifications(): def ClearNotifications(max_age_in_days:int=3):
""" """
This function clears all notifications in state ("level") 2 that are older than 3 days This function clears all notifications in state ("level") 2 that are older than x days
""" """
try: try:
pooledConnection = getPoolConnection() pooledConnection = getPoolConnection()
commands = pydapper.using(pooledConnection) commands = pydapper.using(pooledConnection)
query = "DELETE FROM notification WHERE level = 2 and created < TIMESTAMP(NOW() - INTERVAL 3 DAY)" query = f"DELETE FROM notification WHERE level = 2 and created < TIMESTAMP(NOW() - INTERVAL {max_age_in_days} DAY)"
result = commands.execute(query) result = commands.execute(query)
pooledConnection.close() pooledConnection.close()
if(result > 0): if(result > 0):
@ -259,14 +259,14 @@ def add_function_to_schedule__update_shipcalls(interval_in_minutes:int, options:
return return
def add_function_to_evaluate_notifications(interval_in_minutes:int=1): def add_function_to_evaluate_notifications(interval_in_minutes:int=1):
schedule.every(interval_in_minutes).minutes.do(UpdateNotifications) schedule.every(1).minutes.do(UpdateNotifications, interval_in_minutes)
return return
def add_function_to_clear_notifications(interval_in_minutes:int=60): def add_function_to_clear_notifications(interval_in_days:int=3):
schedule.every(interval_in_minutes).minutes.do(ClearNotifications) schedule.every(30).minutes.do(ClearNotifications, interval_in_days)
return return
def add_function_to_schedule_send_notifications(interval_in_minutes:int=10): def add_function_to_schedule_send_notifications(interval_in_minutes:int=1):
schedule.every(interval_in_minutes).minutes.do(SendNotifications) schedule.every(interval_in_minutes).minutes.do(SendNotifications)
return return
@ -313,9 +313,9 @@ def setup_schedule(update_shipcalls_interval_in_minutes:int=60):
# update the evaluation state in every recent shipcall # update the evaluation state in every recent shipcall
add_function_to_schedule__update_shipcalls(update_shipcalls_interval_in_minutes) add_function_to_schedule__update_shipcalls(update_shipcalls_interval_in_minutes)
add_function_to_evaluate_notifications() add_function_to_evaluate_notifications(defs.NOTIFICATION_COOLDOWN_MINS)
add_function_to_clear_notifications() add_function_to_clear_notifications(defs.NOTIFICATION_MAX_AGE_DAYS)
schedule.every().day.at("09:00").do(eval_next_24_hrs) schedule.every().day.at("09:00").do(eval_next_24_hrs)