Nyxynyx
Nyxynyx

Reputation: 63667

Configure Celery with JSON Serializer (Python + node.js)

I'm running a Celery worker in Python with the celery module v3.1.25, and a Celery client in node.js with the node-celery npm package v0.2.7 (not the latest).

The Python Celery worker works fine when sending a job using a Python Celery client.

Problem: When using a node-celery client to send a task to the Celery backend, we get an error in the JS console:

(STDERR) Celery should be configured with json serializer

Python Celery worker is configured with:

app = Celery('tasks', 
    broker='amqp://test:[email protected]:5672//',
    backend='amqp://',
    task_serializer='json',
    include=['proj.tasks'])

node-celery client is configured with:

var celery = require('node-celery')
var client = celery.createClient({
    CELERY_BROKER_URL: 'amqp://test:[email protected]:5672//',
    CELERY_RESULT_BACKEND: 'amqp',
    CELERY_TASK_SERIALIZER: "json"
});

client.on('connect', function() {
    console.log('connected');

    client.call('proj.tasks.getPriceEstimates', [start_latitude, start_longitude],
        function(result) {
            console.log('result: ', result);
            client.end();
        })
});

Is this a problem with the configuration on the Python Celery worker? Did we miss out on a configuration parameter which can change the return serialization format to json?


Update

Updated with result_serializers and accept_content parameters as suggested by ChillarAnand

from __future__ import absolute_import, unicode_literals
from celery import Celery

app = Celery('tasks', 
    broker='amqp://test:[email protected]:5672//',
    backend='amqp://',
    task_serializer='json',
    result_serializer='json',
    accept_content=['application/json'],
    include=['proj.tasks'])

But node.js Celery client still thinks that its not in json, throwing the same error message.

It gives that error because the results were in the form of 'application/x-python-serialize'.

Checked this to be the case, as RabbitMQ management console shows the results to be content_type: application/x-python-serialize


This forum post says that it is because the tasks were created before the configs were loaded.

Here are how my files are like:

proj/celery.py

from __future__ import absolute_import, unicode_literals
from celery import Celery

app = Celery('tasks', 
    broker='amqp://test:[email protected]:5672//',
    backend='amqp://',
    task_serializer='json',
    result_serializer='json',
    accept_content=['application/json'],
    include=['proj.tasks'])

proj/tasks.py

from __future__ import absolute_import, unicode_literals
from .celery import app

@app.task
def myTask():
    ...
    return ...

Is there a better way to structure the code to ensure that the configs are loaded before the tasks?

Upvotes: 2

Views: 3048

Answers (1)

Chillar Anand
Chillar Anand

Reputation: 29554

When configuring a serializer, you should specify content type, task serializer and result serializer as well.

app = Celery(
    broker='amqp://guest@localhost//',
    backend='amqp://',
    include=['proj.tasks'],

    task_serializer='json',
    result_serializer='json',
    accept_content = ['application/json'],
)

Upvotes: 1

Related Questions