2017-10-31 5 views
1

장고 1.11과 Celery 4.1 프로젝트가 있으며, setup docs에 따라 구성했습니다.Celery 4가 자동 검색 작업이 아닙니다.

.env/bin/celery worker -A myproject -l info 

가 더 작업 샘플 "debug_task"를 제외하고 발견하는 모습 내가 셀러리 여러 설치된 앱이 비록 : 내가 가진 노동자를 시작할 때

from __future__ import absolute_import 

import os 

from celery import Celery 

# set the default Django settings module for the 'celery' program. 
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings.settings' 

app = Celery('myproject') 

app.config_from_object('django.conf:settings', namespace='CELERY') 

#app.autodiscover_tasks(lambda: settings.INSTALLED_APPS) # does nothing 
app.autodiscover_tasks() # also does nothing 

print('Registering debug task...') 
@app.task(bind=True) 
def debug_task(self): 
    print('Request: {0!r}'.format(self.request)) 

,

처럼 내 celery_init.py 보인다 작업은 app.autodiscover_task()에 대한 호출을 통해 발견되었을 것입니다. 이것은 내 근로자는 초기 출력을 생성한다 : 내 응용 프로그램에서

-------------- [email protected] v4.1.0 (latentcall) 
---- **** ----- 
--- * *** * -- Linux-4.13.0-16-generic-x86_64-with-Ubuntu-16.04-xenial 2017-10-31 15:56:42 
-- * - **** --- 
- ** ---------- [config] 
- ** ---------- .> app:   myproject:0x7f952856d650 
- ** ---------- .> transport: amqp://guest:**@localhost:5672// 
- ** ---------- .> results:  amqp:// 
- *** --- * --- .> concurrency: 4 (prefork) 
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker) 
--- ***** ----- 
-------------- [queues] 
       .> celery   exchange=celery(direct) key=celery 


[tasks] 
    . myproject.celery_init.debug_task 

[2017-10-31 15:56:42,180: INFO/MainProcess] Connected to amqp://guest:**@127.0.0.1:5672// 
[2017-10-31 15:56:42,188: INFO/MainProcess] mingle: searching for neighbors 
[2017-10-31 15:56:43,211: INFO/MainProcess] mingle: all alone 
[2017-10-31 15:56:43,237: INFO/MainProcess] [email protected] ready. 

내 모든 레거시 작업 tasks.py 파일을 다음과 같이 정의했다 :

from celery.task import task 

@task(name='mytask') 
def mytask(): 
    blah 

워드 프로세서 shared_task 장식을 사용하는 것이 좋습니다, 그래서 대신 내가 시도 :

from celery import shared_task 

@shared_task 
def mytask(): 
    blah 

하지만 셀러리 작업자는 여전히 그것을 보지 못합니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

편집 : 내 설정의 CELERY_IMPORTS 목록에 명시 적으로 나열하여 작업을 표시 할 수 있었지만 그래도 tasks.py을 편집하여 Django 프로젝트 (models.py, 등) 또는 예외를 발생시킵니다. Apps aren't loaded yet. 이것은 아무것도 아닌 것보다 낫지 만 엄청난 양의 리팩토링이 필요합니다. 더 좋은 방법이 있습니까?

+0

'Celery Worker -A app.tasks -l DEBUG'을 실행하면 어떻게됩니까? 프로젝트 전체가 아니라 애플 리케이션의 태스크 파일을 지정해야한다는 것을 알았습니다. – Jason

답변

0

큰 문제는 셀러리가 현재 사용자 정의 Celery() 인스턴스를 현재 응용 프로그램으로 설정하지 않는다는 것입니다.이 문제를 해결하려면, 나는 포함하는 내 celery_init.py을 수정했다 :

from celery._state import _set_current_app 

# setup my app = Celery(...) 

_set_current_app(app) 
0

위의 행을 추가하여 셀러리가 전체 프로젝트에 쓰여진 모든 태스크를 자동으로 검색하도록 할 수 있습니다. 이것은 나를 위해 일하고있다.

+0

내 게시물 및 예제 코드에서 설명한대로이 시도하고 작동하지 않았다. – Cerin

0

나는 비슷한 문제가있어서 해결책은 셀러리 호출에 include kwarg를 추가하는 것이 었습니다.

include 인수는 작업자가 시작할 때 가져올 모듈 목록입니다. 작업자가 작업을 찾을 수 있도록 여기에 작업 모듈을 추가해야합니다.

app = Celery('myproject', 
      backend = settings.CELERY.get('backend'), 
      broker = settings.CELERY.get('broker'), 
      include = ['ingest.tasks.web', ... ]) 

밖으로 http://docs.celeryproject.org/en/latest/getting-started/next-steps.html#proj-celery-py 자세한 내용 확인은

1

그냥 여기를 게시

from django.conf import settings 

app.config_from_object(settings, namespace='CELERY') 
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS, force=True) 

force=True 솔루션

을 것 같다 (그것이 작동하는 이유를 모르겠어요) 작동하는 또 다른 방법은 셀러리 인스턴스화 전에 django.setup()을 호출하는 것입니다.

from __future__ import absolute_import, unicode_literals 
import os 

import django 

from celery import Celery 


django.setup() # This is key 


# set the default Django settings module for the 'celery' program. 
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings') 

app = Celery('notifs') 
app.config_from_object('django.conf:settings', namespace='CELERY') 

app.autodiscover_tasks() 

이 방법은 force=True, 수입 django.conf.settings 방지하고 나에게 청소기 보인다. 비록 당신이 왜 django.setup에 전화해야하는지 모르겠지만 그것은 문서에 언급되어 있지 않습니다.

0

허용 된 대답은 나를 위해 작동하지 않습니다. 나는 셀레 리 4.1.0과 django 2.0.2를 사용하고있다.이 똑같은 일은 나에게도 일어난다 ... 샘플 "debug_task"를 제외하고는 발견 된 태스크가 없다. 셀러리 태스크가있는 여러 개의 설치된 애플리케이션이 있어야한다. app.autodiscover_task() 호출을 통해 발견되었습니다.

나는 수동으로 이것을 CELERY_IMPORTS에 추가하려고했지만 위에 언급 된 제안을 시도했다. 같은 결과. 행운을 빕니다 시간에 시간을 통해 불고.

관련 문제