Search code examples
mongodbopenwhisk

OpenWhisk stops responding, after the first call


I am using Vagrant VM on Windows 10. I have written an action function in Node.Js. This is a light weigh experimental function to check connectivity to MongoDB from OpenWhisk action, written in Node.JS. Following is the code.

     function entryPoint(args) {



        var mongoose = require('mongoose');
        var MongoClient = require('mongodb').MongoClient;
        var Schema = mongoose.Schema;

        mongoose.Promise = global.Promise;


        return new Promise((resolve, reject) => {

          mongoose.connect("mongodb://192.168.16.1:27017/angularcrud").then(
            () => 
            {

              var Coin = new Schema({ 
                name: {
                  type: String
                },
                price: {
                  type: Number
                }
              });
              var coinModel = mongoose.model('Coin', Coin);

              coinModel.find(function (err, coins){
                if(err){
                  console.log(err);
                }
                else {
                  resolve({coins})
                }
              });

            },
            err => { return reject(err)}
            ); 

        })



      }

 module.exports.main = entryPoint;

I have zipped the NodeJS files and created the action in wsk. When I run this for the first time I get the expected results. But when I run it for the second time, the call hands for a minute or so and then I see the following error.

ok: invoked /_/getcoins, but the request has not yet finished, with id 612da4ebb5f64795ada4ebb5f6e7957c

I do not know where do I start investigating, as I am totally new to both OpenWhisk and MongoDb.


Solution

  • To start investigating this, use wsk activation list 612da4ebb5f64795ada4ebb5f6e7957c in this case to get at least the logs of your failed activation.

    I think the error handling in your action is not correct. If the find call returns an error, the Promise is never resolved.

    I had a brief look at mongooses documentation. find can apparently return a Promise if you call exec on it.

    In any case, make sure to use Promise combinators as much as possible, so you're pretty much guaranteed that any edge-case is properly handled and the Promise always completes in one way or the other. If you need to back out for the "manual" creation of Promises, scope them as small as possible.

    function entryPoint(args) {
        var mongoose = require('mongoose');
        var Schema = mongoose.Schema;
        mongoose.Promise = global.Promise;
    
        return mongoose.connect("mongodb://192.168.16.1:27017/angularcrud").then(() => {
            var Coin = new Schema({ 
                name: {
                    type: String
                },
                price: {
                    type: Number
                }
            });
            var coinModel = mongoose.model('Coin', Coin);
            return coinModel.find().exec();
        });
    }
    
     module.exports.main = entryPoint;