-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfastapi.py
More file actions
450 lines (369 loc) · 12.7 KB
/
fastapi.py
File metadata and controls
450 lines (369 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
"""FastAPI server for local development and testing."""
import asyncio
import threading
import uuid
from dataclasses import dataclass
from typing import Any, Dict, List, Optional
import aiohttp
import uvicorn
from fastapi import APIRouter, FastAPI
from fastapi.encoders import jsonable_encoder
from fastapi.responses import RedirectResponse
from .handler import is_async_generator, is_generator, is_sync_generator
from .heartbeat import Heartbeat
from .job import run_job
from .logger import log
from .state import JobsProgress
TITLE = "WaveSpeed | Development Worker API"
DESCRIPTION = """
The Development Worker API facilitates testing and debugging of your WaveSpeed workers.
It offers a sandbox environment for executing code and simulating interactions with your worker.
Use this API for comprehensive testing of request submissions and result retrieval,
mimicking the behavior of the operational environment.
---
*Note: This API serves as a local testing tool and will not be utilized once your worker is deployed.*
"""
RUN_DESCRIPTION = """
Initiates processing jobs asynchronously, returning a unique job ID.
**Parameters:**
- **input** (dict): The data to be processed by the worker.
- **webhook** (string, optional): A callback URL for result notification upon completion.
**Returns:**
- **id** (string): A unique identifier for the job.
- **status** (string): The job status (IN_PROGRESS).
"""
RUNSYNC_DESCRIPTION = """
Executes processing jobs synchronously, returning the job's output directly.
**Parameters:**
- **input** (dict): The data to be processed by the worker.
- **webhook** (string, optional): A callback URL for async result notification.
**Returns:**
- **id** (string): The job identifier.
- **status** (string): COMPLETED or FAILED.
- **output** (Any): The job output (if successful).
- **error** (string): Error message (if failed).
"""
STREAM_DESCRIPTION = """
Aggregates the output of a streaming/generator job.
**Parameters:**
- **job_id** (string): The unique identifier of the job.
**Returns:**
- **id** (string): The job identifier.
- **status** (string): COMPLETED or FAILED.
- **stream** (list): The aggregated stream output.
"""
STATUS_DESCRIPTION = """
Checks the completion status of a job and returns its output.
**Parameters:**
- **job_id** (string): The unique identifier for the job.
**Returns:**
- **id** (string): The job identifier.
- **status** (string): COMPLETED, IN_PROGRESS, or FAILED.
- **output** (Any): The job output (if complete).
"""
# Store pending jobs for async execution
_pending_jobs: Dict[str, Dict[str, Any]] = {}
_job_results: Dict[str, Dict[str, Any]] = {}
@dataclass
class JobRequest:
"""Represents a job request."""
input: Dict[str, Any]
webhook: Optional[str] = None
@dataclass
class JobResponse:
"""Represents a job response."""
id: str
status: str
output: Optional[Any] = None
error: Optional[str] = None
@dataclass
class StreamResponse:
"""Represents a stream response."""
id: str
status: str
stream: Optional[List[Any]] = None
error: Optional[str] = None
async def _send_webhook(url: str, payload: Dict[str, Any]) -> bool:
"""Send a webhook notification."""
try:
async with aiohttp.ClientSession() as session:
async with session.post(url, json=payload, timeout=10) as response:
return response.status < 400
except Exception as e:
log.error(f"Webhook to {url} failed: {e}")
return False
def _send_webhook_sync(url: str, payload: Dict[str, Any]) -> None:
"""Send webhook in a background thread."""
asyncio.run(_send_webhook(url, payload))
class WorkerAPI:
"""FastAPI server for local development and testing."""
def __init__(self, config: Dict[str, Any]):
"""Initialize the WorkerAPI.
Args:
config: Worker configuration containing the handler.
"""
self.config = config
self.heartbeat = Heartbeat()
self.jobs_progress = JobsProgress()
tags_metadata = [
{
"name": "Sync",
"description": "Synchronous job execution endpoints.",
},
{
"name": "Async",
"description": "Asynchronous job submission endpoints.",
},
{
"name": "Status",
"description": "Job status and result endpoints.",
},
]
self.app = FastAPI(
title=TITLE,
description=DESCRIPTION,
version="1.0.0",
docs_url="/",
openapi_tags=tags_metadata,
)
router = APIRouter()
# Redirect /docs to /
router.add_api_route(
"/docs",
lambda: RedirectResponse(url="/"),
include_in_schema=False,
)
# Async run endpoint
router.add_api_route(
"/run",
self._run,
methods=["POST"],
response_model_exclude_none=True,
summary="Submit a job asynchronously",
description=RUN_DESCRIPTION,
tags=["Async"],
)
# Sync run endpoint
router.add_api_route(
"/runsync",
self._runsync,
methods=["POST"],
response_model_exclude_none=True,
summary="Submit and wait for job completion",
description=RUNSYNC_DESCRIPTION,
tags=["Sync"],
)
# Stream endpoint
router.add_api_route(
"/stream/{job_id}",
self._stream,
methods=["POST"],
response_model_exclude_none=True,
summary="Get streaming job output",
description=STREAM_DESCRIPTION,
tags=["Status"],
)
# Status endpoint
router.add_api_route(
"/status/{job_id}",
self._status,
methods=["POST"],
response_model_exclude_none=True,
summary="Check job status",
description=STATUS_DESCRIPTION,
tags=["Status"],
)
# Health check endpoint
router.add_api_route(
"/health",
lambda: {"status": "ok"},
methods=["GET"],
summary="Health check",
tags=["Status"],
)
self.app.include_router(router)
def start(
self,
host: str = "localhost",
port: int = 8000,
workers: int = 1,
) -> None:
"""Start the FastAPI server.
Args:
host: Host to bind to.
port: Port to bind to.
workers: Number of worker processes.
"""
log.info(f"Starting API server at http://{host}:{port}")
self.heartbeat.start()
try:
uvicorn.run(
self.app,
host=host,
port=port,
workers=workers,
log_level="info",
access_log=False,
)
finally:
self.heartbeat.stop()
async def _run_generator(
self, handler: Any, job_input: Dict[str, Any]
) -> Dict[str, Any]:
"""Run a generator handler and collect outputs.
Args:
handler: The generator handler function.
job_input: The job input dict.
Returns:
Dict with aggregated output or error.
"""
try:
outputs = []
if is_async_generator(handler):
async for partial in handler(job_input):
outputs.append(partial)
elif is_sync_generator(handler):
for partial in handler(job_input):
outputs.append(partial)
return {"output": outputs}
except Exception as e:
log.error(f"Generator handler error: {e}")
return {"error": str(e)}
async def _run(self, job_request: JobRequest) -> Dict[str, Any]:
"""Submit a job asynchronously."""
job_id = f"test-{uuid.uuid4()}"
# Store the pending job
_pending_jobs[job_id] = {
"id": job_id,
"input": job_request.input,
"webhook": job_request.webhook,
}
return jsonable_encoder({"id": job_id, "status": "IN_PROGRESS"})
async def _runsync(self, job_request: JobRequest) -> Dict[str, Any]:
"""Submit a job and wait for completion."""
job_id = f"test-{uuid.uuid4()}"
job = {"id": job_id, "input": job_request.input}
job_input = {"id": job_id, "input": job_request.input}
handler = self.config["handler"]
if is_generator(handler):
job_output = await self._run_generator(handler, job_input)
else:
job_output = await run_job(handler, job)
if job_output.get("error"):
return jsonable_encoder(
{
"id": job_id,
"status": "FAILED",
"error": job_output["error"],
}
)
# Send webhook if provided
if job_request.webhook:
thread = threading.Thread(
target=_send_webhook_sync,
args=(job_request.webhook, job_output),
daemon=True,
)
thread.start()
return jsonable_encoder(
{
"id": job_id,
"status": "COMPLETED",
"output": job_output.get("output"),
}
)
async def _stream(self, job_id: str) -> Dict[str, Any]:
"""Get streaming job output."""
if job_id not in _pending_jobs:
return jsonable_encoder(
{
"id": job_id,
"status": "FAILED",
"error": "Job ID not found",
}
)
pending = _pending_jobs[job_id]
job_input = {"id": job_id, "input": pending["input"]}
handler = self.config["handler"]
if not is_generator(handler):
return jsonable_encoder(
{
"id": job_id,
"status": "FAILED",
"error": "Stream not supported, handler must be a generator.",
}
)
job_output = await self._run_generator(handler, job_input)
# Clean up
del _pending_jobs[job_id]
if job_output.get("error"):
return jsonable_encoder(
{
"id": job_id,
"status": "FAILED",
"error": job_output["error"],
}
)
# Format stream output
stream_output = [{"output": item} for item in job_output.get("output", [])]
# Send webhook if provided
if pending.get("webhook"):
thread = threading.Thread(
target=_send_webhook_sync,
args=(pending["webhook"], stream_output),
daemon=True,
)
thread.start()
return jsonable_encoder(
{
"id": job_id,
"status": "COMPLETED",
"stream": stream_output,
}
)
async def _status(self, job_id: str) -> Dict[str, Any]:
"""Check job status and get output."""
if job_id not in _pending_jobs:
# Check if we have cached results
if job_id in _job_results:
return jsonable_encoder(_job_results[job_id])
return jsonable_encoder(
{
"id": job_id,
"status": "FAILED",
"error": "Job ID not found",
}
)
pending = _pending_jobs[job_id]
job = {"id": job_id, "input": pending["input"]}
job_input = {"id": job_id, "input": pending["input"]}
handler = self.config["handler"]
if is_generator(handler):
job_output = await self._run_generator(handler, job_input)
else:
job_output = await run_job(handler, job)
# Clean up pending job
del _pending_jobs[job_id]
if job_output.get("error"):
result = {
"id": job_id,
"status": "FAILED",
"error": job_output["error"],
}
else:
result = {
"id": job_id,
"status": "COMPLETED",
"output": job_output.get("output"),
}
# Cache the result
_job_results[job_id] = result
# Send webhook if provided
if pending.get("webhook"):
thread = threading.Thread(
target=_send_webhook_sync,
args=(pending["webhook"], job_output),
daemon=True,
)
thread.start()
return jsonable_encoder(result)