Skip to content
This repository was archived by the owner on Dec 13, 2023. It is now read-only.

Commit d16b52d

Browse files
committed
Better handling of tunnels started with the ECM
1 parent 921b046 commit d16b52d

File tree

3 files changed

+58
-19
lines changed

3 files changed

+58
-19
lines changed

api.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,17 @@ module.exports = function(username, authkey, env){
127127
return callback(err, resp);
128128
})
129129
},
130+
isTunnelAlive: function(tunnelId, callback){
131+
makeApiCall(server, 'GET', 'tunnels/' + tunnelId, null, username, authkey, (err, resp) => {
132+
if (err) return callback(err)
133+
// console.log('we got: ' + util.inspect(resp))
134+
if (resp.active !== undefined){
135+
return callback(null, resp.active);
136+
} else {
137+
return callback(null, false)
138+
}
139+
})
140+
},
130141
checkTunnelIp: function(callback){
131142
makeApiCall(server, 'GET', 'tunnels/checkIp', null, username, authkey, (err, resp) => {
132143
// console.log('got resp for checkTunnelIp');

cbt_tunnels.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -640,9 +640,11 @@ function cbtSocket(api, params) {
640640
fs.unlink(self.ready, function(err){
641641
if(err){
642642
global.logger.error(err);
643-
setTimeout(function(){
644-
process.exit(1);
645-
},10000);
643+
if (!self.nokill){
644+
setTimeout(function(){
645+
process.exit(1);
646+
},10000);
647+
}
646648
}
647649
})
648650
}
@@ -652,18 +654,20 @@ function cbtSocket(api, params) {
652654
self.end(function(err, killit){
653655
if(!err && killit === 'killit'){
654656
global.logger.info('Local connection disconnected.');
657+
if(cb) cb(null,true)
655658
if(!self.nokill){
656659
global.logger.info('Bye!');
657-
if(cb) cb(null,true)
658660
process.exit(0);
659661
}
660662
if(cb) cb(null,true)
661663
}else if(err){
662664
global.logger.error(err);
663-
setTimeout(function(){
664-
if(cb) cb(err,false)
665-
process.exit(1);
666-
}, 10000);
665+
if(cb) cb(err,false)
666+
if(!self.nokill){
667+
setTimeout(function(){
668+
process.exit(1);
669+
}, 10000);
670+
}
667671
}
668672
});
669673
}
@@ -679,4 +683,4 @@ function cbtSocket(api, params) {
679683
}
680684
}
681685

682-
module.exports = cbtSocket;
686+
module.exports = cbtSocket;

tunnel_start.js

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,13 @@ var startTunnel = function(api, params, cb){
159159
cbts.endWrap();
160160
}else{
161161
warn(err);
162-
setTimeout(function(){
163-
process.exit(1);
164-
},10000);
162+
if (!cbts.nokill){
163+
setTimeout(function(){
164+
process.exit(1);
165+
},10000);
166+
} else {
167+
throw err;
168+
}
165169
}
166170
})
167171
}
@@ -322,7 +326,15 @@ module.exports = {
322326
if (err){
323327
return cb(err)
324328
} else {
325-
var i = setInterval(()=>{}, 10000);
329+
var i = setInterval(()=>{
330+
// console.log(`checking if tunnel ${tunnelObject.tunnel_id} is still alive`)
331+
api.isTunnelAlive(tunnelObject.tunnel_id, (err, isAlive) =>{
332+
if (!isAlive){
333+
// console.log(`tunnel ${tunnelObject.tunnel_id} is dead!`)
334+
clearInterval(i)
335+
}
336+
})
337+
}, 10000);
326338
process.on('SIGINT',function(){
327339
// kill tunnel
328340
api.deleteTunnel(tunnelObject.tunnel_id, (err) => {
@@ -333,7 +345,10 @@ module.exports = {
333345
fs.unlinkSync(params.ready);
334346
}
335347
global.logger.info('killed connection manager tunnel, quitting')
336-
process.exit(0)
348+
clearInterval(i);
349+
if (!params.nokill){
350+
process.exit(0);
351+
}
337352
});
338353
global.logger.info('\nAttempting a graceful shutdown...');
339354
});
@@ -349,7 +364,10 @@ module.exports = {
349364
fs.unlinkSync(params.ready);
350365
}
351366
global.logger.info('killed connection manager tunnel, quitting')
352-
process.exit(0)
367+
clearInterval(i);
368+
if (!params.nokill){
369+
process.exit(0)
370+
}
353371
});
354372
});
355373
if(params.ready){
@@ -381,13 +399,19 @@ module.exports = {
381399
fs.unlinkSync(params.ready);
382400
}
383401
global.logger.info('killed connection manager tunnel, quitting')
384-
process.exit(0)
402+
clearInterval(i);
403+
if (!params.nokill){
404+
process.exit(0);
405+
}
385406
});
386407
}else{
387408
warn(err);
388-
setTimeout(function(){
389-
process.exit(1);
390-
},10000);
409+
clearInterval(i);
410+
if (!params.nokill){
411+
setTimeout(function(){
412+
process.exit(1);
413+
},10000);
414+
}
391415
}
392416
})
393417
}

0 commit comments

Comments
 (0)