This commit is contained in:
2025-01-20 15:38:15 +08:00
parent a0b76f1d51
commit b9cde3e874
14 changed files with 161 additions and 9 deletions

View File

@@ -0,0 +1,11 @@
from app.core import config
def main():
"""Main entry point for the application."""
print(f"Welcome to {config.APP_NAME}!")
print(f"Debug mode: {config.DEBUG}")
if __name__ == "__main__":
main()

View File

@@ -0,0 +1 @@
from .config import *

View File

@@ -0,0 +1,37 @@
from pathlib import Path
from typing import Optional, Tuple, Type
from pydantic_settings import (
BaseSettings,
PydanticBaseSettingsSource,
SettingsConfigDict,
TomlConfigSettingsSource,
)
class Settings(BaseSettings):
"""Application settings."""
# 应用配置
APP_NAME: str = "{{ cookiecutter.project_name }}"
DEBUG: bool = False
SECRET_KEY: str = "{{ cookiecutter.secret_key }}"
# 路径配置
BASE_DIR: Path = Path(__file__).parent.parent.parent
model_config = SettingsConfigDict(toml_file=BASE_DIR / "config.toml")
@classmethod
def settings_customise_sources(
cls,
settings_cls: Type[BaseSettings],
init_settings: PydanticBaseSettingsSource,
env_settings: PydanticBaseSettingsSource,
dotenv_settings: PydanticBaseSettingsSource,
file_secret_settings: PydanticBaseSettingsSource,
) -> Tuple[PydanticBaseSettingsSource, ...]:
return (TomlConfigSettingsSource(settings_cls),)
# 创建全局设置实例
config = Settings()

View File

@@ -0,0 +1,2 @@
#!/usr/bin/env python3
# -*- coding:utf-8 -*-

View File

@@ -0,0 +1,48 @@
import asyncio
import datetime
from functools import wraps
from loguru import logger
def retry_async(max_retries=3, initial_delay=1, exceptions=(Exception,)):
def decorator(func):
@wraps(func)
async def wrapper(*args, **kwargs):
attempt = 0
delay = initial_delay
while attempt < max_retries:
try:
return await func(*args, **kwargs)
except exceptions as e:
logger.info(
f"{e=}, attempt {attempt + 1}/{max_retries}, will retry in {delay} seconds"
)
logger.exception(f"{e=}")
attempt += 1
if attempt >= max_retries:
logger.info("Max retries reached, aborting")
raise e
await asyncio.sleep(delay)
delay *= 2 # 延迟时间加倍
return wrapper
return decorator
if __name__ == "__main__":
@retry_async()
async def main():
now = datetime.datetime.now()
logger.info(f"{now=}")
# Fail twice then succeed
if main.counter < 2:
main.counter += 1
raise Exception(now)
return now
main.counter = 0
asyncio.run(main())