Skip to content

Commit 2bebc1d

Browse files
committed
Added wasp heartbeats, local wasp spawning from hive and wrk timeout option
1 parent bbd2bed commit 2bebc1d

File tree

4 files changed

+184
-66
lines changed

4 files changed

+184
-66
lines changed

cli/wwb-cli-spawn-local.js

Lines changed: 70 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,69 +5,77 @@ var pwd = require('path').dirname(require.main.filename);
55
var config = require(pwd + '/../config/config.json');
66
const proc = require('child_process');
77
const fs = require('fs');
8+
const request = require('request');
89

9-
program.on('command:*', function () {
10+
program.on('command:*', function()
11+
{
1012
console.error('Invalid command: %s\nSee --help for a list of available commands.', program.args.join(' '));
1113
process.exit(1);
1214
});
1315

14-
var spawn = function() {
16+
var spawn = function()
17+
{
1518
console.log('Starting Hive...')
16-
var h = require('child_process').spawn('node', [pwd + '/../hive/hive.js', config.instance.hive.port, require('path').resolve(program.log)], {
19+
var h = require('child_process').spawn('node', [pwd + '/../hive/hive.js', config.instance.hive.port, require('path').resolve(program.log)],
20+
{
1721
detached: true
1822
});
19-
setTimeout(()=>{
23+
setTimeout(() =>
24+
{
2025
config.instance.hive.pid = h.pid;
21-
var wCount = program.wasps || 1;
22-
var port = 4268;
23-
console.log('Starting '+wCount+' Wasps...')
24-
for (var i = 0; i < wCount; i++) {
26+
console.log('Starting Wasps...')
27+
request(
28+
{
29+
method: 'GET',
30+
uri: `http://${config.instance.hive.ip}:${config.instance.hive.port}/hive/spawn/local/${program.wasps || 1}`
31+
}, (err, httpResponse, body) =>
32+
{
33+
console.log(body)
34+
saveConfig();
35+
process.exit();
36+
})
2537

26-
var s = require('child_process').spawn('node', [pwd + '/../wasp/wasp.js', `http://127.0.0.1:${config.instance.hive.port}/`, port, require('path').resolve(program.log)], {
27-
detached: true
28-
});
29-
config.wasps.push({
30-
port: port,
31-
pid: s.pid
32-
});
33-
port -= 1;
34-
}
35-
console.log('done');
36-
saveConfig();
37-
process.exit();
38-
},500)
38+
}, 500)
3939

4040
}
4141

42-
var stopInstances = function() {
43-
if (config.instance && config.instance.type == 'local' && config.wasps) {
44-
console.log('Stopping Hive...')
45-
try {
46-
process.kill(config.instance.hive.pid)
47-
console.log('Stopped')
48-
} catch (e) {
49-
console.log('Cant stop Hive')
50-
}
51-
52-
for (var i = 0; i < config.wasps.length; i++) {
53-
console.log('Stopping Wasp' + config.wasps[i].pid + '...')
54-
try {
55-
process.kill(config.wasps[i].pid)
42+
var stopInstances = function(cb)
43+
{
44+
if (config.instance && config.instance.type == 'local' && config.wasps)
45+
{
46+
console.log('Stopping wasps...')
47+
request(
48+
{
49+
method: 'DELETE',
50+
uri: `http://${config.instance.hive.ip}:${config.instance.hive.port}/hive/torch/local`
51+
}, (err, httpResponse, body) =>
52+
{
53+
console.log(body)
54+
console.log('Stopping Hive...')
55+
try
56+
{
57+
process.kill(config.instance.hive.pid)
5658
console.log('Stopped')
57-
} catch (e) {
58-
console.log('Cant stop Wasp' + config.wasps[i].pid + '...');
5959
}
60-
}
61-
62-
config.instance.hive.pid = null;
63-
config.wasps = [];
64-
saveConfig();
65-
} else {
60+
catch (e)
61+
{
62+
console.log('Cant stop Hive')
63+
}
64+
config.instance.hive.pid = null;
65+
config.wasps = [];
66+
saveConfig();
67+
cb();
68+
})
69+
}
70+
else
71+
{
6672
console.log('Does not seem to be any instance to stop.')
73+
cb();
6774
}
6875
}
6976

70-
var saveConfig = function() {
77+
var saveConfig = function()
78+
{
7179
fs.writeFileSync(pwd + "/../config/config.json", JSON.stringify(config));
7280
console.log('Config saved')
7381
}
@@ -81,32 +89,41 @@ program
8189
program
8290
.command('start')
8391
.description('Start WWB services')
84-
.action((cmd, prog) => {
85-
if (!config.instance || config.instance.type != 'local') {
92+
.action(async (cmd, prog) =>
93+
{
94+
if (!config.instance || config.instance.type != 'local')
95+
{
8696
config = {
87-
instance: {
97+
instance:
98+
{
8899
type: 'local',
89-
hive: {
100+
hive:
101+
{
90102
ip: '0.0.0.0',
91103
port: program.hivePort || '4269'
92104
},
93-
wasp: {
105+
wasp:
106+
{
94107
ip: '0.0.0.0'
95108
}
96109
}
97110
}
98111
config.wasps = [];
99-
} else {
100-
stopInstances();
112+
spawn();
101113
}
114+
else
115+
{
116+
stopInstances(spawn);
117+
}
118+
102119

103-
spawn();
104120
});
105121

106122
program
107123
.command('stop')
108124
.description('Stop all WWB services that are running')
109-
.action((cmd, prog) => {
125+
.action((cmd, prog) =>
126+
{
110127
stopInstances();
111128
});
112129

hive/hive.js

Lines changed: 94 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,61 @@ const fastify = require('fastify')();
77
const request = require('request');
88
var convert = require('convert-units');
99
const fs = require('fs');
10+
var pwd = require('path').dirname(require.main.filename);
1011

12+
var hivePort = process.argv[2] || process.env.WWB_HIVE_PORT || 4269;
1113
var running = false;
1214
var runTimeout = null;
1315
var duration = 0;
1416
var wasps = [];
17+
var localWasps = [];
1518
var waspDoneCount = 0;
1619
var waspsRunningCount = 0;
20+
var waspLocalPortIndex = 42690; //decrements per wasp
1721
var runTimeStamp = 0;
1822
var idCount = 0;
1923
var report = null;
24+
var logPath = null;
2025

2126
if (process.argv[3])
2227
{
23-
var path = require('path').resolve(process.argv[3]);
28+
logPath = require('path').resolve(process.argv[3]);
2429

2530
console.log = console.error = function(d)
2631
{
27-
fs.appendFileSync(path, d+'\n');
32+
fs.appendFileSync(logPath, d+'\n');
2833
};
2934
}
3035

36+
fastify.get('/wasp/heartbeat/:port', (req, res) =>{
37+
var found = null;
38+
for (var i = 0; i < wasps.length; i++)
39+
{
40+
if (wasps[i].ip == req.ip && wasps[i].port == req.params.port)
41+
{
42+
found = i;
43+
break;
44+
}
45+
}
46+
47+
if(found === null)
48+
{
49+
res.code(400).send();
50+
}
51+
else
52+
{
53+
if(wasps[found])
54+
{
55+
wasps[found].lastHeartbeat = Number(process.hrtime.bigint())/ 1000000;
56+
res.code(200).send();
57+
}
58+
else
59+
{
60+
res.code(400).send();
61+
}
62+
}
63+
})
64+
3165
fastify.get('/wasp/checkin/:port', (req, res) =>
3266
{
3367
var found = null;
@@ -43,9 +77,10 @@ fastify.get('/wasp/checkin/:port', (req, res) =>
4377
var wasp = {
4478
ip: req.ip,
4579
port: req.params.port,
46-
id: 'wasp' + idCount++
80+
id: 'wasp' + idCount++,
81+
lastHeartbeat: Number(process.hrtime.bigint())/ 1000000
4782
}
48-
if (found == null)
83+
if (found === null)
4984
{
5085
wasps.push(wasp);
5186
}
@@ -54,15 +89,13 @@ fastify.get('/wasp/checkin/:port', (req, res) =>
5489
wasps[found] = wasp;
5590
}
5691

57-
5892
res.code('200').send(
5993
{
6094
id: wasp.id
6195
});
6296

6397
console.log(`Wasp ${idCount-1} checking in at ${wasp.ip}!`);
6498
console.log(`Total Wasps: ${wasps.length}`)
65-
6699
})
67100

68101
fastify.get('/wasp/list', (req, res) =>
@@ -143,7 +176,11 @@ fastify.put('/wasp/reportin/:id/failed', (req, res) =>
143176

144177
fastify.put('/hive/poke', (req, res) =>
145178
{
146-
if (!isRunningRes(res))
179+
if(wasps.length <= 0)
180+
{
181+
res.code(400).send('There are no wasps to make angry.');
182+
}
183+
else if (!isRunningRes(res))
147184
{
148185
if (!req.body.target)
149186
{
@@ -185,10 +222,26 @@ fastify.put('/hive/poke', (req, res) =>
185222
}
186223
})
187224

225+
fastify.delete('/hive/torch/local', (req, res) =>
226+
{
227+
res.code(200).send(`R.I.P All ${wasps.length} wasps. :'(`);
228+
229+
for (var i = 0; i < localWasps.length; i++) {
230+
process.kill(localWasps[i].pid);
231+
}
232+
localWasps = [];
233+
console.log('f');
234+
})
235+
188236
fastify.delete('/hive/torch', (req, res) =>
189237
{
190238
res.code(200).send(`R.I.P All ${wasps.length} wasps. :'(`);
239+
191240
wasps = [];
241+
for (var i = 0; i < localWasps.length; i++) {
242+
process.kill(localWasps[i].pid);
243+
}
244+
localWasps = [];
192245
console.log('f');
193246
})
194247

@@ -241,6 +294,29 @@ fastify.get('/hive/status', (req, res) =>
241294
}
242295
})
243296

297+
fastify.get('/hive/spawn/local/:amount', (req, res) =>
298+
{
299+
if (!isRunningRes(res, 200))
300+
{
301+
var wCount = req.params.amount;
302+
303+
console.log('Starting '+wCount+' Wasps...')
304+
for (var i = 0; i < wCount; i++) {
305+
306+
var s = require('child_process').spawn('node', [pwd + '/../wasp/wasp.js', `http://127.0.0.1:${hivePort}/`, waspLocalPortIndex, logPath], {
307+
detached: true
308+
});
309+
localWasps.push({
310+
ip: '127.0.0.1',
311+
port: waspLocalPortIndex,
312+
pid: s.pid
313+
});
314+
waspLocalPortIndex -= 1;
315+
}
316+
317+
res.code(200).send(`Attempted to spawn ${localWasps.length}.`);
318+
}
319+
});
244320

245321
var isRunningRes = function(res, code)
246322
{
@@ -356,5 +432,15 @@ var gError = function(route, res)
356432
console.log(`Bad thingz happened in the ${route} sectorz.`);
357433
}
358434

435+
setInterval(()=>{
436+
if(wasps.length > 0)
437+
{
438+
wasps = wasps.filter((wasp)=>{
439+
return ((Number(process.hrtime.bigint()) / 1000000)- wasp.lastHeartbeat) < 30000
440+
})
441+
}
442+
},30000)
443+
444+
359445
console.log('Hive ready to release the wasps!')
360-
fastify.listen(process.argv[2] || process.env.WWB_HIVE_PORT || 4269, '0.0.0.0')
446+
fastify.listen(hivePort, '0.0.0.0')

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "waspswithbazookas",
3-
"version": "1.0.8",
3+
"version": "1.0.9",
44
"description": "Its like bees with machine guns but way more power",
55
"main": "./hive/hive.js",
66
"scripts": {

0 commit comments

Comments
 (0)