mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-09-13 05:49:07 +00:00
perf: 优化 celery 任务中的数据库连接问题
perf: add comment
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
from django.utils import translation
|
||||
from django.core.cache import cache
|
||||
from celery.signals import task_prerun, before_task_publish
|
||||
from celery.signals import task_prerun, task_postrun, before_task_publish
|
||||
|
||||
from common.db.utils import close_old_connections
|
||||
|
||||
|
||||
TASK_LANG_CACHE_KEY = 'TASK_LANG_{}'
|
||||
@@ -17,7 +19,16 @@ def before_task_publish(headers=None, **kwargs):
|
||||
|
||||
@task_prerun.connect()
|
||||
def on_celery_task_pre_run(task_id='', **kwargs):
|
||||
# 关闭之前的数据库连接
|
||||
close_old_connections()
|
||||
|
||||
# 保存 Lang context
|
||||
key = TASK_LANG_CACHE_KEY.format(task_id)
|
||||
task_lang = cache.get(key)
|
||||
if task_lang:
|
||||
translation.activate(task_lang)
|
||||
|
||||
|
||||
@task_postrun.connect()
|
||||
def on_celery_task_post_run(**kwargs):
|
||||
close_old_connections()
|
||||
|
Reference in New Issue
Block a user