daren_project/role_based_system/celery.py
2025-04-29 10:22:57 +08:00

27 lines
834 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
from celery import Celery
from celery.schedules import crontab
# 设置默认Django设置模块
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'role_based_system.settings')
app = Celery('role_based_system')
# 使用字符串表示避免pickle序列化可能带来的安全问题
app.config_from_object('django.conf:settings', namespace='CELERY')
# 自动从所有注册的Django应用中加载任务
app.autodiscover_tasks()
# 配置定期任务
app.conf.beat_schedule = {
# 每小时检查一次未发布的视频
'check-scheduled-videos-every-hour': {
'task': 'user_management.tasks.check_scheduled_videos',
'schedule': crontab(minute=0, hour='*/1'), # 每小时运行一次
},
}
@app.task(bind=True, ignore_result=True)
def debug_task(self):
print(f'Request: {self.request!r}')