Optimize some global variable definitions (#1178)

This commit is contained in:
Wu Clan
2026-05-15 11:55:22 +08:00
committed by GitHub
parent 5352f98c13
commit 2e86d79150
17 changed files with 136 additions and 127 deletions
+2 -2
View File
@@ -7,7 +7,7 @@ import celery_aio_pool
from celery.signals import worker_process_init
from opentelemetry.instrumentation.celery import CeleryInstrumentor
from backend.app.task.tasks.beat import LOCAL_BEAT_SCHEDULE
from backend.app.task.tasks.beat import get_local_beat_schedule
from backend.common.enums import DataBaseType
from backend.core.conf import settings
from backend.core.path_conf import BASE_PATH
@@ -57,7 +57,7 @@ def init_celery() -> celery.Celery:
database_engine_options={'echo': settings.DATABASE_ECHO},
# result_expires=0,
# beat_sync_every=1,
beat_schedule=LOCAL_BEAT_SCHEDULE,
beat_schedule=get_local_beat_schedule(),
beat_scheduler='backend.app.task.utils.schedulers:DatabaseScheduler',
task_cls='backend.app.task.tasks.base:TaskBase',
task_track_started=True,
+30 -25
View File
@@ -1,29 +1,34 @@
from typing import Any
from celery.schedules import schedule
from backend.app.task.utils.tzcrontab import TzAwareCrontab
# 参考:https://docs.celeryq.dev/en/stable/userguide/periodic-tasks.html
LOCAL_BEAT_SCHEDULE = {
'测试同步任务': {
'task': 'task_demo',
'schedule': schedule(30),
},
'测试异步任务': {
'task': 'task_demo_async',
'schedule': TzAwareCrontab('1'),
},
'测试传参任务': {
'task': 'task_demo_params',
'schedule': TzAwareCrontab('1'),
'args': ['你好,'],
'kwargs': {'world': '世界'},
},
'清理操作日志': {
'task': 'backend.app.task.tasks.db_log.tasks.delete_db_opera_log',
'schedule': TzAwareCrontab('0', '0', day_of_week='6'),
},
'清理登录日志': {
'task': 'backend.app.task.tasks.db_log.tasks.delete_db_login_log',
'schedule': TzAwareCrontab('0', '0', day_of_month='15'),
},
}
def get_local_beat_schedule() -> dict[str, dict[str, Any]]:
"""获取本地 Celery beat 任务配置"""
# 参考:https://docs.celeryq.dev/en/stable/userguide/periodic-tasks.html
return {
'测试同步任务': {
'task': 'task_demo',
'schedule': schedule(30),
},
'测试异步任务': {
'task': 'task_demo_async',
'schedule': TzAwareCrontab('1'),
},
'测试传参任务': {
'task': 'task_demo_params',
'schedule': TzAwareCrontab('1'),
'args': ['你好,'],
'kwargs': {'world': '世界'},
},
'清理操作日志': {
'task': 'backend.app.task.tasks.db_log.tasks.delete_db_opera_log',
'schedule': TzAwareCrontab('0', '0', day_of_week='6'),
},
'清理登录日志': {
'task': 'backend.app.task.tasks.db_log.tasks.delete_db_login_log',
'schedule': TzAwareCrontab('0', '0', day_of_month='15'),
},
}
+6 -6
View File
@@ -6,7 +6,7 @@ import math
from datetime import datetime, timedelta
from multiprocessing.util import Finalize
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Final
from celery import current_app, schedules
from celery.beat import ScheduleEntry, Scheduler
@@ -31,10 +31,10 @@ if TYPE_CHECKING:
from redis.asyncio.lock import Lock
# 此计划程序必须比常规的 5 分钟更频繁地唤醒,因为它需要考虑对计划的外部更改
DEFAULT_MAX_INTERVAL = 5 # seconds
_DEFAULT_MAX_INTERVAL: Final = 5 # seconds
# 计划锁时长,避免重复创建
DEFAULT_MAX_LOCK_TIMEOUT = DEFAULT_MAX_INTERVAL * 5 # seconds
_DEFAULT_MAX_LOCK_TIMEOUT: Final = _DEFAULT_MAX_INTERVAL * 5 # seconds
logger = get_logger('fba.schedulers')
@@ -284,7 +284,7 @@ class DatabaseScheduler(Scheduler):
self._dirty = set()
super().__init__(*args, **kwargs)
self._finalize = Finalize(self, self.sync, exitpriority=5)
self.max_interval = kwargs.get('max_interval') or self.app.conf.beat_max_loop_interval or DEFAULT_MAX_INTERVAL
self.max_interval = kwargs.get('max_interval') or self.app.conf.beat_max_loop_interval or _DEFAULT_MAX_INTERVAL
def schedules_equal(self, *args, **kwargs) -> bool:
"""重写父函数"""
@@ -334,7 +334,7 @@ class DatabaseScheduler(Scheduler):
"""重写父函数"""
if self.lock:
logger.debug('beat: Extending lock...')
run_await(self.lock.extend)(DEFAULT_MAX_LOCK_TIMEOUT, replace_ttl=True)
run_await(self.lock.extend)(_DEFAULT_MAX_LOCK_TIMEOUT, replace_ttl=True)
return super().tick(**kwargs)
@@ -435,7 +435,7 @@ def acquire_distributed_beat_lock(sender=None, **kwargs) -> None: # noqa: ANN00
logger.debug('beat: Acquiring lock...')
lock = redis_client.lock(
scheduler.lock_key,
timeout=DEFAULT_MAX_LOCK_TIMEOUT,
timeout=_DEFAULT_MAX_LOCK_TIMEOUT,
sleep=scheduler.max_interval,
)