|
1 | 1 | import json
|
2 | 2 | import logging
|
3 | 3 | import os
|
| 4 | +from contextlib import closing |
4 | 5 |
|
| 6 | +from core.utils.controller import assign_execute_roles |
5 | 7 | from core.utils.controller import build_collection_uri
|
| 8 | +from core.utils.controller import create_execution_environment |
| 9 | +from core.utils.controller import create_job_templates |
| 10 | +from core.utils.controller import create_labels |
| 11 | +from core.utils.controller import create_project |
6 | 12 | from core.utils.controller import download_collection
|
| 13 | +from core.utils.controller import get_http_session |
| 14 | +from core.utils.controller import save_instance_state |
7 | 15 |
|
8 | 16 | from .models import Pattern
|
| 17 | +from .models import PatternInstance |
9 | 18 | from .models import Task
|
10 | 19 |
|
11 | 20 | logger = logging.getLogger(__name__)
|
@@ -55,3 +64,35 @@ def run_pattern_task(pattern_id: int, task_id: int) -> None:
|
55 | 64 | error_message = f"An unexpected error occurred {str(e)}."
|
56 | 65 | logger.exception(f"Task {task_id} failed unexpectedly.")
|
57 | 66 | task.mark_failed({"error": error_message})
|
| 67 | + |
| 68 | + |
| 69 | +def run_pattern_instance_task(instance_id: int, task_id: int) -> None: |
| 70 | + task = Task.objects.get(id=task_id) |
| 71 | + try: |
| 72 | + instance = PatternInstance.objects.select_related("pattern").get(id=instance_id) |
| 73 | + pattern = instance.pattern |
| 74 | + pattern_def = pattern.pattern_definition |
| 75 | + |
| 76 | + if not pattern_def: |
| 77 | + raise ValueError("Pattern definition is missing.") |
| 78 | + |
| 79 | + # Create a single session for all AAP calls |
| 80 | + with closing(get_http_session()) as session: |
| 81 | + task.mark_running({"info": "Creating controller project"}) |
| 82 | + project_id = create_project(session, instance, pattern) |
| 83 | + task.mark_running({"info": "Creating execution environment"}) |
| 84 | + ee_id = create_execution_environment(session, instance, pattern_def) |
| 85 | + task.mark_running({"info": "Creating labels"}) |
| 86 | + labels = create_labels(session, instance, pattern_def) |
| 87 | + task.mark_running({"info": "Creating job templates"}) |
| 88 | + automations = create_job_templates( |
| 89 | + session, instance, pattern_def, project_id, ee_id |
| 90 | + ) |
| 91 | + task.mark_running({"info": "Saving instance"}) |
| 92 | + save_instance_state(instance, project_id, ee_id, labels, automations) |
| 93 | + task.mark_running({"info": "Assigning roles"}) |
| 94 | + assign_execute_roles(session, instance.executors, automations) |
| 95 | + task.mark_completed({"info": "PatternInstance processed"}) |
| 96 | + except Exception as e: |
| 97 | + logger.exception("Failed to process PatternInstance.") |
| 98 | + task.mark_failed({"error": str(e)}) |
0 commit comments