我遇到了 django-celery 未在指定时间运行的问题,但它总是每 1 分钟运行一次。下面我粘贴了 tasks.py,settings.py 和 celery.py。我已经用djcelery和芹菜进行了测试。我用的是芹菜节拍。
celery.py(编辑为仅提供特定信息并避免机密信息。
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from django.conf import settings
from celery.schedules import crontab
from datetime import timedelta
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'netadc.settings')
app = Celery('netadc')
app.config_from_object(settings, namespace='CELERY')
app.autodiscover_tasks()
app = Celery('proj',
broker='redis://',
backend='redis://',
include=['standalone.tasks'])
CELERY_BEAT_SCHEDULE = {
"runs-every-30-seconds": {
"task": "tasks.cron_span",
"schedule": 30.0,
"args": ()
},
}
if __name__ == '__main__':
app.start()
settings.py :-
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'home',
'ivpn',
'nsp',
'lib',
'ciscoaci',
'standalone',
'arista',
'spa',
'inet',
#'django_countries',
'djcelery',
#'debug_toolbar',
'celery',
'django_celery_beat',
)
ROOT_URLCONF = 'netadc.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'ivpn.context_processors.vpnreq_info',
'django.template.context_processors.debug',
'django.template.context_processors.request',
#'django.core.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'netadc.wsgi.application'
# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGE_CODE = 'en-us'
#TIME_ZONE = 'UTC'
TIME_ZONE = 'America/Phoenix'
USE_TZ = True
## Celery specific settings
CELERY_BROKER_URL = 'redis://localhost:6379/0'
CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler'
CELERY_RESULT_BACKEND = 'redis://'
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'America/Phoenix'
CELERY_ENABLE_UTC = True
tasks.py :-
from __future__ import absolute_import, division, print_function
# -*- coding: utf-8 -*-
from __builtin__ import *
#from future.builtins.disabled import *
from django.template.context_processors import csrf
from django.http import Http404
from django.shortcuts import get_object_or_404,render
from django.http import HttpResponse, HttpResponseRedirect
from django.template import loader
# Create your views here.
from django.shortcuts import render
#from django.urls import
from django.core.urlresolvers import reverse
import json
import os
import requests
import argparse
import collections
import datetime
from django.template import RequestContext
from standalone.forms import UploadFileForm
from django.contrib import messages
#import pyexcel.ext.xls
import urllib
import urllib2
import cookielib
import sys, getopt
import StringIO,contextlib
import xml.etree.ElementTree as ET
from standalone.views import *
from standalone.models import Fabric, Span
#from celery.decorators import task
from celery import task
from celery.utils.log import get_task_logger
from celery import shared_task
from celery.schedules import crontab
from celery.decorators import periodic_task
from time import time as time1
import time
from datetime import timedelta
import datetime
logger = get_task_logger(__name__)
@task()
def cron_span():
########### code #############################
我已经将时区更改为所有地方的时区相同,并且我还使用以下命令更新了数据库调度程序:- 从 djcelery.models import PeriodicTask PeriodicTask.objects.update(last_run_at=None)
任何帮助将不胜感激。
芹菜原木:-
[2018-03-18 11:09:55,078: INFO/Beat] Writing entries...
[2018-03-18 11:10:00,001: INFO/Beat] Scheduler: Sending due task standalone.tasks.cron_span (standalone.tasks.cron_span)
[2018-03-18 11:10:00,003: INFO/MainProcess] Received task: standalone.tasks.cron_span[707c0da7-d76b-4c4d-92a4-3839f5cd6527]
[2018-03-18 11:10:00,005: WARNING/ForkPoolWorker-2] where are we
[2018-03-18 11:10:00,011: WARNING/ForkPoolWorker-2] each_all_values
[2018-03-18 11:10:00,011: WARNING/ForkPoolWorker-2] <type 'datetime.datetime'>
[2018-03-18 11:10:00,011: WARNING/ForkPoolWorker-2] each_all_values.time_span
[2018-03-18 11:10:00,012: WARNING/ForkPoolWorker-2] <type 'float'>
[2018-03-18 11:10:00,012: WARNING/ForkPoolWorker-2] present_time
[2018-03-18 11:10:00,013: ERROR/ForkPoolWorker-2] Task standalone.tasks.cron_span[707c0da7-d76b-4c4d-92a4-3839f5cd6527] raised unexpected: ValueError('invalid literal for float(): 2018-03-12 21:22:05',)
Traceback (most recent call last):
File "/root/.venvs/netadc/lib/python2.7/site-packages/celery-4.1.0-py2.7.egg/celery/app/trace.py", line 374, in trace_task
R = retval = fun(*args, **kwargs)
File "/root/.venvs/netadc/lib/python2.7/site-packages/celery-4.1.0-py2.7.egg/celery/app/trace.py", line 629, in __protected_call__
return self.run(*args, **kwargs)
File "/apps/workspace/netadc/standalone/tasks.py", line 57, in cron_span
time_taken = present_time - float(each_all_values.time_span.strftime("%Y-%m-%d %H:%M:%S")) # time_taken is in seconds
ValueError: invalid literal for float(): 2018-03-12 21:22:05
[2018-03-18 11:11:00,001: INFO/Beat] Scheduler: Sending due task standalone.tasks.cron_span (standalone.tasks.cron_span)
[2018-03-18 11:11:00,004: INFO/MainProcess] Received task: standalone.tasks.cron_span[23928119-c93a-41c5-9bc7-e2c909324b6f]
[2018-03-18 11:11:00,005: WARNING/ForkPoolWorker-5] where are we
[2018-03-18 11:11:00,012: WARNING/ForkPoolWorker-5] each_all_values
[2018-03-18 11:11:00,012: WARNING/ForkPoolWorker-5] <type 'datetime.datetime'>
[2018-03-18 11:11:00,012: WARNING/ForkPoolWorker-5] each_all_values.time_span
[2018-03-18 11:11:00,012: WARNING/ForkPoolWorker-5] <type 'float'>
[2018-03-18 11:11:00,013: WARNING/ForkPoolWorker-5] present_time
[2018-03-18 11:11:00,014: ERROR/ForkPoolWorker-5] Task standalone.tasks.cron_span[23928119-c93a-41c5-9bc7-e2c909324b6f] raised unexpected: ValueError('invalid literal for float(): 2018-03-12 21:22:05',)
Traceback (most recent call last):
File "/root/.venvs/netadc/lib/python2.7/site-packages/celery-4.1.0-py2.7.egg/celery/app/trace.py", line 374, in trace_task
R = retval = fun(*args, **kwargs)
File "/root/.venvs/netadc/lib/python2.7/site-packages/celery-4.1.0-py2.7.egg/celery/app/trace.py", line 629, in __protected_call__
return self.run(*args, **kwargs)
File "/apps/workspace/netadc/standalone/tasks.py", line 57, in cron_span
time_taken = present_time - float(each_all_values.time_span.strftime("%Y-%m-%d %H:%M:%S")) # time_taken is in seconds
ValueError: invalid literal for float(): 2018-03-12 21:22:05
如果要每30 秒运行一次任务,则应执行以下操作:
CELERY_BEAT_SCHEDULE = {
"runs-every-30-seconds": {
"task": "tasks.cron_span",
"schedule": timedelta(seconds=30),
"args": ()
},
}
对于旧的芹菜版本。
确保在settings.py
文件中设置了CELERY_ENABLE_UTC = False
,否则芹菜会将您的日程安排转换为 UTC。