From c9a958f861a2822a0248167efba82e818a806030 Mon Sep 17 00:00:00 2001 From: Eric Callahan Date: Sun, 13 Jun 2021 09:15:09 -0400 Subject: [PATCH] database: track unsafe shutdowns Keep track of the number of unsafe shutdowns a machine experiences. This data is useful in diagnosing potential file system issues and issues with the datbase itself. Signed-off-by: Eric Callahan --- moonraker/components/database.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/moonraker/components/database.py b/moonraker/components/database.py index 53893e2..f2f8496 100644 --- a/moonraker/components/database.py +++ b/moonraker/components/database.py @@ -104,6 +104,15 @@ class MoonrakerDatabase: debug_counter) if debug_counter: logging.info(f"Database Debug Count: {debug_counter}") + + # Track unsafe shutdowns + unsafe_shutdowns: int = self.get_item( + "moonraker", "database.unsafe_shutdowns", 0) + logging.info(f"Unsafe Shutdown Count: {unsafe_shutdowns}") + # Increment unsafe shutdown counter. This will be reset if + # moonraker is safely restarted + self.insert_item("moonraker", "database.unsafe_shutdowns", + unsafe_shutdowns + 1) self.server.register_endpoint( "/server/database/list", ['GET'], self._handle_list_request) self.server.register_endpoint( @@ -392,6 +401,12 @@ class MoonrakerDatabase: return {'namespace': namespace, 'key': key, 'value': val} def close(self) -> None: + # Decrement unsafe shutdown counter + unsafe_shutdowns: int = self.get_item( + "moonraker", "database.unsafe_shutdowns", 0) + self.insert_item("moonraker", "database.unsafe_shutdowns", + unsafe_shutdowns - 1) + # log db stats msg = "" with self.lmdb_env.begin() as txn: