From a3ace997566ad6cb233001dfb3fc25c4829e063b Mon Sep 17 00:00:00 2001 From: David Maisonave <47364845+David-Maisonave@users.noreply.github.com> Date: Sun, 11 Aug 2024 15:48:49 -0400 Subject: [PATCH] Added logic to trim DB files --- plugins/FileMonitor/README.md | 2 +- plugins/FileMonitor/filemonitor.py | 25 ++++++++++++++++++++--- plugins/FileMonitor/filemonitor.yml | 2 +- plugins/FileMonitor/filemonitor_config.py | 9 ++++---- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/plugins/FileMonitor/README.md b/plugins/FileMonitor/README.md index 51d244c..3e4da37 100644 --- a/plugins/FileMonitor/README.md +++ b/plugins/FileMonitor/README.md @@ -1,4 +1,4 @@ -# FileMonitor: Ver 0.7.5 (By David Maisonave) +# FileMonitor: Ver 0.7.7 (By David Maisonave) FileMonitor is a [Stash](https://github.com/stashapp/stash) plugin with the following two main features: - Updates Stash when any file changes occurs in the Stash library. - Runs scheduled task based on the scheduler configuration in filemonitor_config.py. diff --git a/plugins/FileMonitor/filemonitor.py b/plugins/FileMonitor/filemonitor.py index 4748097..efd6bd4 100644 --- a/plugins/FileMonitor/filemonitor.py +++ b/plugins/FileMonitor/filemonitor.py @@ -106,6 +106,16 @@ def isJobWaitingToRun(): if plugin.CALLED_AS_STASH_PLUGIN: plugin.Trace(f"isJobWaitingToRun() = {isJobWaitingToRun()})") +def trimDbFiles(dbPath, maxFiles): + if not os.path.exists(dbPath) or len(dbPath) < 5: # For safety and security, short path not supported. + return + dbFiles = sorted(os.listdir(dbPath)) + n = len(dbFiles) + for i in range(0, n-maxFiles): + dbFilePath = f"{dbPath}{os.sep}{dbFiles[i]}" + plugin.Log(f"Removing file {dbFilePath}") + os.remove(dbFilePath) + # Reoccurring scheduler code # ToDo: Change the following functions into a class called reoccurringScheduler def runTask(task): @@ -123,7 +133,11 @@ def runTask(task): elif task['task'] == "Generate": plugin.STASH_INTERFACE.metadata_generate() elif task['task'] == "Backup": - plugin.STASH_INTERFACE.call_GQL("mutation { backupDatabase(input: {download: false})}") + plugin.LogOnce("Note: Backup task does not get listed in the Task Queue, but user can verify that it started by looking in the Stash log file as an INFO level log line.") + plugin.STASH_INTERFACE.backup_database() + if plugin.pluginConfig['BackupsMax'] > 0 and plugin.pluginConfig['BackupDatabasePath'] != "" and os.path.exists(plugin.pluginConfig['BackupDatabasePath']): + plugin.Log("Checking quantity of DB backups.") + trimDbFiles(plugin.pluginConfig['BackupDatabasePath'], plugin.pluginConfig['BackupsMax']) elif task['task'] == "Scan": plugin.STASH_INTERFACE.metadata_scan(paths=stashPaths) elif task['task'] == "Auto Tag": @@ -281,9 +295,14 @@ def start_library_monitor(): break if plugin.pluginSettings['turnOnScheduler']: checkSchedulePending() - plugin.LogOnce("Wait start") + plugin.LogOnce("Waiting for a file change-trigger.") signal.wait(timeout=SIGNAL_TIMEOUT) - plugin.Trace("Wait end") + if plugin.pluginSettings['turnOnScheduler'] and not shouldUpdate: + plugin.Trace("Checking the scheduler.") + elif shouldUpdate: + plugin.Trace("File change trigger occurred.") + else: + plugin.Trace("Wait timeourt occurred.") shouldUpdate = False TmpTargetPaths = [] for TargetPath in TargetPaths: diff --git a/plugins/FileMonitor/filemonitor.yml b/plugins/FileMonitor/filemonitor.yml index 1a47275..ac432c4 100644 --- a/plugins/FileMonitor/filemonitor.yml +++ b/plugins/FileMonitor/filemonitor.yml @@ -1,6 +1,6 @@ name: FileMonitor description: Monitors the Stash library folders, and updates Stash if any changes occurs in the Stash library paths. -version: 0.7.5 +version: 0.7.7 url: https://github.com/David-Maisonave/Axter-Stash/tree/main/plugins/FileMonitor settings: recursiveDisabled: diff --git a/plugins/FileMonitor/filemonitor_config.py b/plugins/FileMonitor/filemonitor_config.py index ea7784d..9d56b17 100644 --- a/plugins/FileMonitor/filemonitor_config.py +++ b/plugins/FileMonitor/filemonitor_config.py @@ -8,8 +8,8 @@ config = { "onAnyEvent": False, # Enable to monitor changes in file system for modification flag. This option is NOT needed for Windows, because on Windows changes are triggered via CREATE, DELETE, and MOVE flags. Other OS may differ. "scanModified": False, - # Timeout in seconds. This is how often it will check if another job (Task) is in the queue. - "timeOut": 60, # Not needed when running in command line mode. + # Timeout in seconds. This is how often it will check the scheduler and (in-plugin mode) if another job (Task) is in the queue + "timeOut": 60, # Enable to exit FileMonitor by creating special file in plugin folder\working "createSpecFileToExit": True, # Enable to delete special file imediately after it's created in stop process @@ -40,7 +40,7 @@ config = { # 3 = 3rd specified weekday of the month. # 4 = 4th specified weekday of the month. # Example monthly method. - {"task" : "Backup", "weekday" : "saturday", "time" : "02:30", "monthly" : 2}, # Backup -> [Backup] 2nd saturday of the month at 2:30AM + {"task" : "Backup", "weekday" : "sunday", "time" : "01:00", "monthly" : 2}, # Backup -> [Backup] 2nd sunday of the month at 1AM (01:00) # The following is a place holder for a plugin. {"task" : "PluginButtonName_Here", "pluginId" : "PluginId_Here", "hours" : 0}, # The zero frequency value makes this task disabled. @@ -48,7 +48,8 @@ config = { ], # Maximum backups to keep. When scheduler is enabled, and the Backup runs, delete older backups after reaching maximum backups. - "BackupsMax" : 6, # Not yet implemented!!! + "BackupsMax" : 12, # Only works if BackupDatabasePath is properly populated. + "BackupDatabasePath" : "C:\\Users\\admin3\\.stash\\DbBackup", # ToDo: Implement code to automate fetching this value # When enabled, if CREATE flag is triggered, DupFileManager task is called if the plugin is installed. "onCreateCallDupFileManager": False, # Not yet implemented!!!!