我正在尝试在项目中集成django_celery_beat。 当我使用django管理面板设置任务时,数据库中的条目被正确反映,当我开始项目任务成功执行时。但是,在第一次执行后,每次连续尝试发送任务都会失败,或者任何一条消息都没有发送给经纪人。
我的设置:
django_celery_beat : 1.0.1
celery: 4.0.1
在celeryconfig中设置:
CELERY_ENABLE_UTC = True
BROKER_URL = 'amqp://guest:guest@localhost:5672//'
CELERYBEAT_SCHEDULER = 'django_celery_beat.schedulers.DatabaseScheduler'
settings.py
# -*- coding: utf-8 -*-
import os
import tempfile
import time
from buy import constants as buy_constants
PROJECT_ROOT = os.path.realpath(os.path.dirname(__file__))
# CSRF_COOKIE_SECURE = True
# SESSION_COOKIE_SECURE = True
ADMIN_SITE_ENABLED = False
WAIT_BETWEEN_RATING = 7 # In days
WAIT_BETWEEN_EMAIL_VERIFY = 30 # In minutes
WAIT_BETWEEN_RECOMMENDATIONS = 1 # In minutes
WAIT_BETWEEN_CONNECTION_UPDATE = 60*24*5 # In minutes
SITEMAP_CACHE = 86400
DEBUG = False
TEMPLATE_DEBUG = DEBUG
ADMINS = (('Pooja Shete', 'pooja.shete@gmail.com'),)
MANAGERS = ADMINS
DATABASES = {}
# Hosts/domain names that are valid for this site; required if DEBUG is False
ALLOWED_HOSTS = ['*']
# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# In a Windows environment this must be set to your system time zone.
TIME_ZONE = 'America/Chicago'
# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en'
SITE_ID = 1
# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True
# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True
# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = True
# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/var/www/example.com/media/"
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media')
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://example.com/media/", "http://media.example.com/"
MEDIA_URL = '/media/'
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/var/www/example.com/static/"
# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(PROJECT_ROOT, "static"),
)
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_STORAGE = 'require.storage.OptimizedCachedStaticFilesStorage'
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'pipeline.finders.PipelineFinder',
'pipeline.finders.CachedFileFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
# Make this unique, and don't share it with anybody.
SECRET_KEY = 'c28yui+jyipz5prse%!o^u_e06$!*_daps8b9+km_aa51f=i%='
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django_user_agents.middleware.UserAgentMiddleware',
'django.middleware.security.SecurityMiddleware',
'logs.middleware.ContextMiddleware',
'middleware.locale.LocaleMiddleware',
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
DJANGO_CONTEXT_LOGGING_EXTRACTOR = lambda request: {}
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
),
'PAGINATE_BY': 10
}
# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'app.wsgi.application'
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__), '..', 'templates').replace('\\','/'),
os.path.join(PROJECT_ROOT, 'templates'),
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'django.contrib.sitemaps',
'django.contrib.admin',
'django.contrib.admindocs',
'billing',
'django_user_agents',
'pipeline',
'celery',
'django_celery_beat',
)
SITE_ID = 1
import django.db.models.options as options
options.DEFAULT_NAMES = options.DEFAULT_NAMES + ('db_name',)
SESSION_SERIALIZER = 'django.contrib.sessions.serializers.JSONSerializer'
CELERYBEAT_SCHEDULER = 'django_celery_beat.schedulers.DatabaseScheduler'
TEMPLATE_CONTEXT_PROCESSORS = ('django.core.context_processors.request',"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages",
"django.core.context_processors.csrf",
)
PIPELINE_DISABLE_WRAPPER = True
PIPELINE_COMPILERS = (
'pipeline.compilers.sass.SASSCompiler',
)
PIPELINE_YUGLIFY_BINARY = '/usr/local/bin/yuglify'
PIPELINE_JS_COMPRESSOR = 'pipeline.compressors.uglifyjs.UglifyJSCompressor'
PIPELINE_CLOSURE_ARGUMENTS = '--compilation_level=WHITESPACE_ONLY'
PIPELINE_CSS_COMPRESSOR = 'pipeline.compressors.yui.YUICompressor'
CACHES = {}
# Change this value in local_config as per requirement to use dummy paypal
DUMMY_PAYPAL = False
# Initialize cached object
##############################
try:
INITIALIZE_FILE_CACHE
except NameError:
try:
from django.core.cache import get_cache
cache = get_cache('redis_cache')
try:
wp = cache.get('word_parser', None)
except:
wp = None
if not wp:
from bin.cron.wordparser import WordParser
wp = WordParser()
cache.set('word_parser', wp, timeout=None)
except ImportError:
pass
gettext = lambda s: s
LANGUAGES = (
('en', gettext('English')),
('fr', gettext('French')),
('es', gettext('Spanish')),
('ja_JP', gettext('Japanese')),
)
PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))
LOCALE_PATHS = (
os.path.join(PROJECT_PATH, '../locale'),
)
用于启动celerybeat的命令:
celery beat -A app-S django_celery_beat.schedulers.DatabaseScheduler --loglevel=INFO
用于启动工人的命令:
celery worker -A app--loglevel=INFO
当我开始吃芹菜时,我确实例外:
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/celery/worker/pidbox.py", line 42, in on_message
self.node.handle_message(body, message)
File "/usr/local/lib/python3.4/dist-packages/kombu/pidbox.py", line 129, in handle_message
return self.dispatch(**body)
File "/usr/local/lib/python3.4/dist-packages/kombu/pidbox.py", line 112, in dispatch
ticket=ticket)
File "/usr/local/lib/python3.4/dist-packages/kombu/pidbox.py", line 135, in reply
serializer=self.mailbox.serializer)
File "/usr/local/lib/python3.4/dist-packages/kombu/pidbox.py", line 265, in _publish_reply
**opts
File "/usr/local/lib/python3.4/dist-packages/kombu/messaging.py", line 181, in publish
exchange_name, declare,
File "/usr/local/lib/python3.4/dist-packages/kombu/messaging.py", line 203, in _publish
mandatory=mandatory, immediate=immediate,
File "/usr/local/lib/python3.4/dist-packages/amqp/channel.py", line 1748, in _basic_publish
(0, exchange, routing_key, mandatory, immediate), msg
File "/usr/local/lib/python3.4/dist-packages/amqp/abstract_channel.py", line 64, in send_method
conn.frame_writer(1, self.channel_id, sig, args, content)
File "/usr/local/lib/python3.4/dist-packages/amqp/method_framing.py", line 174, in write_frame
write(view[:offset])
File "/usr/local/lib/python3.4/dist-packages/amqp/transport.py", line 269, in write
self._write(s)
BrokenPipeError: [Errno 32] Broken pipe
登录beat.stderr.log:
[2017-06-12 05:40:00,002: INFO/MainProcess] Scheduler: Sending due task pooja_task (app.tasks.send_test_mail)
[2017-06-12 05:41:00,002: INFO/MainProcess] Scheduler: Sending due task pooja_task (app.tasks.send_test_mail)
[2017-06-12 05:41:00,002: INFO/MainProcess] Scheduler: Sending due task pooja_task (app.tasks.send_test_mail)
然而,从上面发送的所有任务开始,芹菜只选择第一个任务,并在我开始服务器时执行。所有其他消息都被避免了。
任何人都可以帮助我。