我阅读了很多关于Django
Celery
的文档,并尝试创建我的第一个cron task
。
目的:
此任务应该能够执行一个函数,该函数每天上午 11:30 清理特定表
我的代码 :
一切似乎都很好,我在芹菜中看到了任务,但没有任何变化。
我的 base.py 文件中有:
INSTALLED_APPS = (
....
'django_cron',
)
CRON_CLASSES = [
"app.cron.cron.DeleteOldToken",
]
CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_IGNORE_RESULT = False
CELERY_TASK_TRACK_STARTED = True
# Add a one-minute timeout to all Celery tasks.
CELERYD_TASK_SOFT_TIME_LIMIT = 60
我有一个 celery.py 文件:
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'main.settings.base')
app = Celery('main')
app.config_from_object('django.conf:settings', namespace='CELERY')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
我有一个 cron.py 文件:
# -*- coding: utf-8 -*-
from django_cron import CronJobBase, Schedule
from ..tasks import delete_old_token
class DeleteOldToken(CronJobBase):
RUN_AT_TIMES = ['11:30']
schedule = Schedule(run_at_times=RUN_AT_TIMES)
code = 'app.delete_old_token'
def do(self):
delete_old_token()
文件 tasks.py:
# -*- coding: utf-8 -*-
import datetime
from datetime import datetime
from celery import shared_task, task
from dateutil.relativedelta import relativedelta
from django.conf import settings
from token_jwt.models import UserToken
@task()
def delete_old_token(self):
for token in UserToken.objects.exclude(date_information__isnull=True):
tdi = token.date_information
if datetime(tdi.year, tdi.month, tdi.day) < datetime.now() + relativedelta(months=2):
token.delete()
命令:
然后我尝试启动此命令:
celery -A main worker --loglevel=info
它显示:
-------------- celery@pydev-jungbluth v4.2.1 (windowlicker)
---- **** -----
--- * *** * -- Linux-4.15.0-45-generic-x86_64-with-debian-buster-sid 2019-02-08 11:29:10
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app: main:0x7fd4befe5cf8
- ** ---------- .> transport: redis://localhost:6379//
- ** ---------- .> results: redis://localhost:6379/
- *** --- * --- .> concurrency: 4 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
-------------- [queues]
.> celery exchange=celery(direct) key=celery
[tasks]
. app.tasks.delete_old_token
[2019-02-08 11:29:10,889: INFO/MainProcess] Connected to redis://localhost:6379//
[2019-02-08 11:29:10,894: INFO/MainProcess] mingle: searching for neighbors
[2019-02-08 11:29:11,914: INFO/MainProcess] mingle: all alone
[2019-02-08 11:29:11,920: INFO/MainProcess] celery@pydev-jungbluth ready.
但什么也没出现。似乎我的 cron 任务不起作用或没有执行。
你有什么想法吗?
编辑:
芹菜节拍给我:
$ celery -A main beat
celery beat v4.2.1 (windowlicker) is starting.
__ - ... __ - _
LocalTime -> 2019-02-08 12:03:22
Configuration ->
. broker -> redis://localhost:6379//
. loader -> celery.loaders.app.AppLoader
. scheduler -> celery.beat.PersistentScheduler
. db -> celerybeat-schedule
. logfile -> [stderr]@%WARNING
. maxinterval -> 5.00 minutes (300s)
django-cron
不与celery
接口。 如果希望芹菜在计划的时间运行作业,则需要使用 django-celery-beat
。 Django celery beat 将允许您通过 django 管理面板设置您的 cron 作业计划。 使用它时,您必须修改芹菜节拍以从以下选项开始:
--scheduler django_celery_beat.schedulers:DatabaseScheduler
完整的设置步骤可以在这里看到。