index.js
1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**!
* sendmessage - index.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
/**
* Module dependencies.
*/
var IS_NODE_DEV_RUNNER = /node\-dev$/.test(process.env._ || '');
if (!IS_NODE_DEV_RUNNER && process.env.IS_NODE_DEV_RUNNER) {
IS_NODE_DEV_RUNNER = true;
}
module.exports = function send(child, message) {
if (typeof child.send !== 'function') {
// not a child process
return setImmediate(child.emit.bind(child, 'message', message));
}
if (IS_NODE_DEV_RUNNER || process.env.SENDMESSAGE_ONE_PROCESS) {
// run with node-dev, only one process
// https://github.com/node-modules/sendmessage/issues/1
return setImmediate(child.emit.bind(child, 'message', message));
}
// cluster.fork(): child.process is process
// childprocess.fork(): child is process
var connected = child.process ? child.process.connected : child.connected;
if (connected) {
return child.send(message);
}
// just log warnning message
var pid = child.process ? child.process.pid : child.pid;
var err = new Error('channel closed');
console.warn('[%s][sendmessage] WARN pid#%s channel closed, nothing send\nstack: %s',
Date(), pid, err.stack);
};