我需要幫助排除故障celery
與redis
運行的Django應用程序。在這個應用程序中,用戶上傳照片,我試圖運行一個後臺進程,根據用戶投票和上傳後的時間定期對所有照片進行排名(將其視爲基本的類似reddit的排名算法)。Django應用程序故障排除芹菜和Redis
我在我的項目文件夾celery.py
(相同的文件夾settings.py
),含:
from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
app = Celery('myapp', broker='redis://localhost:6379/0', backend='redis://localhost:6379/0',include=['myfolder.myapp.tasks'])
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
app.conf.update(
CELERY_TASK_RESULT_EXPIRES=3600,
)
if __name__ == '__main__':
app.start()
tasks.py
上面的文件,坐落在myapp
文件夾中所提到的,並且包含:
import os
from myproject import celery_app1
import time
from myapp.models import Photo
@celery_app1.task(name='tasks.rank_all_photos')
def rank_all_photos():
for photo in Photo.objects.all():
photo.set_rank()
print "ranked"
__init__.py
myproject文件夾中包含:
from __future__ import absolute_import
from .celery import app as celery_app1
最後,從settings.py
相關配置如下:
import djcelery
djcelery.setup_loader()
BROKER_URL = 'redis://localhost:6379/0'
CELERY_IMPORTS = ('myapp.tasks',)
CELERY_ALWAYS_EAGER = False
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_IGNORE_RESULT=True
from datetime import timedelta
CELERYBEAT_SCHEDULE = {
'tasks.rank_all_photos': {
'task': 'tasks.rank_all_photos',
'schedule': timedelta(seconds=10),
},
}
CELERY_TIMEZONE = 'UTC'
注意'djcelery'
也包含在INSTALLED_APPS。對我而言,以上所有內容都看起來是正確的。當我運行celery worker -A myproject --loglevel=INFO
,我看到輸出:
---- **** -----
--- * *** * -- Linux-3.16.0-30-generic-x86_64-with-Ubuntu-14.04-trusty
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app: myapp:0x7f0a15acb310
- ** ---------- .> transport: redis://localhost:6379/0
- ** ---------- .> results: redis://localhost:6379/0
- *** --- * --- .> concurrency: 2 (prefork)
-- ******* ----
--- ***** ----- [queues]
-------------- .> celery exchange=celery(direct) key=celery
[tasks]
. tasks.rank_all_photos
[2016-05-17 02:19:19,733: INFO/MainProcess] Connected to redis://localhost:6379/0
[2016-05-17 02:19:19,745: INFO/MainProcess] mingle: searching for neighbors
[2016-05-17 02:19:20,750: INFO/MainProcess] mingle: all alone
/home/hassan/.virtualenvs/myenv/local/lib/python2.7/site-packages/celery/fixups/django.py:265: UserWarning: Using settings.DEBUG leads to a memory leak, never use this setting in production environments!
warnings.warn('Using settings.DEBUG leads to a memory leak, never '
[2016-05-17 02:19:20,761: WARNING/MainProcess] /home/hassan/.virtualenvs/myenv/local/lib/python2.7/site-packages/celery/fixups/django.py:265: UserWarning: Using settings.DEBUG leads to a memory leak, never use this setting in production environments!
warnings.warn('Using settings.DEBUG leads to a memory leak, never '
[2016-05-17 02:19:20,761: WARNING/MainProcess] [email protected] ready.
我看到輸出,但我沒有看到任何證據表明tasks.py
進行了處理。沒有輸出被打印,也沒有任何排名完成。
我是初學者,所以我可能在這裏錯過了一些基本的東西。你可以看一看,爲我解決這個問題嗎?我目前只是試圖在本地測試這個fg - 一旦它正在運行,我會考慮在生產環境中進行守護。謝謝。