创建备份脚本
sudo vim /usr/local/bin/pgsql_backup/xxx.sh
报错如下
2024-12-06 11:28:17,853 - uvicorn.access - INFO - 39.174.79.28:0 - "GET /api/customer/detail/76d23967-bedf-4442-ba37-4aa8837c26a8 HTTP/1.0" 500
2024-12-06 11:28:17,854 - uvicorn.error - ERROR - Exception in ASGI application
Traceback (most recent call last):
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\uvicorn\protocols\http\httptools_impl.py", line 409, in run_asgi
result = await app( # type: ignore[func-returns-value]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\uvicorn\middleware\proxy_headers.py", line 60, in __call__
return await self.app(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\fastapi\applications.py", line 1054, in __call__
await super().__call__(scope, receive, send)
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\starlette\applications.py", line 123, in __call__
await self.middleware_stack(scope, receive, send)
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\starlette\middleware\errors.py", line 186, in __call__
raise exc
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\starlette\middleware\errors.py", line 164, in __call__
await self.app(scope, receive, _send)
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\starlette\middleware\cors.py", line 93, in __call__
await self.simple_response(scope, receive, send, request_headers=headers)
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\starlette\middleware\cors.py", line 148, in simple_response
await self.app(scope, receive, send)
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\starlette\middleware\exceptions.py", line 65, in __call__
await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\starlette\_exception_handler.py", line 64, in wrapped_app
raise exc
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
await app(scope, receive, sender)
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\starlette\routing.py", line 756, in __call__
await self.middleware_stack(scope, receive, send)
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\starlette\routing.py", line 776, in app
await route.handle(scope, receive, send)
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\starlette\routing.py", line 297, in handle
await self.app(scope, receive, send)
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\starlette\routing.py", line 77, in app
await wrap_app_handling_exceptions(app, request)(scope, receive, send)
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\starlette\_exception_handler.py", line 64, in wrapped_app
raise exc
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
await app(scope, receive, sender)
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\starlette\routing.py", line 72, in app
response = await func(request)
^^^^^^^^^^^^^^^^^^^
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\fastapi\routing.py", line 269, in app
solved_result = await solve_dependencies(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\fastapi\dependencies\utils.py", line 602, in solve_dependencies
solved = await run_in_threadpool(call, **sub_values)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\starlette\concurrency.py", line 42, in run_in_threadpool
return await anyio.to_thread.run_sync(func, *args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\anyio\to_thread.py", line 56, in run_sync
return await get_async_backend().run_sync_in_worker_thread(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\anyio\_backends\_asyncio.py", line 2441, in run_sync_in_worker_thread
return await future
^^^^^^^^^^^^
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\asyncio\futures.py", line 291, in __await__
yield self # This tells Task to wait for completion.
^^^^^^^^^^
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\asyncio\tasks.py", line 385, in __wakeup
future.result()
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\asyncio\futures.py", line 203, in result
raise self._exception.with_traceback(self._exception_tb)
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\anyio\_backends\_asyncio.py", line 943, in run
result = context.run(func, *args)
^^^^^^^^^^^^^^^^^^^^^^^^
File "src/dependency_injector/_cwiring.pyx", line 28, in dependency_injector._cwiring._get_sync_patched._patched
File "C:\zb\code\wk3\ms_crm\backend\basic_module\utils\user_auth_util.py", line 24, in validate_token
ignore_auth = user_service.check_backend_api_ignore(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\zb\code\wk3\ms_crm\backend\basic_module\service\user_service.py", line 285, in check_backend_api_ignore
backend_api = self.__backend_api_repository.get_by_url(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\zb\code\wk3\ms_crm\backend\basic_module\repository\backend_api_repository.py", line 138, in get_by_url
return self.get_by_params(
^^^^^^^^^^^^^^^^^^^
File "C:\zb\code\wk3\ms_crm\backend\basic\repository\base_repository.py", line 127, in get_by_params
result = self.session.execute(text(sql), params=params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\sqlalchemy\orm\scoping.py", line 778, in execute
return self._proxied.execute(
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\sqlalchemy\orm\session.py", line 2362, in execute
return self._execute_internal(
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\sqlalchemy\orm\session.py", line 2237, in _execute_internal
conn = self._connection_for_bind(bind)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\sqlalchemy\orm\session.py", line 2106, in _connection_for_bind
return trans._connection_for_bind(engine, execution_options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<string>", line 2, in _connection_for_bind
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\sqlalchemy\orm\state_changes.py", line 139, in _go
ret_value = fn(self, *arg, **kw)
^^^^^^^^^^^^^^^^^^^^
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\sqlalchemy\orm\session.py", line 1189, in _connection_for_bind
conn = bind.connect()
^^^^^^^^^^^^^^
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\sqlalchemy\engine\base.py", line 3278, in connect
return self._connection_cls(self)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\sqlalchemy\engine\base.py", line 146, in __init__
self._dbapi_connection = engine.raw_connection()
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\sqlalchemy\engine\base.py", line 3302, in raw_connection
return self.pool.connect()
^^^^^^^^^^^^^^^^^^^
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\sqlalchemy\pool\base.py", line 449, in connect
return _ConnectionFairy._checkout(self)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\sqlalchemy\pool\base.py", line 1263, in _checkout
fairy = _ConnectionRecord.checkout(pool)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\sqlalchemy\pool\base.py", line 712, in checkout
rec = pool._do_get()
^^^^^^^^^^^^^^
File "C:\Users\268zh\anaconda3\envs\env_ms_crm_wk3_python312_2\Lib\site-packages\sqlalchemy\pool\impl.py", line 168, in _do_get
raise exc.TimeoutError(
sqlalchemy.exc.TimeoutError: QueuePool limit of size 5 overflow 10 reached, connection timed out, timeout 30.00 (Background on this error at: https://sqlalche.me/e/20/3o7r)
原因
没有及时关闭连接
解决方法:加上 with uow,因为uow在事务结束的时候会统一关闭数据库连接
import os
import toml
from fastapi import Depends, HTTPException, Request
from dependency_injector.wiring import inject, Provide
from basic.api_response.api_response import ApiResponse
from basic.repository.unit_of_work import UnitOfWork
from basic_module import BasicModuleContainer
from basic_module.service.user_service import UserService
PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_PATH = os.path.join(PATH, "../")
APP_CONFIG = toml.load(fr"{BASE_PATH}app_config.toml")
ACTIVE = APP_CONFIG.get('settings', {}).get('active', 'development')
CONFIG_NAME = f'app_{ACTIVE}_config.toml'
CONFIG = toml.load(fr"{BASE_PATH}config/{CONFIG_NAME}")
@inject
def validate_token(
request: Request,
uow: UnitOfWork = Depends(Provide[BasicModuleContainer.unit_of_work]),
user_service: UserService = Depends(Provide[BasicModuleContainer.user_service])
):
with uow:
ignore_auth = user_service.check_backend_api_ignore(
url=request.url.path,
)
if ignore_auth:
return
token = request.headers.get("Authorization")
if token is None or not token.startswith("Bearer "):
raise HTTPException(status_code=401, detail=ApiResponse(code=40131, message="令牌缺失").to_json())
token_value = token.split(" ")[1]
return user_service.validate_token(
token=token_value,
url=request.url.path,
)