-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
startPool.js
112 lines (94 loc) · 2.92 KB
/
startPool.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
const { spawn } = require('child_process');
const http = require('http');
/**
* Pool testing script
*
* Creates a pool of nodes to use for testing
*/
const configs = [
{
port: 1240,
databaseName: 'db_rutile',
privateKey: 'C0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DE',
},
{
port: 1234,
databaseName: 'db_rutile1234',
privateKey: '20DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DE',
},
{
port: 1236,
databaseName: 'db_rutile1236',
privateKey: '30DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DE',
},
{
port: 1235,
databaseName: 'db_rutile1235',
privateKey: '40DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DE',
},
{
port: 1237,
databaseName: 'db_rutile1237',
privateKey: '50DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DE',
},
{
port: 1238,
databaseName: 'db_rutile1238',
privateKey: '60DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DE',
},
{
port: 1239,
databaseName: 'db_rutile1239',
privateKey: '70DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DE',
},
{
port: 1230,
databaseName: 'db_rutile1230',
privateKey: '80DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DEC0DE',
},
];
const nodes = [];
function spawnNode(port, dbName, pk) {
const node = spawn('node', ['./build/rutile.js', '--port', port, '--databaseName', dbName, '--privateKey', pk, '--nodesListUrl', 'http://localhost:8903/examples/network-file/RutileNodes.json']);
nodes.push(node);
node.stdout.on('data', (data) => {
console.log(`Node[${port}:${dbName}] -> ${data}`);
});
node.stderr.on('data', (data) => {
console.error(`Node[${port}:${dbName}] -> ${data}`);
});
}
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
const onlineNodes = [];
function startNodeListServer() {
const httpServer = http.createServer((req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.writeHead(200, {
'Content-Type': 'application/json',
});
const result = onlineNodes.map((config) => {
return {
nodeId: config.databaseName,
nodeUrl: `http://localhost:${config.port}`,
dbUrl: `http://localhost:5984/${config.databaseName}`,
};
});
res.end(JSON.stringify(result));
});
httpServer.listen(8903, '0.0.0.0');
}
async function startPool() {
startNodeListServer();
for (const config of configs) {
await sleep(10000);
onlineNodes.push(config);
spawnNode(config.port, config.databaseName, config.privateKey);
}
// configs.forEach((config) => {
// });
}
startPool();