You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Celery-beat间隔日调度任务无法按时执行问题求助

Troubleshooting Celery Beat Daily/Weekly Task Trigger Issues

Let's break down the potential issues that could be preventing your daily/weekly interval tasks from firing, even though they show up correctly in the Django admin:

1. Timezone Misconfiguration

A common culprit here is mismatched timezones between Django and Celery Beat. You're using timezone.now() for start_time, which uses Django's configured timezone, but if Celery Beat is using a different timezone, it might calculate the next run time incorrectly.

  • Check if CELERY_TIMEZONE is set in your Django settings and matches TIME_ZONE. For example:
    TIME_ZONE = 'Asia/Shanghai'
    CELERY_TIMEZONE = TIME_ZONE
    
  • Without this alignment, the start_time you set could be interpreted as UTC by Celery, leading to the task not triggering when you expect it to.

2. Invalid IntervalSchedule Period Value

Double-check the period value you're passing to IntervalSchedule.objects.get_or_create(). In django-celery-beat, the period field uses lowercase string choices like 'days', 'hours', etc.

  • If IntervalSchedule.DAYS is defined as an uppercase string (e.g., 'DAYS') in your code, that's invalid—Celery Beat won't recognize it. Try replacing it with the literal string 'days' instead:
    schedule, created = IntervalSchedule.objects.get_or_create(every=7, period='days')
    
  • You can verify the valid choices by checking the django_celery_beat.models.IntervalSchedule source code, which defines period choices as ('days', _('Days')), ('hours', _('Hours')), ('minutes', _('Minutes')), ('seconds', _('Seconds')).

3. Celery Beat Not Using the Database Scheduler

Even though you've set CELERY_BEAT_SCHEDULER in your settings, you need to ensure the Celery Beat process is started with the correct scheduler argument.

  • Your Beat startup command should explicitly specify the database scheduler:
    celery -A your_project_name beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler
    
  • If you omit the --scheduler flag, Celery Beat will use the default scheduler (which doesn't read tasks from the Django database), so your PeriodicTask entries won't be picked up.

4. Logging for Debugging

Enable debug-level logging for Celery Beat to see exactly what's happening with your scheduled tasks:

  • Start Beat with debug logging:
    celery -A your_project_name beat -l debug
    
  • Look for log lines related to your start_primaries task—you should see entries like "Scheduler: Sending task start_primaries ..." when it's supposed to trigger. If you don't see these, it means Beat isn't recognizing the task's schedule. If you see errors, they'll point you directly to the issue (e.g., timezone mismatches, invalid period values).

5. Task Registration Verification

While your minute/hour tasks work, it's still worth confirming that the start_primaries task is correctly registered with Celery:

  • Ensure the task is decorated with @app.task(name='start_primaries') (matching the name you're passing to PeriodicTask.objects.create()).
  • Verify that the app containing this task is listed in INSTALLED_APPS so Celery can discover it.

Quick Test to Validate

To rule out code issues, try creating a simple 1-day interval task manually via the Django admin and see if it triggers. If it does, the problem is likely in how you're creating the task in your setup_task method (most likely the period value or timezone).

内容的提问来源于stack exchange,提问作者Andrew Gorshenin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 20:02:34