Search code examples
node.jsapiibm-cloudopenwhisk

How should I invoke an asynchronous action in OpenWhisk using API Manager from IBM Bluemix?


I have created a small OpenWhisk action written in Node.js. This action makes a call to a backend system. The function implementing the action follows (I have removed the part of code which is not related with this problem).

/**
  *
  * main() will be invoked when you Run This Action.
  *
  */


function main() {

    var request = require("request");

    var data = {
    };

    var options = {
        method: "POST",
        url: "https://whatever.it.is",
        body: data,
        json: true
    };

    request.post(options, function(error,response,body) {
        var data = []; 
        return whisk.done({"msg":data});
    });

    return whisk.async();
}

Then I have tried to define an API call in Bluemix API Manager.The call is done. But I receive as a result the Id for the activity and not the contents I returned using whisk.done().

Any ideas?. Thanks in advance.


Solution

  • Tldr: Append blocking=true to the query of your URL to return the result of the activation in the same HTTP request.


    Speaking of action invocations in OpenWhisk there are two different modes you can choose from. blocking or non-blocking.

    Non-blocking invocations are the default, where you POST your payload against the OpenWhisk backend and get back an activation id, under which the results of your invocation (aka activation) will be stored. You can get those results at a later point in time (hence non-blocking) via the Activations API.

    Blocking invocations will wait until your activation is finished and return the results in the same HTTP request. Blocking requests are made via the same API but with the query parameter blocking=true appended.

    Your issue sounds like you most likely want a blocking invocation. Variations of query parameters can be found at the OpenWhisk REST API Reference.