Added logic for weekly and monthly scheduled reoccurring task

This commit is contained in:
David Maisonave
2024-08-10 00:51:56 -04:00
parent 51898fa817
commit 5bbaa8a7b1
4 changed files with 52 additions and 12 deletions

View File

@@ -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. 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. It also has a scheduler which can be used to schedule reoccurring task.

View File

@@ -104,7 +104,15 @@ if plugin.CALLED_AS_STASH_PLUGIN:
# Reoccurring scheduler code # Reoccurring scheduler code
def runTask(task): def runTask(task):
import datetime
plugin.Trace(f"Running task {task}") 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": if task['task'] == "Clean":
plugin.STASH_INTERFACE.metadata_clean(paths=stashPaths, dry_run=plugin.DRY_RUN) plugin.STASH_INTERFACE.metadata_clean(paths=stashPaths, dry_run=plugin.DRY_RUN)
elif task['task'] == "Generate": elif task['task'] == "Generate":
@@ -125,6 +133,7 @@ def reoccurringScheduler():
import schedule # pip install schedule # https://github.com/dbader/schedule import schedule # pip install schedule # https://github.com/dbader/schedule
# ToDo: Extend schedule class so it works persistently (remember schedule between restarts) # ToDo: Extend schedule class so it works persistently (remember schedule between restarts)
# Or replace schedule with apscheduler https://github.com/agronholm/apscheduler # 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']: for task in plugin.pluginConfig['task_reoccurring_scheduler']:
if 'days' in task and task['days'] > 0: if 'days' in task and task['days'] > 0:
plugin.Log(f"Adding to reoccurring scheduler task '{task['task']}' at {task['days']} days interval") 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: elif 'minutes' in task and task['minutes'] > 0:
plugin.Log(f"Adding to reoccurring scheduler task '{task['task']}' at {task['minutes']} minutes interval") plugin.Log(f"Adding to reoccurring scheduler task '{task['task']}' at {task['minutes']} minutes interval")
schedule.every(task['minutes']).minutes.do(runTask, task) 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(): def checkSchedulePending():
import schedule # pip install schedule # https://github.com/dbader/schedule import schedule # pip install schedule # https://github.com/dbader/schedule
schedule.run_pending() schedule.run_pending()

View File

@@ -1,6 +1,6 @@
name: FileMonitor name: FileMonitor
description: Monitors the Stash library folders, and updates Stash if any changes occurs in the Stash library paths. 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 url: https://github.com/David-Maisonave/Axter-Stash/tree/main/plugins/FileMonitor
settings: settings:
recursiveDisabled: recursiveDisabled:

View File

@@ -17,21 +17,36 @@ config = {
# Enable to run metadata clean task after file deletion. # Enable to run metadata clean task after file deletion.
"runCleanAfterDelete": False, "runCleanAfterDelete": False,
# The scheduler my only work reliably when FileMonitor runs as a service. # The reoccurring scheduler task list.
# 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": [ "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" : "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" : "Auto Tag", "hours" : 24}, # Auto Tag -> [Auto Tag] (Daily)
{"task" : "Optimise Database", "hours" : 24}, # Maintenance -> [Optimise Database] (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. # 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.
# Add additional task here. {"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. # 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" : 6, # Not yet implemented!!!