-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
158 lines (113 loc) · 3.29 KB
/
index.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
var cluster = require( 'cluster' );
var domain = require( 'domain' );
var fs = require( 'fs' );
var path = require( 'path' );
var numCPUs = require( 'os' ).cpus().length;
var kluster = module.exports = {
workers : {},
options : {
workers : numCPUs * 2,
extensions : [ '.js' ],
ignoreDirectories : [],
watchDirectory : [],
interval : 2000
},
set: function( key, value ) {
this.options[ key ] = value;
return this;
},
fork: function() {
var worker = cluster.fork();
var self = this;
this.workers[ worker.process.pid ] = worker;
worker.on( 'message', function( msg ) {
if( msg ) {
console.log( 'broadcasting to all workers', msg );
self.broadcastMsg( msg );
}
});
console.log( 'Work Fork: ' + worker.process.pid );
},
exit: function( worker ) {
console.log( 'Work Exit: ' + worker.process.pid );
delete( this.workers[ worker.process.pid ] );
this.fork();
},
// Code from: https://github.com/learnboost/cluster
reload: function() {
if (this.options.watchDirectory.length === 0) {
return;
}
console.log( 'Watch Directory Enabled' );
this.options.ignoreDirectories.forEach(
function( file, index, dirs ) {
dirs[ index ] = path.resolve( file );
}
);
this.options.watchDirectory.forEach( traverse );
var self = this;
function traverse( file ) {
file = path.resolve( file );
fs.stat( file, function( err, stat ) {
if( ! err ) {
if( stat.isDirectory() ) {
if( ~self.options.ignoreDirectories.indexOf( file ) ){
console.log( 'Ignore Directory: ' + file );
return;
}
fs.readdir( file, function( err, files ) {
files.map( function( f ) {
return file + '/' + f;
}).forEach( traverse );
});
}
else {
watch( file );
}
}
});
}
function watch( file ) {
if( !~self.options.extensions.indexOf( path.extname( file ) ) )
return;
console.log( 'Watch: ' + file );
fs.watchFile( file, { interval: self.options.interval }, function( curr, prev ) {
if( curr.mtime > prev.mtime ) {
console.log( '\033[36mchanged\033[0m \033[90m- %s\033[0m', file );
self.restartWorkers();
}
});
}
},
restartWorkers: function() {
for( var index in this.workers ) {
console.log( 'Restart Worker:' + index );
this.workers[ index ].process.disconnect();
}
},
broadcastMsg: function( msg ) {
for( var i in this.workers ) {
var worker = this.workers[ i ];
worker.send( msg );
}
},
start: function( server ) {
if( cluster.isMaster ) {
if( this.options.workers < 2 ) {
this.options.workers = 2;
}
for( var i = 0; i < this.options.workers; i++ ) {
this.fork();
}
cluster.on( 'exit', this.exit.bind( this ) );
this.reload();
}
else {
var serverDomain = domain.create();
serverDomain.on( 'error', function( err ) {
console.log( 'uncaughtException: ', err.stack );
});
serverDomain.run( server );
}
}
};