Skip to content
This repository was archived by the owner on Apr 21, 2026. It is now read-only.

Commit d8b2b36

Browse files
author
Subbu
committed
Add
0 parents  commit d8b2b36

18 files changed

Lines changed: 867 additions & 0 deletions

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
*.iml
2+
.DS_Store
3+
.idea
4+
*.log
5+
temp
6+
node_modules
7+
pids
8+
reports
9+
target
10+
docs
11+
logs

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
test

TODO.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
* Basic cluster
2+
* St art from js
3+
* Start from command line args
4+
* Mon
5+
* ECV
6+
* Graceful shutdown - stop connection listening
7+
* Traffic in and out - continue connection listening but update ecv
8+
* Process restart
9+
* Process recycle
10+
* Drain incoming connections on timeout
11+
* Drain incoming connections on shutdown
12+
* Raise heartbeats thru logEmitter https://github.scm.corp.ebay.com/qlio/ql.io/issues/74

examples/express-server.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2012 eBay Software Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
var cluster2 = require('../lib/index.js'),
18+
express = require('express');
19+
20+
//
21+
// An express server cluster
22+
23+
var app = express.createServer();
24+
app.get('/', function(req, res){
25+
res.send('hello');
26+
});
27+
28+
cluster2.listen({
29+
port: 3000,
30+
cluster: true,
31+
ecv: {
32+
monitor: '/',
33+
validator: function() {
34+
return true;
35+
}
36+
}
37+
}, function(cb) {
38+
cb(app);
39+
});

examples/http-server.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2012 eBay Software Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
var cluster2 = require('../lib/index.js'),
18+
http = require('http');
19+
20+
//
21+
// A TCP server cluster
22+
23+
var server = http.createServer(function (req, res) {
24+
console.log('server connected');
25+
res.writeHead(200);
26+
res.end('hello');
27+
});
28+
29+
cluster2.listen({
30+
port: 3000,
31+
cluster: true,
32+
ecv: false
33+
}, function(cb) {
34+
cb(server);
35+
});

examples/tcp-server.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2012 eBay Software Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
var cluster2 = require('../lib/index.js'),
18+
net = require('net');
19+
20+
//
21+
// A TCP server cluster
22+
//
23+
var server = net.createServer(function (c) {
24+
console.log('server connected');
25+
c.on('end', function () {
26+
console.log('server disconnected');
27+
});
28+
c.write('hello\r\n');
29+
c.pipe(c);
30+
});
31+
32+
cluster2.listen({
33+
port: 3000,
34+
cluster: true
35+
}, function(cb) {
36+
cb(server);
37+
});

lib/ecv.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2012 eBay Software Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
"use strict";
18+
19+
var http = require('http'),
20+
os = require('os');
21+
22+
exports.version = require('../package.json').version;
23+
24+
/**
25+
* The ECV check sends a "/tables" request to the running server. Anything other than a valid JSON response is
26+
* treated as an error.
27+
*/
28+
var hostname = os.hostname();
29+
30+
exports.enable = function(app, port, path, monitor, validator) {
31+
app.get(path || '/ecv', function(req, res) {
32+
var tosend = {
33+
date : new Date ,
34+
port : port
35+
};
36+
var options = options || {
37+
host: 'localhost',
38+
port: port,
39+
path: monitor || '/',
40+
method: 'GET',
41+
headers: {
42+
host:'localhost',
43+
connection:'close',
44+
accept: 'application/json'
45+
46+
}
47+
};
48+
var creq = http.request(options, function(cres) {
49+
cres.setEncoding('utf8');
50+
var data = '';
51+
cres.on('data', function(chunk) {
52+
data = data + chunk;
53+
});
54+
55+
cres.on('end', function() {
56+
if(cres.statusCode >= 300) {
57+
// Not happy
58+
unhappy(req, res, tosend);
59+
}
60+
else {
61+
try {
62+
if(validator.apply(this, [data]));
63+
happy(req, res, tosend);
64+
}
65+
catch(e) {
66+
// Not happy
67+
unhappy(req, res, tosend);
68+
}
69+
}
70+
});
71+
});
72+
creq.on('error', function(err) {
73+
unhappy(req, res, tosend.date);
74+
});
75+
creq.end();
76+
});
77+
};
78+
79+
function happy(req, res, tosend) {
80+
res.writeHead(200, {
81+
'content-type': 'text/plain',
82+
'cache-control': 'no-cache'
83+
});
84+
res.write('status=AVAILABLE&ServeTraffic=true&ip='+ req.connection.address()['address'] +'&hostname='+ hostname +'&port=' + tosend.port+ '&time=' + tosend.date.toString());
85+
res.end();
86+
}
87+
88+
function unhappy(req, res, tosend) {
89+
res.writeHead(500, {
90+
'content-type': 'text/plain',
91+
'cache-control': 'no-cache'
92+
});
93+
res.write('status=WARNING&ServeTraffic=false&ip='+ req.connection.address()['address'] +'&hostname='+ hostname +'&port=' + tosend.port + '&time=' + tosend.date.toString());
94+
res.end();
95+
}

lib/index.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2012 eBay Software Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
var Master = require('./master.js');
20+
21+
// Trap all uncaught exception here.
22+
process.on('uncaughtException', function (error) {
23+
// TODO: This has to the log file
24+
console.log(error.stack || error);
25+
});
26+
27+
exports.version = require('../package.json').version;
28+
29+
exports.listen = function (opts, createApp, cb) {
30+
opts = opts || {};
31+
opts.port = opts.port || 8080;
32+
opts.monPort = opts.monPort || 8081;
33+
opts.ecvPath = opts.ecvPath || '/ecv';
34+
35+
if(opts.cluster) {
36+
var master = new Master({
37+
pids: process.cwd() + '/pids',
38+
logs: process.cwd() + '/logs',
39+
port: opts.port,
40+
monPort: opts.monPort,
41+
ecv: opts.ecv
42+
});
43+
44+
if(opts.stop) {
45+
master.stop()
46+
}
47+
else if(opts.shutdown) {
48+
master.shutdown();
49+
}
50+
else {
51+
createApp(function (app, emitter) {
52+
master.listen(app, function () {
53+
if(cb) {
54+
cb(app, emitter);
55+
}
56+
});
57+
});
58+
}
59+
}
60+
else {
61+
createApp(function (app, emitter) {
62+
app.listen(opts.port, function () {
63+
console.log('Listening on ' + opts.port);
64+
if(cb) {
65+
cb(app, emitter);
66+
}
67+
});
68+
});
69+
}
70+
}

0 commit comments

Comments
 (0)