|
3 | 3 |
|
4 | 4 | import os
|
5 | 5 | import uuid
|
| 6 | +from dataclasses import dataclass |
6 | 7 | from typing import Union, Optional, Dict, Any
|
7 | 8 |
|
8 | 9 | import uvicorn
|
9 | 10 | from fastapi import FastAPI, APIRouter
|
10 | 11 | from fastapi.encoders import jsonable_encoder
|
11 | 12 | from fastapi.responses import RedirectResponse
|
12 |
| -from pydantic import BaseModel |
13 | 13 |
|
14 | 14 | from .rp_handler import is_generator
|
15 | 15 | from .rp_job import run_job, run_job_generator
|
|
39 | 39 |
|
40 | 40 |
|
41 | 41 | # ------------------------------- Input Objects ------------------------------ #
|
42 |
| -class Job(BaseModel): |
| 42 | +@dataclass |
| 43 | +class Job: |
43 | 44 | ''' Represents a job. '''
|
44 | 45 | id: str
|
45 | 46 | input: Union[dict, list, str, int, float, bool]
|
46 | 47 |
|
47 | 48 |
|
48 |
| -class TestJob(BaseModel): |
| 49 | +@dataclass |
| 50 | +class TestJob: |
49 | 51 | ''' Represents a test job.
|
50 | 52 | input can be any type of data.
|
51 | 53 | '''
|
52 |
| - id: Optional[str] |
53 |
| - input: Optional[Union[dict, list, str, int, float, bool]] |
| 54 | + id: Optional[str] = None |
| 55 | + input: Optional[Union[dict, list, str, int, float, bool]] = None |
54 | 56 |
|
55 | 57 |
|
56 |
| -class DefaultInput(BaseModel): |
| 58 | +@dataclass |
| 59 | +class DefaultInput: |
57 | 60 | """ Represents a test input. """
|
58 | 61 | input: Dict[str, Any]
|
59 | 62 |
|
60 | 63 |
|
61 | 64 | # ------------------------------ Output Objects ------------------------------ #
|
62 |
| -class JobOutput(BaseModel): |
| 65 | +@dataclass |
| 66 | +class JobOutput: |
63 | 67 | ''' Represents the output of a job. '''
|
64 | 68 | id: str
|
65 | 69 | status: str
|
66 |
| - output: Optional[Union[dict, list, str, int, float, bool]] |
67 |
| - error: Optional[str] |
| 70 | + output: Optional[Union[dict, list, str, int, float, bool]] = None |
| 71 | + error: Optional[str] = None |
68 | 72 |
|
69 | 73 |
|
70 |
| -class StreamOutput(BaseModel): |
| 74 | +@dataclass |
| 75 | +class StreamOutput: |
71 | 76 | """ Stream representation of a job. """
|
72 | 77 | id: str
|
73 | 78 | status: str = "IN_PROGRESS"
|
74 |
| - stream: Optional[Union[dict, list, str, int, float, bool]] |
75 |
| - error: Optional[str] |
| 79 | + stream: Optional[Union[dict, list, str, int, float, bool]] = None |
| 80 | + error: Optional[str] = None |
76 | 81 |
|
77 | 82 |
|
78 | 83 | # ---------------------------------------------------------------------------- #
|
@@ -191,6 +196,13 @@ async def _sim_runsync(self, job_input: DefaultInput) -> JobOutput:
|
191 | 196 | else:
|
192 | 197 | job_output = await run_job(self.config["handler"], job.__dict__)
|
193 | 198 |
|
| 199 | + if job_output.get('error', None): |
| 200 | + return jsonable_encoder({ |
| 201 | + "id": job.id, |
| 202 | + "status": "FAILED", |
| 203 | + "error": job_output['error'] |
| 204 | + }) |
| 205 | + |
194 | 206 | return jsonable_encoder({
|
195 | 207 | "id": job.id,
|
196 | 208 | "status": "COMPLETED",
|
@@ -253,6 +265,13 @@ async def _sim_status(self, job_id: str) -> JobOutput:
|
253 | 265 |
|
254 | 266 | job_list.remove_job(job.id)
|
255 | 267 |
|
| 268 | + if job_output.get('error', None): |
| 269 | + return jsonable_encoder({ |
| 270 | + "id": job_id, |
| 271 | + "status": "FAILED", |
| 272 | + "error": job_output['error'] |
| 273 | + }) |
| 274 | + |
256 | 275 | return jsonable_encoder({
|
257 | 276 | "id": job_id,
|
258 | 277 | "status": "COMPLETED",
|
|
0 commit comments