-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.py
More file actions
77 lines (64 loc) · 2.22 KB
/
app.py
File metadata and controls
77 lines (64 loc) · 2.22 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
import os
import time
import asyncio
from datetime import datetime, timezone
from contextlib import asynccontextmanager
from fastapi import FastAPI
from agents.workflows.index import close_checkpointer
from controllers.workflow_controller import workflow_router
from tools.logger_config import logger
from middleware.logging_middlewares import (
LoggingMiddleware,
AddRequestContextMiddleware,
TraceIDMiddleware,
)
@asynccontextmanager
async def lifespan(app: FastAPI):
logger.info("Application starting up...")
yield
logger.info("Application shutting down...")
await asyncio.to_thread(close_checkpointer)
app = FastAPI(
title="FastAPI Workflow Orchestration System",
description="A workflow orchestration system built with FastAPI",
version="1.0.0",
lifespan=lifespan,
)
# Register logging middlewares (applied in reverse order of definition)
app.add_middleware(LoggingMiddleware)
app.add_middleware(AddRequestContextMiddleware)
app.add_middleware(TraceIDMiddleware)
# Include workflow router
app.include_router(workflow_router)
@app.get("/")
def hello_world():
return {
'message': 'FastAPI Workflow Orchestration System',
'version': '1.0.0',
'endpoints': {
'start_workflow': 'POST /workflows/{workflow_name}',
'continue_workflow': 'POST /workflows/{workflow_name}/{thread_id}',
'get_state': 'GET /workflows/{workflow_name}/{thread_id}/state',
'available_workflows': 'GET /workflows/available'
}
}
# Store application start time
start_time = time.time()
def convert_seconds_to_hms(seconds):
"""Convert seconds to hours, minutes, seconds format"""
hours = int(seconds // 3600)
minutes = int((seconds % 3600) // 60)
seconds = int(seconds % 60)
return f"{hours}h {minutes}m {seconds}s"
@app.get("/health")
def health_check():
uptime_seconds = time.time() - start_time
health_check_response = {
"status": "UP",
"timestamp": datetime.now(timezone.utc).isoformat(),
"uptime": convert_seconds_to_hms(uptime_seconds),
}
return health_check_response
if __name__ == '__main__':
import uvicorn
uvicorn.run("app:app", host='0.0.0.0', port=int(os.getenv("PORT")), reload=True)