ES6

Generators

'use-strict';
module.exports = {
    available: function(req, res) {

        // Test the connection to memcached
        var memcached = new Memcached(m.port.replace('tcp://', ''), {});

        memcached
            .set('available', JSON.stringify({
                status: true
            }), parseInt(process.env.CACHE_EXPIRE, 10))
            .then(function(data) {
                return memcached.get('available');
            })
            .then(function(value) {
                if (value == '{"status":true}') {
                    return Promise();
                } else {
                    throw new Error('memcached connection is not functional' );
                }
            })
            .catch(function(err) {
               return err;
            })
            .done(function(err) {
                console.log("done");
                if (err) {
                    res.send("NOT OK " + err);
                } else {
                    res.send("OK");
                }
            });

    }
};

Health Check with Promises

'use-strict';
module.exports = {
    avail: function(req, res) {
        
        co(function* () {

            console.log("Connecting to ", m.port);

            try {

                var cache = new Cache(m.port.replace('tcp://', ''));
                
                yield cache.set('available', JSON.stringify({ status: true }), 0);

                var value = yield cache.get('available');

                if (value == '{"status":true}') {
                    res.send("OK");
                } else {
                    res.send("NOT OK");
                }

            } catch(e) {
                res.send("NOT OK");
                console.log(e.stack);
            }


        });

    },
};

Health Check with Generators

ES6

By bozzltron

ES6

  • 989