1

js/express . what i am doing is send a request to express server from a different server with some user details .

add the request details to a job queue (kue) , then using workers (cluster) i process the jobs and send emails to users . and i want sent a response back to the server which send the request to my express server .

the normal res.send() won't work here because after we add the request parameters to the queue and process there is no link between the request and response . here is sample of my code .

app.js

app.get('/my_request/:param1/:param2/:param3',
    function( req, res ) {

        req.params.type = "ORDER_COMPLETE";
        var params = req.params;

        jobs.create('jobQueue', { 'title': "order_receipt", 'params' : params } )
            .ttl( 30 * 1000 ) //value in ms // 30 min
            .removeOnComplete( true )
            .save(function(err) {
                if (err) {
                    console.log('jobs.create.err', err);
                } 
        });

        //res.send( JSON.stringify( {"success" : true} ) ).end();

    }

);

There is a new requirement : instead of sending the email now i need to create the email html and send it back to the requester .

if i did not use a job queue i could use res.send() . but now i can't .

I thought about saving my res object to the job queue and then lately try to send the response back to the same user using that response object .

Is there any solutions that i can use in a situation like this ?

any help is greatly appreciated :)

Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193

1 Answers1

2

You can use job-specific events:

app.get('/my_request/:param1/:param2/:param3',
function( req, res ) {

    req.params.type = "ORDER_COMPLETE";
    var params = req.params;

    var job = jobs.create('jobQueue', { 'title': "order_receipt", 'params' : params } )
        .ttl( 30 * 1000 ) //value in ms // 30 s
        .removeOnComplete( true )
        .save(function(err) {
            if (err) {
                console.log('jobs.create.err', err);
            } 
        });

    //res.send( JSON.stringify( {"success" : true} ) ).end();
   job.on('complete', function(result){
     res.send(result); // This is just an example. You can use JSON here as well.
   });

});
xavier.seignard
  • 11,254
  • 13
  • 51
  • 75
gnerkus
  • 11,357
  • 6
  • 47
  • 71