Slightly extend background jobs api

This commit is contained in:
Matthias 2024-05-12 09:12:53 +02:00
parent 0279cf5fed
commit e4881580fd
3 changed files with 25 additions and 1 deletions

View File

@ -1,5 +1,6 @@
import logging
from copy import deepcopy
from typing import List
from fastapi import APIRouter, BackgroundTasks, Depends
from fastapi.exceptions import HTTPException
@ -21,6 +22,19 @@ logger = logging.getLogger(__name__)
router = APIRouter()
@router.get('/background', response_model=List[BackgroundTaskStatus], tags=['webserver'])
def background_job_list():
return [{
'job_id': jobid,
'job_category': job['category'],
'status': job['status'],
'running': job['is_running'],
'progress': job.get('progress'),
'error': job.get('error', None),
} for jobid, job in ApiBG.jobs.items()]
@router.get('/background/{jobid}', response_model=BackgroundTaskStatus, tags=['webserver'])
def background_job(jobid: str):
if not (job := ApiBG.jobs.get(jobid)):
@ -32,7 +46,7 @@ def background_job(jobid: str):
'status': job['status'],
'running': job['is_running'],
'progress': job.get('progress'),
# 'job_error': job['error'],
'error': job.get('error', None),
}

View File

@ -44,6 +44,7 @@ class BackgroundTaskStatus(BaseModel):
status: str
running: bool
progress: Optional[float] = None
error: Optional[str] = None
class BackgroundTaskResult(BaseModel):

View File

@ -1964,6 +1964,15 @@ def test_api_pairlists_evaluate(botclient, tmp_path, mocker):
assert_response(rc, 404)
assert rc.json()['detail'] == 'Job not found.'
# Background list
rc = client_get(client, f"{BASE_URI}/background")
assert_response(rc)
response = rc.json()
assert isinstance(response, list)
assert len(response) == 1
assert response[0]['job_id'] == job_id
# Get individual job
rc = client_get(client, f"{BASE_URI}/background/{job_id}")
assert_response(rc)
response = rc.json()