From 5bbaa8a7b1b46ba4fa3bfd99df3e524ff1eab870 Mon Sep 17 00:00:00 2001 From: David Maisonave <47364845+David-Maisonave@users.noreply.github.com> Date: Sat, 10 Aug 2024 00:51:56 -0400 Subject: [PATCH] Added logic for weekly and monthly scheduled reoccurring task --- plugins/FileMonitor/README.md | 2 +- plugins/FileMonitor/filemonitor.py | 25 ++++++++++++++++ plugins/FileMonitor/filemonitor.yml | 2 +- plugins/FileMonitor/filemonitor_config.py | 35 ++++++++++++++++------- 4 files changed, 52 insertions(+), 12 deletions(-) diff --git a/plugins/FileMonitor/README.md b/plugins/FileMonitor/README.md index 9091719..233f079 100644 --- a/plugins/FileMonitor/README.md +++ b/plugins/FileMonitor/README.md @@ -1,4 +1,4 @@ -# FileMonitor: Ver 0.7.1 (By David Maisonave) +# FileMonitor: Ver 0.7.2 (By David Maisonave) FileMonitor is a [Stash](https://github.com/stashapp/stash) plugin which updates Stash if any changes occurs in the Stash library paths. It also has a scheduler which can be used to schedule reoccurring task. diff --git a/plugins/FileMonitor/filemonitor.py b/plugins/FileMonitor/filemonitor.py index 5314bd3..cc89c64 100644 --- a/plugins/FileMonitor/filemonitor.py +++ b/plugins/FileMonitor/filemonitor.py @@ -104,7 +104,15 @@ if plugin.CALLED_AS_STASH_PLUGIN: # Reoccurring scheduler code def runTask(task): + import datetime plugin.Trace(f"Running task {task}") + if 'monthly' in task: + dayOfTheMonth = datetime.datetime.today().day + FirstAllowedDate = ((task['monthly'] - 1) * 7) + 1 + LastAllowedDate = task['monthly'] * 7 + if dayOfTheMonth < FirstAllowedDate or dayOfTheMonth > LastAllowedDate: + plugin.Log(f"Skipping task {task['task']} because today is not the right {task['weekday']} of the month. Target range is between {FirstAllowedDate} and {LastAllowedDate}.") + return if task['task'] == "Clean": plugin.STASH_INTERFACE.metadata_clean(paths=stashPaths, dry_run=plugin.DRY_RUN) elif task['task'] == "Generate": @@ -125,6 +133,7 @@ def reoccurringScheduler(): import schedule # pip install schedule # https://github.com/dbader/schedule # ToDo: Extend schedule class so it works persistently (remember schedule between restarts) # Or replace schedule with apscheduler https://github.com/agronholm/apscheduler + dayOfTheWeek = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"] for task in plugin.pluginConfig['task_reoccurring_scheduler']: if 'days' in task and task['days'] > 0: plugin.Log(f"Adding to reoccurring scheduler task '{task['task']}' at {task['days']} days interval") @@ -135,6 +144,22 @@ def reoccurringScheduler(): elif 'minutes' in task and task['minutes'] > 0: plugin.Log(f"Adding to reoccurring scheduler task '{task['task']}' at {task['minutes']} minutes interval") schedule.every(task['minutes']).minutes.do(runTask, task) + elif 'weekday' in task and task['weekday'].lower() in dayOfTheWeek and 'time' in task: + plugin.Log(f"Adding to reoccurring scheduler task '{task['task']}' (weekly) every {task['weekday']} at {task['time']}") + if task['weekday'].lower() == "monday": + schedule.every().monday.at(task['time']).do(runTask, task) + elif task['weekday'].lower() == "tuesday": + schedule.every().tuesday.at(task['time']).do(runTask, task) + elif task['weekday'].lower() == "wednesday": + schedule.every().wednesday.at(task['time']).do(runTask, task) + elif task['weekday'].lower() == "thursday": + schedule.every().thursday.at(task['time']).do(runTask, task) + elif task['weekday'].lower() == "friday": + schedule.every().friday.at(task['time']).do(runTask, task) + elif task['weekday'].lower() == "saturday": + schedule.every().saturday.at(task['time']).do(runTask, task) + elif task['weekday'].lower() == "sunday": + schedule.every().sunday.at(task['time']).do(runTask, task) def checkSchedulePending(): import schedule # pip install schedule # https://github.com/dbader/schedule schedule.run_pending() diff --git a/plugins/FileMonitor/filemonitor.yml b/plugins/FileMonitor/filemonitor.yml index ef9440f..a762467 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.1 +version: 0.7.2 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 734d2d9..f561239 100644 --- a/plugins/FileMonitor/filemonitor_config.py +++ b/plugins/FileMonitor/filemonitor_config.py @@ -17,21 +17,36 @@ config = { # Enable to run metadata clean task after file deletion. "runCleanAfterDelete": False, - # The scheduler my only work reliably when FileMonitor runs as a service. - # Reoccurring scheduler task list. + # The reoccurring scheduler task list. + # The scheduler may only work reliably when FileMonitor runs as a service. + # Frequency can be in minutes, hours, or days. A zero frequency value disables the task. + # For weekly and monthly task, use the syntax as done in the **Generate** and **Backup** task below. "task_reoccurring_scheduler": [ - # Frequency can be in minutes, hours, or days. - # A zero frequency value disables the task. {"task" : "Clean", "days" : 2}, # Maintenance -> [Clean] (every 2 days) - {"task" : "Generate", "days" : 7}, # Generated Content-> [Generate] (Weekly) - {"task" : "Backup", "days" : 30}, # Backup -> [Backup] (Monthly) - {"task" : "Scan", "days" : 7}, # Library -> [Scan] (Weekly) {"task" : "Auto Tag", "hours" : 24}, # Auto Tag -> [Auto Tag] (Daily) {"task" : "Optimise Database", "hours" : 24}, # Maintenance -> [Optimise Database] (Daily) - {"task" : "Create Tags", "pluginId" : "pathParser", "days" : 1}, # Requires plugin [Path Parser] (Daily) - {"task" : "PluginButtonName_Here", "pluginId" : "PluginId_Here", "hours" : 0}, # Place holder for custom task. - # Add additional task here. + + # The following is the syntax used for plugins. A plugin task requires the plugin name for the [task] field, and the plugin-ID for the [pluginId] field. + {"task" : "Create Tags", "pluginId" : "pathParser", "days" : 0}, # This task requires plugin [Path Parser]. To enable this task change the zero to a positive number. + + # Note: For a weekly task do NOT use days. Instead use the weekday method which is more reliable. The hour section in time MUST be a two digit number, and use military time format. Example: 1PM = "13:00" + {"task" : "Generate", "weekday" : "sunday", "time" : "07:00"}, # Generated Content-> [Generate] (Every Sunday at 7AM) + {"task" : "Scan", "weekday" : "sunday", "time" : "03:00"}, # Library -> [Scan] (Weekly) (Every Sunday at 3AM) + + # To perform a task monthly, specify the day of the month as in the weekly schedule format, and add a monthly field. + # The monthly field value must be 1, 2, 3, or 4. + # 1 = 1st specified weekday of the month. Example 1st monday. + # 2 = 2nd specified weekday of the month. Example 2nd monday of the month. + # 3 = 3rd specified weekday of the month. + # 4 = 4th specified weekday of the month. + # Example monthly method. + {"task" : "Backup", "weekday" : "saturday", "time" : "01:00", "monthly" : 2}, # Backup -> [Backup] 2nd saturday of the month at 1AM + + # 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. + # Add additional plugin task here. ], + # Maximum backups to keep. When scheduler is enabled, and the Backup runs, delete older backups after reaching maximum backups. "BackupsMax" : 6, # Not yet implemented!!!