2016-04-25 147 views
2

我在我的django項目中使用芹菜來運行定期任務。繼芹菜網站上的標準教程,這裏是我的項目結構 項目芹菜定期任務不啓動

|_ settings.py 
|_ __init__.py 
|_ celery_app.py (instead of celery.py) 
|_ app 
    |_ tasks.py 

settings.py中相關部分看起來是這樣的 -

CELERY_RESULT_BACKEND = "amqp" 
CELERY_IMPORTS = ["app.tasks"] 

CELERY_ALWAYS_EAGER = True  

CELERY_RESULT_BACKEND = 'djcelery.backends.database:DatabaseBackend' 
CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler' 

CELERY_DB_REUSE_MAX = 1 

CELERY_ACCEPT_CONTENT = ['json'] 
CELERY_TASK_SERIALIZER = 'json' 
CELERY_RESULT_SERIALIZER = 'json' 
CELERY_TIMEZONE = 'America/New York' 

CELERYBEAT_SCHEDULE = { 
    'test_task': { 
     'task': 'tasks.test_task', 
     'schedule': timedelta(seconds=5), 
     'args':(), 
    }, 
} 

celery_app.py看起來是這樣的 -

from __future__ import absolute_import 
import os 
from celery import Celery 
from django.conf import settings 

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings') 
app = Celery('project') 
app.config_from_object('django.conf:settings') 
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS) 

tasks.py看起來像這樣 -

from __future__ import absolute_import 
from celery.utils.log import get_task_logger 
from celery import task 
from celery_app import app 

logger = get_task_logger(__name__) 

@app.task 
def test_task(): 
    for i in range(0, 4): 
     logger.debug("celery test task") 

當我運行芹菜工作者,我可以看到我的任務被發現 -

$ python manage.py celery -A project worker --loglevel=DEBUG --app=celery_app:app 

-------------- [email protected] v3.1.19 (Cipater) 
---- **** ----- 
--- * *** * -- Darwin-15.4.0-x86_64-i386-64bit 
-- * - **** --- 
- ** ---------- [config] 
- ** ---------- .> app:   project:0x10d5b9a10 
- ** ---------- .> transport: amqp://sgcelery1:**@localhost:5672/sgceleryhost 
- ** ---------- .> results:  djcelery.backends.database:DatabaseBackend 
- *** --- * --- .> concurrency: 4 (prefork) 
-- ******* ---- 
--- ***** ----- [queues] 
-------------- .> celery   exchange=celery(direct) key=celery 


[tasks] 
    . celery.backend_cleanup 
    . celery.chain 
    . celery.chord 
    . celery.chord_unlock 
    . celery.chunks 
    . celery.group 
    . celery.map 
    . celery.starmap 
    . app.tasks.test_task 

[2016-04-24 23:54:55,452: INFO/MainProcess] Connected to amqp://sgcelery1:**@127.0.0.1:5672/sgceleryhost 
[2016-04-24 23:54:55,471: INFO/MainProcess] mingle: searching for neighbors 
[2016-04-24 23:54:56,481: INFO/MainProcess] mingle: all alone 
[2016-04-24 23:54:56,497: WARNING/MainProcess] [email protected] ready. 

當我運行的節拍,這表明從settings.py正被攝取的任務,但它從來沒有真正運行。

$ python manage.py celery -A project beat --app=celery_app:app --loglevel=DEBUG 

[2016-04-24 23:55:04,059: DEBUG/MainProcess] Current schedule: 
<ModelEntry: test_task tasks.test_task(*[], **{}) {4}> 

我在這裏錯過了什麼?

+0

即使在計劃任務沒有得到回升 - '蟒蛇manage.py芹菜-A項目工人-B --loglevel = DEBUG --app = celery_app:app' – DancingJohn

回答

0

嘗試設置CELERY_ALWAYS_EAGER = False

設置CELERY_ALWAYS_EAGER = True使得任務同時被用於運行無芹菜。通過將它切換到False將使芹菜拍起來並定期運行。當您不想芹菜處理任務時,False開關用於開發模式。運行工人,一起跳動

結帳的文檔here

+0

'CELERY_ALWAYS_EAGER = False'沒有任何區別。 – DancingJohn

+0

芹菜是否在您的機器上運行?芹菜節拍需要運行以執行定期任務。 –

+0

如果您在原始問題的底部看到,我正在運行兩者。工人,然後擊敗。兩者都通過manage.py。或者還有別的東西可以運行? – DancingJohn