From f8626633cfcaf2c31304cb18371b768e09506a49 Mon Sep 17 00:00:00 2001 From: amcmanu3 Date: Sat, 25 May 2024 13:51:40 -0400 Subject: [PATCH] Add action ID option to schedules --- .../controllers/management_controller.py | 2 + app/classes/models/management.py | 4 ++ app/classes/shared/tasks.py | 1 + app/migrations/20240308_multi-backup.py | 58 ++++++++++++++++++- 4 files changed, 64 insertions(+), 1 deletion(-) diff --git a/app/classes/controllers/management_controller.py b/app/classes/controllers/management_controller.py index fc661e84..6f4da3cb 100644 --- a/app/classes/controllers/management_controller.py +++ b/app/classes/controllers/management_controller.py @@ -123,6 +123,7 @@ class ManagementController: cron_string="* * * * *", parent=None, delay=0, + action_id=None, ): return HelpersManagement.create_scheduled_task( server_id, @@ -137,6 +138,7 @@ class ManagementController: cron_string, parent, delay, + action_id, ) @staticmethod diff --git a/app/classes/models/management.py b/app/classes/models/management.py index 3390e428..24505471 100644 --- a/app/classes/models/management.py +++ b/app/classes/models/management.py @@ -89,6 +89,7 @@ class Schedules(BaseModel): interval_type = CharField() start_time = CharField(null=True) command = CharField(null=True) + action_id = CharField(null=True) name = CharField() one_time = BooleanField(default=False) cron_string = CharField(default="") @@ -114,6 +115,7 @@ class Backups(BaseModel): shutdown = BooleanField(default=False) before = CharField(default="") after = CharField(default="") + enabled = BooleanField(default=True) class Meta: table_name = "backups" @@ -268,6 +270,7 @@ class HelpersManagement: cron_string="* * * * *", parent=None, delay=0, + action_id=None, ): sch_id = Schedules.insert( { @@ -278,6 +281,7 @@ class HelpersManagement: Schedules.interval_type: interval_type, Schedules.start_time: start_time, Schedules.command: command, + Schedules.action_id: action_id, Schedules.name: name, Schedules.one_time: one_time, Schedules.cron_string: cron_string, diff --git a/app/classes/shared/tasks.py b/app/classes/shared/tasks.py index b9513441..57e3c4d0 100644 --- a/app/classes/shared/tasks.py +++ b/app/classes/shared/tasks.py @@ -337,6 +337,7 @@ class TasksManager: job_data["cron_string"], job_data["parent"], job_data["delay"], + job_data.get("action_id", None), ) # Checks to make sure some doofus didn't actually make the newly diff --git a/app/migrations/20240308_multi-backup.py b/app/migrations/20240308_multi-backup.py index 85106e16..4aa854b9 100644 --- a/app/migrations/20240308_multi-backup.py +++ b/app/migrations/20240308_multi-backup.py @@ -5,7 +5,7 @@ import logging from app.classes.shared.console import Console from app.classes.shared.migration import Migrator, MigrateHistory -from app.classes.models.management import Backups +from app.classes.models.management import Backups, Schedules logger = logging.getLogger(__name__) @@ -19,6 +19,10 @@ def migrate(migrator: Migrator, database, **kwargs): migrator.add_columns("backups", backup_id=peewee.UUIDField(default=uuid.uuid4)) migrator.add_columns("backups", backup_name=peewee.CharField(default="Default")) migrator.add_columns("backups", backup_location=peewee.CharField(default="")) + migrator.add_columns("backups", enabled=peewee.BooleanField(default=True)) + migrator.add_columns( + "schedules", action_id=peewee.CharField(null=True, default=None) + ) class Servers(peewee.Model): server_id = peewee.CharField(primary_key=True, default=str(uuid.uuid4())) @@ -58,12 +62,34 @@ def migrate(migrator: Migrator, database, **kwargs): shutdown = peewee.BooleanField(default=False) before = peewee.CharField(default="") after = peewee.CharField(default="") + enabled = peewee.BooleanField(default=True) class Meta: table_name = "new_backups" database = db + class NewSchedules(peewee.Model): + schedule_id = peewee.IntegerField(unique=True, primary_key=True) + server_id = peewee.ForeignKeyField(Servers, backref="schedule_server") + enabled = peewee.BooleanField() + action = peewee.CharField() + interval = peewee.IntegerField() + interval_type = peewee.CharField() + start_time = peewee.CharField(null=True) + command = peewee.CharField(null=True) + action_id = peewee.CharField(null=True) + name = peewee.CharField() + one_time = peewee.BooleanField(default=False) + cron_string = peewee.CharField(default="") + parent = peewee.IntegerField(null=True) + delay = peewee.IntegerField(default=0) + next_run = peewee.CharField(default="") + + class Meta: + table_name = "new_schedules" + migrator.create_table(NewBackups) + migrator.create_table(NewSchedules) migrator.run() @@ -83,6 +109,7 @@ def migrate(migrator: Migrator, database, **kwargs): shutdown=backup.shutdown, before=backup.before, after=backup.after, + enabled=True, ) # Drop the existing backups table @@ -92,6 +119,35 @@ def migrate(migrator: Migrator, database, **kwargs): migrator.rename_table("new_backups", "backups") migrator.drop_columns("servers", ["backup_path"]) + for schedule in Schedules.select(): + action_id = None + if schedule.command == "backup_server": + backup = NewBackups.get(NewBackups.server_id == schedule.server_id) + action_id = backup.backup_id + NewSchedules.create( + schedule_id=schedule.schedule_id, + server_id=schedule.server_id, + enabled=schedule.enabled, + action=schedule.action, + interval=schedule.interval, + interval_type=schedule.interval_type, + start_time=schedule.start_time, + command=schedule.command, + action_id=action_id, + name=schedule.name, + one_time=schedule.one_time, + cron_string=schedule.cron_string, + parent=schedule.parent, + delay=schedule.delay, + next_run=schedule.next_run, + ) + + # Drop the existing backups table + migrator.drop_table("schedules") + + # Rename the new table to backups + migrator.rename_table("new_schedules", "schedules") + def rollback(migrator: Migrator, database, **kwargs): """