2026-01-04 19:17:33 +08:00
|
|
|
import os
|
|
|
|
|
from celery import Celery
|
|
|
|
|
from django.conf import settings
|
2026-05-25 21:09:43 +08:00
|
|
|
from celery.schedules import crontab
|
2026-01-04 19:17:33 +08:00
|
|
|
|
|
|
|
|
# 设置默认的Django设置模块
|
|
|
|
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'diary_family.settings')
|
|
|
|
|
|
|
|
|
|
# 创建Celery应用实例
|
|
|
|
|
app = Celery('diary_family')
|
|
|
|
|
|
|
|
|
|
# 从Django设置中读取配置
|
|
|
|
|
app.config_from_object('django.conf:settings', namespace='CELERY')
|
|
|
|
|
|
|
|
|
|
# 自动发现任务
|
|
|
|
|
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
|
|
|
|
|
|
2026-05-25 21:09:43 +08:00
|
|
|
# Celery Beat 周期任务配置
|
|
|
|
|
app.conf.beat_schedule = {
|
|
|
|
|
'cleanup-expired-temp-files': {
|
|
|
|
|
'task': 'core.tasks.cleanup_expired_temp_files',
|
|
|
|
|
'schedule': crontab(minute=0), # 每小时整点执行
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-04 19:17:33 +08:00
|
|
|
@app.task(bind=True)
|
|
|
|
|
def debug_task(self):
|
|
|
|
|
"""调试任务"""
|
|
|
|
|
print(f'Request: {self.request!r}')
|