{"issue":{"title":"Cluster 2.0 – step 4 : Worker class and isolate internal messages","user":{"login":"AndreasMadsen","id":505333,"url":"https://api.github.com/users/AndreasMadsen","avatar_url":"https://secure.gravatar.com/avatar/e6a7ed778a0e8d9f284c28e119f1d212?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"e6a7ed778a0e8d9f284c28e119f1d212"},"created_at":"2011-12-20T16:45:49Z","milestone":null,"labels":[{"name":"cluster","url":"https://api.github.com/repos/joyent/node/labels/cluster","color":"e10c02"}],"state":"open","updated_at":"2011-12-28T21:47:07Z","id":2614907,"pull_request":{"patch_url":"https://github.com/joyent/node/pull/2388.patch","html_url":"https://github.com/joyent/node/pull/2388","diff_url":"https://github.com/joyent/node/pull/2388.diff"},"closed_at":null,"html_url":"https://github.com/joyent/node/issues/2388","body":"I know this is a big patch, but it is the foundation of the `cluster 2.0` and cannot be split without creating a hopeless design.\r\n\r\n**Documentation:**\r\nThe documentation can be found here: [doc](https://github.com/AndreasMadsen/node-1/blob/cluster2/doc/api/cluster.markdown)\r\n\r\nThe important part here is that there now exist a Worker class/object this exist in both workers and clusters and are used in the same way. The worker object is a worker accessed using `cluster.worker` and in the cluster the object is return from `fork` and stored in `cluster.workers` and a callback argument in a lot of functions and events.\r\n\r\n**Example on worker object:**\r\nIn the master you send a message to the worker using `worker.send`. In a worker you send a message to the master using `worker.send`.\r\n\r\nThe next important part is the message system, this now isolate internal messages from userland, and support callbacks both ways.\r\n\r\nThis changes may seam to have nothing to do with each other, but the Worker object use internal messages more and the message system don't make sense without a standardized Worker object.  \r\n\r\n**The API changes are:**\r\n\r\n* `cluster.fork` do no longer extend directly on on `child_process.fork`. The object return by `child_process.fork` must now be accessed from `cluster.fork().process`.\r\n* `worker.kill` is renamed to `worker.destroy`.\r\n* `worker.on('exit')` is now `worker.on('death')`\r\n* You should not use `process.send` but `cluster.worker.send` in the worker.\r\n* You should not use `process.on('message')` but `cluster.worker.on('message')` in the worker.\r\n* You should not use `process.exit` but `cluster.worker.destroy`, the difference is that `destroy` send a suicide message to the `master`, this indicate that we won't restart the worker (not implemented yet).\r\n* You may want to know that the env property `CLUSTER_WORKER_ID` is now called  `CLUSTER_UNIQUE_ID`, but this was not a part of the official API.\r\n* The `worker.send()` has change a lot internal, but should not cause any problems, the old function exist now as `worker.process.send()` \r\n\r\n**The API additions are:**\r\n\r\n* New events in the `cluster` object `fork, online, listening`\r\n* New events in the `worker` object `online, listening`\r\n* A state property in the worker object `worker.state` this can be `none, online, listening, dead`\r\n* A suicide property in the worker object `worker.suicide` if `undefined` the worker is alive, if `false` the worker died `accidently`, if `true` the worker died by purpose.\r\n* New `cluster.eachWorker` use it to run a function on every living worker.\r\n* A uniqueID is assigned to each worker and can be read from `worker.workerID`.\r\n* The send function now support callback in both worker and master, the callback will be called when the message is received. \r\n\r\n_This is about the merging in the `cluster 2.0` ( see #2038 ) pull request_","number":2388,"url":"https://api.github.com/repos/joyent/node/issues/2388","comments":13,"assignee":null},"comment":{"user":{"login":"ry","id":80,"url":"https://api.github.com/users/ry","avatar_url":"https://secure.gravatar.com/avatar/b984299ceb40752b58146714eb192554?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"b984299ceb40752b58146714eb192554"},"created_at":"2011-12-28T21:47:06Z","updated_at":"2011-12-28T21:47:06Z","id":3295516,"body":"Another option is to leak the messages out to the user. \r\n\r\n> The last option, is to modify the core to filter all message containing a NODE_ prefix in its cmd property, and relay this messages to an internal event. I think is the the best option, but I'm not happy. It add unnecessary complexity to a good workeing API.\r\n\r\nThis seems good. \r\n\r\nAre the callbacks removed?\r\n\r\nNext item for debate is `cluster.worker`. I want the workers to be able to do `process.send()` not `cluster.worker.send`.\r\n","url":"https://api.github.com/repos/joyent/node/issues/comments/3295516"},"action":"created"}