使用celery删除查询集结果的定期任务

时间:2015-05-05 16:46:41

标签: python django celery django-celery periodic-task

我尝试使用芹菜来执行定期任务,以删除未及时激活帐户的用户。下面的屏幕截图显示正确发现并执行了任务,但是当我检查数据库时没有进行任何更改。

芹菜任务:

#tasks.py
from celery.task.schedules import crontab
from celery.decorators import periodic_task
from celery.utils.log import get_task_logger
from .utils import unconfirmed_users_delete

logger = get_task_logger(__name__)

# A periodic task that will run every minute (the symbol "*" means every)
@periodic_task(run_every=(crontab(hour="*", minute="*", day_of_week="*")))
def delete_unconfirmed_users():
    return unconfirmed_users_delete()

要执行的查询集(在django shell中检查并正确工作):

#utils.py
from django.contrib.auth.models import User
from django.utils import timezone

def unconfirmed_users_delete():
    return User.objects.filter(is_active=False).filter(profile__key_expires__lt=timezone.now()).delete()

每分钟都正确调用该任务:

enter image description here

可能出现什么问题?

1 个答案:

答案 0 :(得分:1)

正如@schillingt在大多数时候提到的,我们忘记(重新)启动定期任务的工作流程。

这是因为我们有一个节拍调度程序,它调度执行任务的任务和工作人员。

celery -A my_task beat  # schedule tasks
celery worker -A my_task -l info  # consume tasks

更好的解决方案是让工作人员安排任务和工作。执行。您可以使用

执行此操作
celery worker -A my_task -l info --beat  # schedule & consume tasks

这会安排定期任务并使用它。

相关问题