我的应用程序中有使用芹菜运行的任务。我在我的开发环境中毫无压力地设置了它,并且它与 redis 作为经纪人完美配合。昨天我将代码传输到我的服务器并设置 redis,但芹菜无法发现任务。代码是一样的。
我的celery_conf.py
文件(最初celery.py
(:
# coding: utf-8
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from django.conf import settings
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'vertNews.settings')
app = Celery('vertNews')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
设置中的芹菜配置
# Celery Configuration
CELERY_TASK_ALWAYS_EAGER = False
CELERY_BROKER_URL = SECRETS['celery']['broker_url']
CELERY_RESULT_BACKEND = SECRETS['celery']['result_backend']
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = TIME_ZONE
根应用的__init__.py
# coding: utf-8
from __future__ import absolute_import, unicode_literals
from .celery_conf import app as celery_app
__all__ = ['celery_app']
我的任务
# coding=utf-8
from __future__ import unicode_literals, absolute_import
import logging
from celery.schedules import crontab
from celery.task import periodic_task
from .api import fetch_tweets, delete_tweets
logger = logging.getLogger(__name__)
@periodic_task(
run_every=(crontab(minute=10, hour='0, 6, 12, 18, 23')),
name="fetch_tweets_task",
ignore_result=True)
def fetch_tweets_task():
logger.info("Tweet download started")
fetch_tweets()
logger.info("Tweet download and summarization finished")
@periodic_task(
run_every=(crontab(minute=13, hour=13)),
name="delete_tweets_task",
ignore_result=True)
def delete_tweets_task():
logger.info("Tweet deletion started")
delete_tweets()
logger.info("Tweet deletion finished")
在远程服务器中运行时的结果(不起作用(
(trendiz) kenneth@bots:~/projects/verticals-news/src$ celery -A vertNews beat -l debug
Trying import production.py settings...
celery beat v4.0.2 (latentcall) is starting.
__ - ... __ - _
LocalTime -> 2017-04-03 13:55:49
Configuration ->
. broker -> redis://localhost:6379//
. loader -> celery.loaders.app.AppLoader
. scheduler -> celery.beat.PersistentScheduler
. db -> celerybeat-schedule
. logfile -> [stderr]@%DEBUG
. maxinterval -> 5.00 minutes (300s)
[2017-04-03 13:55:49,770: DEBUG/MainProcess] Setting default socket timeout to 30
[2017-04-03 13:55:49,771: INFO/MainProcess] beat: Starting...
[2017-04-03 13:55:49,785: DEBUG/MainProcess] Current schedule:
[2017-04-03 13:55:49,785: DEBUG/MainProcess] beat: Ticking with max interval->5.00 minutes
[2017-04-03 13:55:49,785: DEBUG/MainProcess] beat: Waking up in 5.00 minutes.
当我在开发服务器(工作(中运行时的结果
LocalTime -> 2017-04-03 14:16:19
Configuration ->
. broker -> redis://localhost:6379//
. loader -> celery.loaders.app.AppLoader
. scheduler -> celery.beat.PersistentScheduler
. db -> celerybeat-schedule
. logfile -> [stderr]@%DEBUG
. maxinterval -> 5.00 minutes (300s)
[2017-04-03 14:16:19,919: DEBUG/MainProcess] Setting default socket timeout to 30
[2017-04-03 14:16:19,919: INFO/MainProcess] beat: Starting...
[2017-04-03 14:16:19,952: DEBUG/MainProcess] Current schedule:
<ScheduleEntry: fetch_tweets_task fetch_tweets_task() <crontab: 36 0, 6, 12, 18, 22 * * * (m/h/d/dM/MY)>
<ScheduleEntry: delete_tweets_task delete_tweets_task() <crontab: 13 13 * * * (m/h/d/dM/MY)>
[2017-04-03 14:16:19,952: DEBUG/MainProcess] beat: Ticking with max interval->5.00 minutes
[2017-04-03 14:16:19,953: DEBUG/MainProcess] beat: Waking up in 5.00 minutes.
我在这两种环境中都运行 python 3.5 和芹菜 4.0.2
我不知道
问题到底是什么,但是清除项目中的所有 *.pyc 文件解决了这个问题