diff --git a/plexpy/__init__.py b/plexpy/__init__.py index b96b3c11..95298144 100644 --- a/plexpy/__init__.py +++ b/plexpy/__init__.py @@ -439,6 +439,8 @@ def initialize_scheduler(): backup_hours = CONFIG.BACKUP_INTERVAL if 1 <= CONFIG.BACKUP_INTERVAL <= 24 else 6 + schedule_job(database.optimize, 'Optimize Tautulli database', + hours=24, minutes=0, seconds=0) schedule_job(database.make_backup, 'Backup Tautulli database', hours=backup_hours, minutes=0, seconds=0, args=(True, True)) schedule_job(config.make_backup, 'Backup Tautulli config', diff --git a/plexpy/common.py b/plexpy/common.py index c4a56bdf..6032fd0f 100644 --- a/plexpy/common.py +++ b/plexpy/common.py @@ -226,6 +226,7 @@ SCHEDULER_LIST = [ ('Refresh users list', 'scheduled'), ('Refresh libraries list', 'scheduled'), ('Refresh Plex server URLs', 'scheduled'), + ('Optimize Tautulli database', 'scheduled'), ('Backup Tautulli database', 'scheduled'), ('Backup Tautulli config', 'scheduled') ] diff --git a/plexpy/database.py b/plexpy/database.py index 073e4fdc..c994068b 100644 --- a/plexpy/database.py +++ b/plexpy/database.py @@ -180,7 +180,7 @@ def import_tautulli_db(database=None, method=None, backup=False): for table_name in session_history_tables: db.action('DROP TABLE {table}_copy'.format(table=table_name)) - db.action('VACUUM') + vacuum() logger.info("Tautulli Database :: Tautulli database import complete.") set_is_importing(False) @@ -199,7 +199,7 @@ def clear_table(table=None): logger.debug("Tautulli Database :: Clearing database table '%s'." % table) try: monitor_db.action('DELETE FROM %s' % table) - monitor_db.action('VACUUM') + vacuum() return True except Exception as e: logger.error("Tautulli Database :: Failed to clear database table '%s': %s." % (table, e)) @@ -232,6 +232,7 @@ def delete_rows_from_table(table, row_ids): for row_ids_group in helpers.chunk(row_ids, sqlite_max_variable_number): query = "DELETE FROM " + table + " WHERE id IN (%s) " % ','.join(['?'] * len(row_ids_group)) monitor_db.action(query, row_ids_group) + vacuum() except Exception as e: logger.error("Tautulli Database :: Failed to delete rows from %s database table: %s" % (table, e)) return False @@ -274,6 +275,20 @@ def delete_library_history(section_id=None): return delete_session_history_rows(row_ids=row_ids) +def vacuum(): + monitor_db = MonitorDatabase() + + logger.info("Tautulli Database :: Vacuuming database.") + try: + monitor_db.action('VACUUM') + except Exception as e: + logger.error("Tautulli Database :: Failed to vacuum database: %s" % e) + + +def optimize(): + vacuum() + + def db_filename(filename=FILENAME): """ Returns the filepath to the db """