0

I'm new to node-celery and am trying to start simple by getting a result from a celery task. I've written my JS in an 'await' fashion in order to wait for the result from the celery and then move forward, however, it seems the JS never gets a result from 'client.call('task')'

Celery Code:

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

Node JS Code:

async function addCelery() {
    client = celery.createClient({
        CELERY_BROKER_URL: "redis://myurl.com:0000"
    });

    let clientResult = new Promise((resolve, reject) => {
        console.log("Log 1");                    //This shows in the console
        client.on('connect', () => {
            console.log("Log 2");                //This also shows in console, making me believe it connected successfully
            var call = client.call('tasks.add', [1,2]); //Call task
            console.log("Log 3");                //This also shows in console, making me believe the task call is called successfully              
            call.on('ready', function(result){   //When call gets result
                console.log(result);             //Never gets here
            });
        });
    })
    await clientResult;
    console.log('done');       //Never gets here

I'm not sure exactly why it's never getting past the never getting a result from the call. A lot of the console.log()'s I have for debugging make me believe it's successfully connecting and calling the task

Is it not connecting to the celery? Am I calling the task wrong? It shouldn't be a time thing as I've executed this and waited 10 mins still with no result. I'm very new to this so any help would be appreciated

mmarion
  • 859
  • 8
  • 20
  • what is `var client.call('tasks.add', [1,2]);`? that doesn't parse; also, where is `call` defined, for `call.on`? – Christian Fritz Apr 01 '21 at 19:33
  • client.call() is what's shown in the documentation to call a task, see it here under the 'backends' section: https://github.com/mher/node-celery. Can you explain further what you mean by 'doesn't parse'? Also 'call' is defined in my code, I just updated the above example to reflect that – mmarion Apr 01 '21 at 20:00
  • @ChristianFritz ^ – mmarion Apr 01 '21 at 20:35
  • 1
    well now that you've edited the code, it parses again (it wasn't valid js syntax, `var client.call(...)`). – Christian Fritz Apr 01 '21 at 20:39

1 Answers1

0

You never called resolve or reject in the Promise you created.

Also, according to the examples on the page you linked in the comments, you should provide the callback function as an argument to client.call, like so:

client.call('tasks.add', [1, 2], resolve)

(resolve has the same signature as the callback so you can pass it directly)

Edit: why doesn't result.on('ready', fn) work?

Because you're using a Redis backend. That only works for an AMQP backend.

  • You're right about not calling `resolve` or `reject`. But not all the examples on that page use a callback function as an argument to `client.call`. The last example in the "Backends" section is very similar to OP's code. – David Knipe Apr 01 '21 at 20:47
  • OP is not using an AMQP backend. Directly above that example, it says `AMQP backend allows to subscribe to the task result and get it immediately, without polling:` –  Apr 01 '21 at 20:55
  • Added this to the answer. Should have included it originally. –  Apr 01 '21 at 21:01