2022-01-13 06:18:32 +00:00
|
|
|
from django.utils import translation
|
|
|
|
from django.core.cache import cache
|
2022-01-17 03:07:27 +00:00
|
|
|
from celery.signals import task_prerun, task_postrun, before_task_publish
|
|
|
|
|
|
|
|
from common.db.utils import close_old_connections
|
2022-01-13 06:18:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
TASK_LANG_CACHE_KEY = 'TASK_LANG_{}'
|
|
|
|
TASK_LANG_CACHE_TTL = 1800
|
|
|
|
|
|
|
|
|
|
|
|
@before_task_publish.connect()
|
|
|
|
def before_task_publish(headers=None, **kwargs):
|
|
|
|
task_id = headers.get('id')
|
|
|
|
current_lang = translation.get_language()
|
|
|
|
key = TASK_LANG_CACHE_KEY.format(task_id)
|
|
|
|
cache.set(key, current_lang, 1800)
|
|
|
|
|
|
|
|
|
|
|
|
@task_prerun.connect()
|
|
|
|
def on_celery_task_pre_run(task_id='', **kwargs):
|
2022-01-17 03:07:27 +00:00
|
|
|
# 关闭之前的数据库连接
|
|
|
|
close_old_connections()
|
|
|
|
|
|
|
|
# 保存 Lang context
|
2022-01-13 06:18:32 +00:00
|
|
|
key = TASK_LANG_CACHE_KEY.format(task_id)
|
|
|
|
task_lang = cache.get(key)
|
|
|
|
if task_lang:
|
|
|
|
translation.activate(task_lang)
|
2022-01-17 03:07:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
@task_postrun.connect()
|
|
|
|
def on_celery_task_post_run(**kwargs):
|
|
|
|
close_old_connections()
|