diff --git a/green_classroom/celery.py b/green_classroom/celery.py new file mode 100644 index 0000000..064be95 --- /dev/null +++ b/green_classroom/celery.py @@ -0,0 +1,49 @@ +import os +from celery import Celery +from django.conf import settings + +# 设置默认Django设置模块 +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'green_classroom.settings') + +app = Celery('green_classroom') + +# 使用Django的设置文件 +app.config_from_object('django.conf:settings', namespace='CELERY') + +# 自动发现任务 +app.autodiscover_tasks(lambda: settings.INSTALLED_APPS) + +# 配置任务路由 +app.conf.task_routes = { + 'core.tasks.*': {'queue': 'crawler'}, + 'core.tasks.crawl_website': {'queue': 'crawler'}, + 'core.tasks.crawl_all_websites': {'queue': 'crawler'}, +} + +# 配置任务序列化 +app.conf.task_serializer = 'json' +app.conf.result_serializer = 'json' +app.conf.accept_content = ['json'] + +# 配置时区 +app.conf.timezone = settings.TIME_ZONE + +# 配置任务执行时间限制 +app.conf.task_time_limit = 30 * 60 # 30分钟 +app.conf.task_soft_time_limit = 25 * 60 # 25分钟 + +# 配置重试策略 +app.conf.task_acks_late = True +app.conf.task_reject_on_worker_lost = True + +# 配置结果后端 +app.conf.result_backend = settings.CELERY_RESULT_BACKEND + +# 配置工作进程 +app.conf.worker_prefetch_multiplier = 1 +app.conf.worker_max_tasks_per_child = 1000 + + +@app.task(bind=True) +def debug_task(self): + print(f'Request: {self.request!r}')