2013-08-23 78 views
2

我有一個Django項目的芹菜設置,我似乎無法讓我的任務正常運行。我在Ubuntu 13.04上使用Django 1.4.3,celery 3.0.1,django-celery 3.0.17和Python 2.7。使用Django與芹菜接收未註冊的任務

我已經驗證了我的RabbitMQ服務器是否正在運行:

sudo service rabbitmq-server status 
Status of node [email protected] ... 
[{pid,1667}, 
{running_applications,[{rabbit,"RabbitMQ","3.0.2"}, 
         {os_mon,"CPO CXC 138 46","2.2.9"}, 
         {mnesia,"MNESIA CXC 138 12","4.7"}, 
         {sasl,"SASL CXC 138 11","2.2.1"}, 
         {stdlib,"ERTS CXC 138 10","1.18.1"}, 
         {kernel,"ERTS CXC 138 10","2.15.1"}]}, 
{os,{unix,linux}}, 
{erlang_version,"Erlang R15B01 (erts-5.9.1) [source] [64-bit] [smp:8:8] [async-threads:30] [kernel-poll:true]\n"}, 
{memory,[{total,28112760}, 
      {connection_procs,93432}, 
      {queue_procs,42680}, 
      {plugins,0}, 
      {other_proc,9331824}, 
      {mnesia,65288}, 
      {mgmt_db,0}, 
      {msg_index,86656}, 
      {other_ets,764048}, 
      {binary,1343288}, 
      {code,12372068}, 
      {atom,512601}, 
      {other_system,3500875}]}, 
{vm_memory_high_watermark,0.4}, 
{vm_memory_limit,6719406080}, 
{disk_free_limit,1000000000}, 
{disk_free,122925531136}, 
{file_descriptors,[{total_limit,924}, 
        {total_used,7}, 
        {sockets_limit,829}, 
        {sockets_used,3}]}, 
{processes,[{limit,1048576},{used,151}]}, 
{run_queue,0}, 
{uptime,275789}] 
...done. 

我有一個virtualenv中我的Django項目的設置。我有芹菜在settings.py文件配置爲:

INSTALLED_APPS = [ 
    # django apps 

    # our apps ... 

    # third party modules ... 
    'djcelery', 
] 

import djcelery 
djcelery.setup_loader() 

CELERY_IMPORTS = ('my.task') 

的RabbitMQ的經紀人是正確設置 - 我們還有其他任務芹菜運行正常。

我的任務:

import celery 

class MyClass(object): 

    @celery.task() 
    def my_task(self, new, old): 
     # do stuff 
     return 

    def kick_off_tasks(self): 
     # do stuff 
     new = 'something' 
     old = 'something else' 
     self.my_task.apply_async(args(new, old), kwarg1='new', kwarg2='old) 
     # do more stuff 

當我運行調用kick_off_tasks我收到的celeryd信息日誌中的以下觀點:

./manage.py celeryd --loglevel=info 

[Tasks] 
    . my.task 
    . other.tasks 
    . more.tasks 
    . etc.etc.etc 

[2013-08-23 14:18:40,772: WARNING/MainProcess] [email protected] has started. 
[2013-08-23 14:20:40,757: ERROR/MainProcess] Received unregistered task of type 'my.task'. 
The message has been ignored and discarded. 

Did you remember to import the module containing this task? 
Or maybe you are using relative imports? 
Please see http://bit.ly/gLye1c for more information. 

The full contents of the message body was: 
{'retries': 0, 'task': 'my.task', 'eta': None, 'args': ('something, 'something else'), 'expires': None, 'callbacks': None, 'errbacks': None, 'kwargs': {}, 'id': 'bf4d5ee7-9701-42b6-a887-c6b6470d9810', 'utc': True} (2297b) 

Traceback (most recent call last): 
    File "/home/ricomoss/.virtualenvs/proton/local/lib/python2.7/site-packages/celery/worker/consumer.py", line 394, in on_task_received 
    strategies[name](message, body, message.ack_log_error) 
KeyError: 'my.task' 

有什麼建議?

回答

2

我能夠通過重構得到這個工作,以便任務不是類方法。我把它們放在我們的tasks.py文件中,一切正常。我沒有時間研究爲什麼會出現這種情況,我打算這樣做,但在此之前,如果有人遇到同樣的問題,您可以快速解決問題。

+0

你有沒有找到一種方法使類方法作爲任務工作?我有類似的問題。目前我使用'tasks.py'文件來調用類方法,當我想在後臺運行它們時,我寧願直接調用它們。 – Matthew

+0

@Mthethew我從來沒有能夠成功地任務一個類或類方法。我相信這與它被醃製的方式有關,但我從來沒有花時間去理解底層問題是什麼。 – Rico