Search code examples
node.jsexpressgoogle-app-enginetask-queuegoogle-tasks-api

Where are Google cloud task queues endpoints located and can someone use nodejs child process instead?


I have a nodejs on Google app engine server but I need to do some background tasks immediately after sending response to user. I have been using the sample code below with nodejs child-process as described here but it only works well when tested locally but does not run in google app engine.

let {fork} = require('child_process');
const forked = fork('./worker.js');

router.post('/endpoint', (req, res) => {
    let options = req.body.options;
    let isResponded = false;
    async_helper.doWork(options).then((s) => {
        let reply = JSON.stringify(s);
        isResponded = true;
       res.status(200).send(reply).end();
       return null
    }).then(value => {
    //This is the long task of sending email
        forked.send(value);
        return null
    }).catch(err => {
    console.log(err);
        if (!isResponded) {
            let errr = err;
            res.status(500).send('Failed').end();
        }
        return null
    });
});

Which is the best way of implementing such tasks in a production app? Why is the child-process approach not working in App Engine? Must I use Task queues?

Task Queue: I have also tried task queue after the problem above but I cannot differentiate between task queue handler endpoint and my server's endpoint since this official doc also indicates that the handler is also listening to a port, just like my pre-existing app.js entry point.

So does it mean that the same app.js is the task queue's entry point too?

or does it mean that task queue entry point is another "server" in app engine?

For hours I haven't found a clear guide.


Solution

  • After several trial and error with task queues, I found out it's the same app.js which is the same endpoint receiving the task queue calls.