Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
255 lines (221 loc) · 8.94 KB
/
Copy pathserver.py
File metadata and controls
255 lines (221 loc) · 8.94 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
import asyncio
from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager
from fastapi import FastAPI
# from plugins.core.runtime.application import get_plugin_application_runtime
from common.router import auto_register_routers
from config.env import AppConfig
from config.get_db import close_async_engine, init_create_table
from config.get_redis import RedisUtil
# from config.get_scheduler import SchedulerUtil
from exceptions.handle import handle_exception
from middlewares.handle import handle_middleware
from sub_applications.handle import handle_sub_applications
from utils.common_util import worship
from utils.log_util import logger
from utils.server_util import APIDocsUtil, IPUtil
async def _start_background_tasks(app: FastAPI) -> None:
"""
启动应用后台任务
:param app: FastAPI对象
:return: None
"""
# await SchedulerUtil.init_system_scheduler(app.state.redis)
# app.state.log_aggregator_task = asyncio.create_task(
# LogAggregatorService.consume_stream(app.state.redis)
# )
async def _stop_background_tasks(app: FastAPI) -> None:
"""
停止应用后台任务并释放资源
:param app: FastAPI对象
:return: None
"""
try:
log_task = getattr(app.state, "log_aggregator_task", None)
if log_task:
log_task.cancel()
try:
await log_task
except asyncio.CancelledError:
pass
finally:
try:
pass
# Scheduler负责停止续期并释放Application租约,必须先于Redis连接池关闭。
# await SchedulerUtil.close_system_scheduler()
finally:
try:
await RedisUtil.close_redis_pool(app)
finally:
await close_async_engine()
async def _shutdown_application_runtime(app: FastAPI) -> None:
"""
关闭插件运行时并保证基础设施资源始终释放。
:param app: FastAPI对象
:return: None
"""
print("--------------- shutdown, close resource...........+2 ")
try:
pass
# if getattr(app.state, "plugin_application_runtime_started", False):
# await get_plugin_application_runtime().shutdown(app)
finally:
try:
await _stop_background_tasks(app)
finally:
# 所有sink均使用enqueue=True,进程退出前必须等待插件Hook等尾部日志落盘。
await logger.complete()
# 生命周期事件
@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
"""
应用生命周期管理
yield 之前是启动阶段,yield 之后是关闭阶段.
:param app: FastAPI对象
:return: None
"""
# # 进入 yield 之前的代码 ← STARTUP
app.state.redis = await RedisUtil.create_redis_pool(log_enabled=False)
app.state.plugin_application_runtime_started = False
try:
application_leader = True
# application_lock_owner_token = SchedulerUtil.get_application_lock_owner_token()
# application_leader = await StartupUtil.acquire_application_leader(
# redis=app.state.redis,
# lock_key=LockConstant.APP_STARTUP_LOCK_KEY,
# owner_token=application_lock_owner_token,
# lock_expire_seconds=LockConstant.LOCK_EXPIRE_SECONDS,
# )
# app.state.application_leader = application_leader
# app.state.application_lock_owner_token = application_lock_owner_token
# # 获取锁成功后立即启动锁续期任务,避免初始化时间过长导致锁过期
# if application_leader:
# SchedulerUtil.start_application_lock_renewal(app.state.redis)
startup_logger = logger.bind(
startup_phase="application_startup",
startup_role="application_leader",
)
# if application_leader:
# startup_logger.info(f"⏰️ {AppConfig.app_name}开始启动")
# worship()
startup_logger.info(f"⏰️ {AppConfig.app_name}开始启动")
worship()
# TransportKeyProvider.validate_runtime_configuration()
await _initialize_application_runtime(
app, application_leader=application_leader
)
# 初始化期间可能因续期失败失去租约;此时不得继续输出leader专属成功摘要。
# application_leader = (
# application_leader and SchedulerUtil.is_application_leader()
# )
app.state.application_leader = application_leader
if application_leader:
# 短暂等待确保下面的启动日志在最后打印
await asyncio.sleep(1)
startup_logger.info(f"🚀 {AppConfig.app_name}启动成功")
host = AppConfig.app_host
port = AppConfig.app_port
if host == "0.0.0.0":
local_ip = IPUtil.get_local_ip()
network_ips = IPUtil.get_network_ips()
else:
local_ip = host
network_ips = [host]
app_links = [f"🏠 Local: <cyan>http://{local_ip}:{port}</cyan>"]
app_links.extend(
f"📡 Network: <cyan>http://{ip}:{port}</cyan>" for ip in network_ips
)
logger.opt(colors=True).info("💻 应用地址:\n" + "\n".join(app_links))
if not AppConfig.app_disable_swagger:
swagger_links = [
f"🏠 Local: <cyan>http://{local_ip}:{port}{APIDocsUtil.docs_url()}</cyan>"
]
swagger_links.extend(
f"📡 Network: <cyan>http://{ip}:{port}{APIDocsUtil.docs_url()}</cyan>"
for ip in network_ips
)
logger.opt(colors=True).info(
"📄 Swagger文档:\n" + "\n".join(swagger_links)
)
if not AppConfig.app_disable_redoc:
redoc_links = [
f"🏠 Local: <cyan>http://{local_ip}:{port}{APIDocsUtil.redoc_url()}</cyan>"
]
redoc_links.extend(
f"📡 Network: <cyan>http://{ip}:{port}{APIDocsUtil.redoc_url()}</cyan>"
for ip in network_ips
)
logger.opt(colors=True).info("📚 ReDoc文档:\n" + "\n".join(redoc_links))
# 确保启动阶段的插件摘要在ASGI lifespan启动完成前已写入stdout和日志文件。
await logger.complete() # 等待所有日志落盘
#
yield # ← 应用运行中,处理请求
finally:
# 无论正常退出还是异常退出都会被执行。
# Worker 优雅关闭(执行 lifespan shutdown)
await _shutdown_application_runtime(app) ## ← SHUTDOWN(异常时也会执行)
async def _initialize_application_runtime(
app: FastAPI, application_leader: bool
) -> None:
"""
初始化应用运行时资源。
:param app: FastAPI对象
:param application_leader: 当前worker是否为Application leader
:return: None
"""
# plugin_runtime = get_plugin_application_runtime()
# plugin_runtime.prepare_metadata(app)
# await init_create_table(
# stage="platform",
# log_success_enabled=application_leader,
# )
async def create_plugin_entity_tables() -> None:
"""在插件 writer 导入实体后同步插件表。"""
await init_create_table(
stage="plugin_entities",
log_success_enabled=True,
)
# await plugin_runtime.startup(
# app,
# create_tables=create_plugin_entity_tables,
# )
app.state.plugin_application_runtime_started = True
await RedisUtil.check_redis_connection(
app.state.redis,
log_enabled=application_leader,
log_error_enabled=True,
)
# await RedisUtil.init_sys_dict(app.state.redis)
# await RedisUtil.init_sys_config(app.state.redis)
# await _start_background_tasks(app)
def create_app() -> FastAPI:
"""
创建FastAPI应用
:return: FastAPI对象
"""
# 配置API文档静态资源
APIDocsUtil.setup_docs_static_resources()
# 初始化FastAPI对象
app = FastAPI(
title=AppConfig.app_name,
description=f"{AppConfig.app_name}接口文档",
version=AppConfig.app_version,
lifespan=lifespan,
openapi_url=APIDocsUtil.proxy_openapi_url(),
docs_url=APIDocsUtil.proxy_docs_url(),
redoc_url=APIDocsUtil.proxy_redoc_url(),
swagger_ui_oauth2_redirect_url=APIDocsUtil.proxy_oauth2_redirect_url(),
)
# 自定义API文档路由,修复无法直接通过后端地址访问文档的问题
APIDocsUtil.custom_api_docs_router(app)
# 挂载子应用
handle_sub_applications(app)
# 加载中间件处理方法
handle_middleware(app)
# 加载全局异常处理方法
handle_exception(app)
# 自动注册内置路由
auto_register_routers(app)
# 初始化插件应用运行时
# get_plugin_application_runtime().bind_app(app)
return app