2018-04-02 05:19:31 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2019-01-15 02:23:30 +00:00
|
|
|
import logging
|
2018-04-02 05:19:31 +00:00
|
|
|
|
|
|
|
from celery import subtask
|
2019-01-15 02:23:30 +00:00
|
|
|
from celery.signals import (
|
2019-02-19 12:03:02 +00:00
|
|
|
worker_ready, worker_shutdown, after_setup_logger
|
2019-01-15 02:23:30 +00:00
|
|
|
)
|
2023-06-21 09:42:25 +00:00
|
|
|
from django.core.cache import cache
|
2018-04-02 05:19:31 +00:00
|
|
|
from django_celery_beat.models import PeriodicTask
|
|
|
|
|
2019-01-15 02:23:30 +00:00
|
|
|
from common.utils import get_logger
|
|
|
|
from .decorator import get_after_app_ready_tasks, get_after_app_shutdown_clean_tasks
|
2021-01-25 11:34:41 +00:00
|
|
|
from .logger import CeleryThreadTaskFileHandler
|
2018-04-02 05:19:31 +00:00
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
2019-01-28 04:18:53 +00:00
|
|
|
safe_str = lambda x: x
|
2018-04-02 05:19:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
@worker_ready.connect
|
2019-01-15 02:23:30 +00:00
|
|
|
def on_app_ready(sender=None, headers=None, **kwargs):
|
2018-04-02 05:19:31 +00:00
|
|
|
if cache.get("CELERY_APP_READY", 0) == 1:
|
|
|
|
return
|
|
|
|
cache.set("CELERY_APP_READY", 1, 10)
|
|
|
|
tasks = get_after_app_ready_tasks()
|
2019-01-15 02:23:30 +00:00
|
|
|
logger.debug("Work ready signal recv")
|
|
|
|
logger.debug("Start need start task: [{}]".format(", ".join(tasks)))
|
2018-04-02 05:19:31 +00:00
|
|
|
for task in tasks:
|
2024-04-17 02:48:53 +00:00
|
|
|
periodic_task = PeriodicTask.objects.filter(task=task).first()
|
|
|
|
if periodic_task and not periodic_task.enabled:
|
|
|
|
logger.debug("Periodic task [{}] is disabled!".format(task))
|
|
|
|
continue
|
2018-04-02 05:19:31 +00:00
|
|
|
subtask(task).delay()
|
|
|
|
|
|
|
|
|
|
|
|
@worker_shutdown.connect
|
2019-01-15 02:23:30 +00:00
|
|
|
def after_app_shutdown_periodic_tasks(sender=None, **kwargs):
|
2018-04-02 05:19:31 +00:00
|
|
|
if cache.get("CELERY_APP_SHUTDOWN", 0) == 1:
|
|
|
|
return
|
|
|
|
cache.set("CELERY_APP_SHUTDOWN", 1, 10)
|
|
|
|
tasks = get_after_app_shutdown_clean_tasks()
|
2019-01-15 02:23:30 +00:00
|
|
|
logger.debug("Worker shutdown signal recv")
|
|
|
|
logger.debug("Clean period tasks: [{}]".format(', '.join(tasks)))
|
2018-04-02 05:19:31 +00:00
|
|
|
PeriodicTask.objects.filter(name__in=tasks).delete()
|
|
|
|
|
|
|
|
|
2019-01-15 02:23:30 +00:00
|
|
|
@after_setup_logger.connect
|
|
|
|
def add_celery_logger_handler(sender=None, logger=None, loglevel=None, format=None, **kwargs):
|
|
|
|
if not logger:
|
2018-04-02 07:54:49 +00:00
|
|
|
return
|
2021-01-25 11:34:41 +00:00
|
|
|
task_handler = CeleryThreadTaskFileHandler()
|
2019-02-19 08:59:00 +00:00
|
|
|
task_handler.setLevel(loglevel)
|
2023-06-21 09:45:09 +00:00
|
|
|
formatter = logging.Formatter(format)
|
2019-02-19 08:59:00 +00:00
|
|
|
task_handler.setFormatter(formatter)
|
|
|
|
logger.addHandler(task_handler)
|