From 6fad14ae6fe5643638bb3d12e1b2bf81c363c740 Mon Sep 17 00:00:00 2001 From: jasquat <2487833+jasquat@users.noreply.github.com> Date: Tue, 18 Jun 2024 16:54:09 -0400 Subject: [PATCH] do not try to queue task if the pi cannot be found and delete future tasks if parent task deleted w/ burnettk (#1761) Co-authored-by: jasquat --- .../migrations/versions/7eaec0e12079_.py | 32 +++++++++++++++++++ .../background_processing_service.py | 2 +- .../models/future_task.py | 5 +-- 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 spiffworkflow-backend/migrations/versions/7eaec0e12079_.py diff --git a/spiffworkflow-backend/migrations/versions/7eaec0e12079_.py b/spiffworkflow-backend/migrations/versions/7eaec0e12079_.py new file mode 100644 index 00000000..47d39f70 --- /dev/null +++ b/spiffworkflow-backend/migrations/versions/7eaec0e12079_.py @@ -0,0 +1,32 @@ +"""empty message + +Revision ID: 7eaec0e12079 +Revises: 43afc70a7016 +Create Date: 2024-06-18 16:45:06.102210 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '7eaec0e12079' +down_revision = '43afc70a7016' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('future_task', schema=None) as batch_op: + batch_op.create_foreign_key('future_task_task_guid_fk', 'task', ['guid'], ['guid'], ondelete='CASCADE') + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('future_task', schema=None) as batch_op: + batch_op.drop_constraint('future_task_task_guid_fk', type_='foreignkey') + + # ### end Alembic commands ### diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/background_processing/background_processing_service.py b/spiffworkflow-backend/src/spiffworkflow_backend/background_processing/background_processing_service.py index 9a8dfa82..c8c565d1 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/background_processing/background_processing_service.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/background_processing/background_processing_service.py @@ -80,7 +80,7 @@ class BackgroundProcessingService: .filter(TaskModel.guid == future_task.guid) .first() ) - if process_instance.allowed_to_run(): + if process_instance and process_instance.allowed_to_run(): queue_future_task_if_appropriate( process_instance, eta_in_seconds=future_task.run_at_in_seconds, task_guid=future_task.guid ) diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/models/future_task.py b/spiffworkflow-backend/src/spiffworkflow_backend/models/future_task.py index 16d45355..cda4f367 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/models/future_task.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/models/future_task.py @@ -2,6 +2,7 @@ import time from dataclasses import dataclass from flask import current_app +from sqlalchemy import ForeignKey from sqlalchemy.dialects.mysql import insert as mysql_insert from sqlalchemy.dialects.postgresql import insert as postgres_insert from sqlalchemy.dialects.sqlite import insert as sqlite_insert @@ -9,17 +10,17 @@ from sqlalchemy.sql import false from spiffworkflow_backend.models.db import SpiffworkflowBaseDBModel from spiffworkflow_backend.models.db import db +from spiffworkflow_backend.models.task import TaskModel # noqa: F401 @dataclass class FutureTaskModel(SpiffworkflowBaseDBModel): __tablename__ = "future_task" - guid: str = db.Column(db.String(36), primary_key=True) + guid: str = db.Column(ForeignKey(TaskModel.guid, ondelete="CASCADE", name="future_task_task_guid_fk"), primary_key=True) run_at_in_seconds: int = db.Column(db.Integer, nullable=False, index=True) completed: bool = db.Column(db.Boolean, default=False, nullable=False, index=True) archived_for_process_instance_status: bool = db.Column( - # db.Boolean, default=False, server_default=db.sql.False_(), nullable=False, index=True db.Boolean, default=False, server_default=false(),