Files
amq-adapter-python/cleverswarm/rest/v0/endpoints/jobs.py
T

75 lines
3.8 KiB
Python

from typing import List
import logging
from fastapi import APIRouter, status, Depends
from fastapi import Path as FPath
from cleverswarm.schemas.request_schema import RequestSchema
from cleverswarm.schemas.user_schema import UserSchema
from cleverswarm.core.deps import get_current_user
logger = logging.getLogger('Jobs')
# Enable propagation to the root logger
logger.propagate = True
router = APIRouter()
@router.get('/{request_id}', summary='Obtains the status of a given job',
description='Obtains the status of an existing job by its id',
status_code=status.HTTP_200_OK,
responses={
200: {'description': 'Job status retrieved successfully'},
404: {'description': 'Job not found'},
500: {'description': 'A server error occurred while processing the request'}
})
async def get_job_status(request_id: str = FPath(title='Job ID',
description='The id of the job for which the status is to be queried'),
authenticated_user: UserSchema = Depends(get_current_user)):
logging.info(f'===> GET /jobs/{request_id}: Obtains the status of a given job')
return "JobStatus.Completed"
@router.get('/', summary='List all user submitted requests',
description='List all user submitted requests along with their status',
status_code=status.HTTP_200_OK,
response_model=List[RequestSchema],
response_description='User submitted job requests',
responses={
200: {'description': 'User submitted job requests'},
500: {'description': 'A server error occurred while processing the request'}
})
async def get_requests(authenticated_user: UserSchema = Depends(get_current_user)):
logging.info('===> GET /jobs: List all user submitted requests')
return []
@router.delete('/{request_id}', summary='Deletes a completed or failed job',
description='Deletes a completed or failed job by its request_id',
status_code=status.HTTP_204_NO_CONTENT,
responses={
204: {'description': 'Job data was successfully deleted'},
404: {'description': 'Job ID not found'},
412: {'description': 'Job is in state that does not allow deletion'},
500: {'description': 'A server error occurred while processing the request'}
})
async def delete_job(request_id: str = FPath(title='Request ID',
description='The id of the failed or completed request to be deleted'),
authenticated_user: UserSchema = Depends(get_current_user)):
logging.info(f'===> DELETE /jobs/{request_id}: Deletes a completed or failed job')
return 204
@router.put('/{request_id}', summary='Retries execution of a given job',
description='Retries the execution of a given job by its id, provided the current job status is failed',
status_code=status.HTTP_202_ACCEPTED,
responses={
202: {'model': RequestSchema,
'description': 'The updated job status and its configuration'},
406: {'description': 'Job is an state that does not allow it to be retried'},
500: {'description': 'A server error occurred while processing the request'}
})
async def retry_job(request_id: str = FPath(title='Job ID',
description='The id of the job to be retried'),
authenticated_user: UserSchema = Depends(get_current_user)):
logging.info(f'===> PUT /jobs/{request_id}: Retries execution of a given job')
return RequestSchema(job_id=request_id, status='Failed')