我有一個Django應用程序,我的目標是通過redis通過芹菜運行任務。與主管Django應用程序運行芹菜工人故障排除
項目文件夾結構如下:
/mhb11/myfolder/myproject
├── myproject
│ ├── celery.py # The Celery app file
│ ├── __init__.py # The project module file (modified)
│ ├── settings.py # Including Celery settings
│ ├── urls.py
│ └── wsgi.py
├── manage.py
├── celerybeat-schedule
└── myapp
├── __init__.py
├── models.py
├── tasks.py # File containing tasks for this app
├── tests.py
└── views.py
我celery.conf
在/etc/supervisor/conf.d
包含:
[program:celery]
command=/home/mhb11/.virtualenvs/myenv/local/lib/python2.7/site-packages/celery/bin/celery --app=myproject.celery:app worker -l info
command=/home/mhb11/.virtualenvs/myenv/local/lib/python2.7/site-packages/celery/bin/celery --app=myproject.celery:app beat -l info
directory = /home/mhb11/myfolder/myproject
user=mhb11
numprocs=1
stdout_logfile = /etc/supervisor/logs/celery-worker.log
stderr_logfile = /etc/supervisor/logs/celery-worker.log
autostart = true
autorestart = true
startsecs=10
stopwaitsecs = 600
killasgroup = true
priority = 998
而在/etc/supervisor/logs
,我有叫celery-worker.log
一個空文件。設置此功能後,我跑到下面的命令:
sudo supervisorctl reread
sudo supervisorctl update
這樣做了以後,我的芹菜工人應該開始,但他們沒有。即在我設置的celery-worker.log
文件中什麼也沒有顯示。我不知道我錯過了什麼,因爲這是我第一次設置所有這些。你能幫我解決這個問題嗎?
djcelery
是INSTALLED_APPS的一部分。此外,在settings.py
其他相關設置:
import djcelery
djcelery.setup_loader()
BROKER_URL = 'redis://localhost:6379/0'
BROKER_TRANSPORT = 'redis'
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_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler'
# CELERYBEAT_SCHEDULE = {
# 'tasks.rank_all_photos': {
# 'task': 'tasks.rank_all_photos',
# 'schedule': timedelta(seconds=30),
# },
# }
CELERY_TIMEZONE = 'UTC'
我celery.py
包含:
#this is the celery daemon
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()
__init__.py
包含:
from __future__ import absolute_import
from .celery import app as celery_app1
tasks.py
包含:
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.order_by('-id')[:400]:
photo.set_rank()
最後,在我的Django管理面板中,我還設置了crontab
和periodic task
。
我該怎麼做才能讓所有事情都開始?
沒錯的任務是週期性的。我會試試這個並回復你。感謝您幫助並清除了Tahir的概念:-) –
嘗試了它(更新了問題中的'conf.d'文件,以便您知道它現在的樣子),但是我的日誌中沒有顯示任何內容。任何其他線索,因爲這不起作用?附:之後我也會重讀和更新。 –
@HassanBaig你需要通過主管創建2個獨立的過程。它將需要2個獨立的主管文件。我已經添加了這兩個文件來回答。 –