Question

I keep getting:ERROR/MainProcess] consumer: Cannot connect to amqp://ec2celeryuser when I run celery -A tasks worker on terminal.

Basically what I'm trying to do is get celery/rabbitmq working properly across (2) ec2 instances. To pass a silly task in tasks.py for processing to rabbitmq.

  1. Instance 1 - Houses rabbitMQ

This currently runs RabbitMQ fine. If I run sudo rabbitmqctl status it outputs:

Status of node 'rabbit@ip-xx-xxx-xxx-xx' ...
[{pid,786},

2. Instance 2 - Houses Celery

I'm trying to run celery on instance 2 against Instance 1 using the following in terminal:

celery -A tasks worker

I have a file celeryconfig.py:

BROKER_URL = 'amqp://ec2celeryuser:mypasshere@xx.xxx.xx.xx:5672/celeryserver1/'

#CELERY SETTINGS
CELERY_IMPORTS = ("tasks",)

CELERY_RESULT_BACKEND = "amqp"

I have a file client.py:

from tasks import add

result = add.delay(4, 4) # call task
result_sum = result.get(timeout=5) # wait to get result for a maximum of 5 seconds

I have a file tasks.py:

from celery import Celery

app = Celery('tasks', broker='amqp://ec2celeryuser:mypasshere@xx.xxx.xx.xx:5672/celeryserver1/')

@app.task
def add(x, y):
    return x + y

I've properly setup a vhost, a user ec2celeryuser, and gave this user permissions of:

sudo rabbitmqctl set_permissions -p /celeryserver1 ec2celeryuser ".*" ".*" ".*"

if I do: sudo rabbitmqctl list_users on RabbitMQ (instance 1) it shows:

ec2celeryuser   []
guest   [administrator

I've tried both usernames with their passwords, but no change.

I've been following the Celery Guide, and a tutorial without much luck.

What am I doing wrong here? Clearly there is a connection issue, but what am I doing wrong?

Thank you!

Was it helpful?

Solution

Thanks to user natdempk for helping me fix the configuration syntax of a queues.

The issue was creating a vhost in rabbitmq like:

sudo rabbitmqctl add_vhost /celeryserver1

when it should have been:

sudo rabbitmqctl add_vhost celeryserver1

I then had to reset the permissions for my user ec2celeryuser like:

sudo rabbitmqctl set_permissions -p celeryserver1 ec2celeryuser ".*" ".*" ".*"

The way I realized this was the issue was: I visited /var/log/rabbitmq/<last log file.log>

and saw:

=INFO REPORT==== 30-Apr-2014::12:45:58 ===
accepted TCP connection on [::]:5672 from xx.xxx.xxx.xxx:45964

=INFO REPORT==== 30-Apr-2014::12:45:58 ===
starting TCP connection <x.xxx.x> from from xx.xxx.xxx.xxx:45964

=ERROR REPORT==== 30-Apr-2014::12:46:01 ===
exception on TCP connection <x.xxx.x> from from xx.xxx.xxx.xxx:45964
{channel0_error,opening,
                {amqp_error,access_refused,
                            "access to vhost 'celeryserver1/' refused for user 'ec2celeryuser'",
                            'connection.open'}}

Since fixing the vhost, I now pleasantly see:

[2014-04-30 13:08:10,101: WARNING/MainProcess] celery@ip-xx-xxx-xx-xxx ready.

OTHER TIPS

So I see a few things wrong here. First your broker URL for rabbitMQ in tasks.py doesn't seem correct. It should read something like below.

app = Celery('tasks', broker='amqp://ec2celeryuser:ec2celerypassword@xx.xxx.xx.xx/celeryserver1/')

Also you might want to specify the app you want celery to serve when you run the worker process. You can do this by running celery -A tasks worker from the directory tasks.py is located in.

Another thing is your code in client.py to call your task seems incorrect. From the celery documentation, you can call the task as follows:

from tasks import add

result = add.delay(4, 4) # call task
result_sum = result.get(timeout=5) # wait to get result for a maximum of 5 seconds

Fixing these might solve your issue, or at least get you closer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top