Skip to content

Instantly share code, notes, and snippets.

@jasonleibowitz
Last active January 30, 2024 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonleibowitz/4c141d29cbad315634fb82d4eb7358ab to your computer and use it in GitHub Desktop.
Save jasonleibowitz/4c141d29cbad315634fb82d4eb7358ab to your computer and use it in GitHub Desktop.
Manually Resolve Hanging Workflow
from django.db import transaction
from fund_admin.express.task_management.services import WorkflowService
from fund_admin.express.task_management.constants import WorkflowStatus, WorkflowTemplateName, TaskStatus
from fund_admin.reporting.models.package import FinancialPackageReport
@transaction.atomic
def force_complete_workflow_for_package(package_id):
workflow = WorkflowService().get_active_workflow_for_object(
str(package_id),
FinancialPackageReport._meta.label,
WorkflowTemplateName.PUBLISH_FINANCIAL_PACKAGE.value,
)
tasks_to_cancel = workflow.tasks.filter(status__in=[TaskStatus.PENDING, TaskStatus.ACTIVE])
# First, cancel all outstanding tasks
for task in tasks_to_cancel:
task.cancel()
# Next, mark the workflow as complete
workflow.state = "Completed"
workflow.status = WorkflowStatus.COMPLETE
# Finally, save the workflow
workflow.save()
force_complete_workflow_for_package(package_id=48530)
@jasonleibowitz
Copy link
Author

jasonleibowitz commented Jan 30, 2024

To resolve a situation where a Financial Package was published by the FA after being "Approved, but wait" by the GP. This resulted in the package being published and its status being updated to reflect that, but the package's workflow is still in Approved, waiting to publish.

This Gist will force complete the workflow and cancel any outstanding tasks.

Note: Relevant Confluence Doc

@simranjaising30
Copy link

I have also done something like this in the past

workflow_id = 1236053 last_task = Workflow.objects.get(id=workflow_id)._get_last_task() WorkflowMetric.objects.filter(workflow_id=workflow_id).delete() TaskService().apply_action( action=Action.RECONCILE_TRANSACTION.value, task_id=last_task.id, resolved_by=0, )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment