From 2993b4a691b489f1d7d52a039e402354fec12c18 Mon Sep 17 00:00:00 2001 From: Roman Pertl Date: Thu, 2 Jun 2022 14:54:13 +0200 Subject: [PATCH] fix: notifier state get's never persisted in database - ever time the notification starts, it reports a corrupted state because the state never gets stored in the databse. saving the state in the database using `UPDATE` sql only works when there is already one entry in the table, otherwise the query will run through with zero updated rows. so in the case when the state cannot be loaded, we properly create one record inside the notifier_state table --- src/oncall/notifier/reminder.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/oncall/notifier/reminder.py b/src/oncall/notifier/reminder.py index cf1c41ab..9eb0a6aa 100644 --- a/src/oncall/notifier/reminder.py +++ b/src/oncall/notifier/reminder.py @@ -48,8 +48,12 @@ def reminder(config): cursor.execute('SELECT `last_window_end` FROM `notifier_state`') if cursor.rowcount != 1: window_start = int(time.time() - interval) - logger.warning('Corrupted/missing notifier state; unable to determine last window. Guessing %s', + logger.warning('Corrupted/missing notifier state; unable to determine last window. Guessing %s. Creating state in database', window_start) + # create a clean state in the datebase + cursor.execute('DELETE FROM `notifier_state`') + cursor.execute('INSERT INTO `notifier_state` (`last_window_end`) VALUES (%s) ', window_start) + connection.commit() else: window_start = cursor.fetchone()[0]