Category Archives: Q&A

When using node.js cluster, how to access a worker’s environment when it dies?

I'm using node.js cluster module to create worker processes. And I set a custom variable in each worker's environment as I fork it.

I need to read that custom variable when a worker dies, but when a worker dies, I can't access its environment object anymore.

This is what I tried so far:

var cluster = require('cluster'),
    os = require('os');

if (cluster.isMaster) {

    cluster.on('exit', function (worker, code, signal) {

        console.log('worker ' + worker.process.pid + ' died');

        var x = {
            workerId: worker.process.env.workerId // This is undefined.
        };
        cluster.fork(x);
    });

    for (var i = 0; i < os.cpus().length; i++) {
        var x = {
            workerId: i
        };
        cluster.fork(x);
    }

}
else {
    console.log("workerId: ", process.env.workerId);

    // simulate an exeption:
    throw "fakeError";

}

I know that's not gonna work, my question is: how to access to latest state of a worker's envoronment right before its death?

How to create a user in MongoDB v3.0.5

I need to create a user for my database in mongodb, but it seems that I can't get it to work.

I have installed mongoDb v3.0.5 on my windows 7 machine. according to this article, I connected to my mongo instance using:

mongo -u siteUserAdmin -p password

and then I created first user via:

use admin
db.createUser(
  {
    user: "siteUserAdmin",
    pwd: "password",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

then I created a user for my nodejs application on exampleDb:

use admin
db.createUser(
  {
    user: "myUser",
    pwd: "123456",
    roles: [ { role: "readWrite", db: "exampleDb" } ]
  }
)

and finally in my nodejs application, when I try to connect to my mongo instance using this connection string:

var connString = "mongodb://myUser:123456@127.0.0.1:27017/exampleDb";

an Authentication failed error happens:

{ name: 'MongoError',
  message: 'Authentication failed.',
  ok: 0,
  code: 18,
  errmsg: 'Authentication failed.'
}

How to create a user in MongoDB v3.0.5

I need to create a user for my database in mongodb, but it seems that I can't get it to work.

I have installed mongoDb v3.0.5 on my windows 7 machine. according to this article, I connected to my mongo instance using:

mongo -u siteUserAdmin -p password

and then I created first user via:

use admin
db.createUser(
  {
    user: "siteUserAdmin",
    pwd: "password",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

then I created a user for my nodejs application on exampleDb:

use admin
db.createUser(
  {
    user: "myUser",
    pwd: "123456",
    roles: [ { role: "readWrite", db: "exampleDb" } ]
  }
)

and finally in my nodejs application, when I try to connect to my mongo instance using this connection string:

var connString = "mongodb://myUser:123456@127.0.0.1:27017/exampleDb";

an Authentication failed error happens:

{ name: 'MongoError',
  message: 'Authentication failed.',
  ok: 0,
  code: 18,
  errmsg: 'Authentication failed.'
}

How to extend q.promise in node.js?

I need to extent q.promise with my own functions, I tried:

defer = q.defer();
defer.promise.prototype.funcName = function () {
    // my function
}

witch returns following error:

TypeError: Cannot set property 'funcName' of undefined

How should I properly extend q.promise?


This is my actual problem: I need an alias for .then() witch gets name of an operation as argument (instead of a function), and executes that operation according to a internal library of functions.

How to transfer contexts among separate node.js environments? [closed]

Lets suppose I have my app running on machine A, at some point I need to shut down that machine, and transfer my app to machine B to continue its process. so I need to be able to save and restore context of my app.

How can implement that?

To be more specific, my question is: How can I save/restore application context in node.js?

How to transfer contexts among seperate node.js environments?

Lets suppose I have my app running on machine A, at some point I need to shut down that machine, and transfer my app to machine B to continue its process. so I need to be able to save and restore context of my app.

How can implement that?

To be more specific, my question is: How can I save/restore application context in node.js?