|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from datetime import datetime |
| 4 | +from http import HTTPStatus |
| 5 | +from typing import Annotated, Any |
| 6 | + |
| 7 | +from fastapi import Depends, HTTPException, status |
| 8 | + |
| 9 | +from diracx.core.exceptions import JobNotFoundError, PilotCantAccessJobError |
| 10 | +from diracx.core.models import ( |
| 11 | + HeartbeatData, |
| 12 | + JobCommand, |
| 13 | + JobStatusUpdate, |
| 14 | + SetJobStatusReturn, |
| 15 | +) |
| 16 | +from diracx.logic.jobs.status import add_heartbeat as add_heartbeat_bl |
| 17 | +from diracx.logic.jobs.status import get_job_commands as get_job_commands_bl |
| 18 | +from diracx.logic.jobs.status import ( |
| 19 | + set_job_parameters_or_attributes as set_job_parameters_or_attributes_bl, |
| 20 | +) |
| 21 | +from diracx.logic.jobs.status import set_job_statuses as set_job_statuses_bl |
| 22 | +from diracx.routers.utils.pilots import ( |
| 23 | + AuthorizedPilotInfo, |
| 24 | + verify_dirac_pilot_access_token, |
| 25 | + verify_that_pilot_can_access_jobs, |
| 26 | +) |
| 27 | + |
| 28 | +from ..dependencies import ( |
| 29 | + Config, |
| 30 | + JobDB, |
| 31 | + JobLoggingDB, |
| 32 | + JobParametersDB, |
| 33 | + PilotAgentsDB, |
| 34 | + TaskQueueDB, |
| 35 | +) |
| 36 | +from ..fastapi_classes import DiracxRouter |
| 37 | + |
| 38 | +router = DiracxRouter() |
| 39 | + |
| 40 | + |
| 41 | +@router.patch("/status") |
| 42 | +async def pilot_set_job_statuses( |
| 43 | + job_update: dict[int, dict[datetime, JobStatusUpdate]], |
| 44 | + config: Config, |
| 45 | + job_db: JobDB, |
| 46 | + job_logging_db: JobLoggingDB, |
| 47 | + task_queue_db: TaskQueueDB, |
| 48 | + job_parameters_db: JobParametersDB, |
| 49 | + pilot_agents_db: PilotAgentsDB, |
| 50 | + pilot_info: Annotated[ |
| 51 | + AuthorizedPilotInfo, Depends(verify_dirac_pilot_access_token) |
| 52 | + ], |
| 53 | + force: bool = False, |
| 54 | +) -> SetJobStatusReturn: |
| 55 | + # Endpoint only for DiracX pilots (with a pilot token) |
| 56 | + try: |
| 57 | + await verify_that_pilot_can_access_jobs( |
| 58 | + pilot_db=pilot_agents_db, |
| 59 | + pilot_stamp=pilot_info.pilot_stamp, |
| 60 | + job_ids=list(job_update), |
| 61 | + ) |
| 62 | + except (PilotCantAccessJobError, JobNotFoundError) as e: |
| 63 | + raise HTTPException( |
| 64 | + status_code=status.HTTP_403_FORBIDDEN, detail="Pilot can't access this job." |
| 65 | + ) from e |
| 66 | + |
| 67 | + try: |
| 68 | + result = await set_job_statuses_bl( |
| 69 | + status_changes=job_update, |
| 70 | + config=config, |
| 71 | + job_db=job_db, |
| 72 | + job_logging_db=job_logging_db, |
| 73 | + task_queue_db=task_queue_db, |
| 74 | + job_parameters_db=job_parameters_db, |
| 75 | + force=force, |
| 76 | + ) |
| 77 | + except ValueError as e: |
| 78 | + raise HTTPException( |
| 79 | + status_code=HTTPStatus.BAD_REQUEST, |
| 80 | + detail=str(e), |
| 81 | + ) from e |
| 82 | + |
| 83 | + if not result.success: |
| 84 | + raise HTTPException( |
| 85 | + status_code=HTTPStatus.NOT_FOUND, |
| 86 | + detail=result.model_dump(), |
| 87 | + ) |
| 88 | + |
| 89 | + return result |
| 90 | + |
| 91 | + |
| 92 | +@router.patch("/heartbeat") |
| 93 | +async def pilot_add_heartbeat( |
| 94 | + data: dict[int, HeartbeatData], |
| 95 | + config: Config, |
| 96 | + job_db: JobDB, |
| 97 | + job_logging_db: JobLoggingDB, |
| 98 | + task_queue_db: TaskQueueDB, |
| 99 | + job_parameters_db: JobParametersDB, |
| 100 | + pilot_agents_db: PilotAgentsDB, |
| 101 | + pilot_info: Annotated[ |
| 102 | + AuthorizedPilotInfo, Depends(verify_dirac_pilot_access_token) |
| 103 | + ], |
| 104 | +) -> list[JobCommand]: |
| 105 | + """Register a heartbeat from the job. |
| 106 | +
|
| 107 | + This endpoint is used by the JobAgent to send heartbeats to the WMS and to |
| 108 | + receive job commands from the WMS. It also results in stalled jobs being |
| 109 | + restored to the RUNNING status. |
| 110 | +
|
| 111 | + The `data` parameter and return value are mappings keyed by job ID. |
| 112 | + """ |
| 113 | + # Endpoint only for DiracX pilots (with a pilot token) |
| 114 | + try: |
| 115 | + await verify_that_pilot_can_access_jobs( |
| 116 | + pilot_db=pilot_agents_db, |
| 117 | + pilot_stamp=pilot_info.pilot_stamp, |
| 118 | + job_ids=list(data), |
| 119 | + ) |
| 120 | + except (PilotCantAccessJobError, JobNotFoundError) as e: |
| 121 | + raise HTTPException( |
| 122 | + status_code=status.HTTP_403_FORBIDDEN, detail="Pilot can't access this job." |
| 123 | + ) from e |
| 124 | + |
| 125 | + await add_heartbeat_bl( |
| 126 | + data, config, job_db, job_logging_db, task_queue_db, job_parameters_db |
| 127 | + ) |
| 128 | + return await get_job_commands_bl(data, job_db) |
| 129 | + |
| 130 | + |
| 131 | +@router.patch("/metadata", status_code=HTTPStatus.NO_CONTENT) |
| 132 | +async def pilot_patch_metadata( |
| 133 | + updates: dict[int, dict[str, Any]], |
| 134 | + job_db: JobDB, |
| 135 | + job_parameters_db: JobParametersDB, |
| 136 | + pilot_agents_db: PilotAgentsDB, |
| 137 | + pilot_info: Annotated[ |
| 138 | + AuthorizedPilotInfo, Depends(verify_dirac_pilot_access_token) |
| 139 | + ], |
| 140 | +): |
| 141 | + # Endpoint only for DiracX pilots (with a pilot token) |
| 142 | + try: |
| 143 | + await verify_that_pilot_can_access_jobs( |
| 144 | + pilot_db=pilot_agents_db, |
| 145 | + pilot_stamp=pilot_info.pilot_stamp, |
| 146 | + job_ids=list(updates), |
| 147 | + ) |
| 148 | + except (PilotCantAccessJobError, JobNotFoundError) as e: |
| 149 | + raise HTTPException( |
| 150 | + status_code=status.HTTP_403_FORBIDDEN, detail="Pilot can't access this job." |
| 151 | + ) from e |
| 152 | + |
| 153 | + try: |
| 154 | + await set_job_parameters_or_attributes_bl(updates, job_db, job_parameters_db) |
| 155 | + except ValueError as e: |
| 156 | + raise HTTPException( |
| 157 | + status_code=HTTPStatus.BAD_REQUEST, |
| 158 | + detail=str(e), |
| 159 | + ) from e |
0 commit comments