Skip to content

Commit

Permalink
Merge pull request #23 from Shivanshu-lambdatest/master
Browse files Browse the repository at this point in the history
Detached Mode in Node Tunnel
  • Loading branch information
Shivanshu-lambdatest authored Aug 29, 2022
2 parents 2e14d45 + 67f390e commit d97e193
Show file tree
Hide file tree
Showing 3 changed files with 3,531 additions and 19 deletions.
119 changes: 112 additions & 7 deletions lib/tunnel.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const axios = require('axios');
var childProcess = require('child_process'),
TunnelBinary = require('./tunnel_binary'),
split = require('split'),
Expand All @@ -16,6 +17,8 @@ var childProcess = require('child_process'),
packageName = package_.name,
usingPorts = [],
globalForceRetries = 0;
const maxBinaryExecutionRetries = 5
const maxCheckTunnelStatusRetries = 10

/**
* Tunnel is a function based Class.
Expand Down Expand Up @@ -96,13 +99,25 @@ function Tunnel() {
self.binaryPath = binaryPath;
// Run binary after getting this and attempting atmost 5 times
console.log(`Starting tunnel`);
runBinary_(self, 5, function(e, response) {
if (e) {
return reject(e);
} else {
return resolve(response);
if (options.detachedMode === 'true') {
console.log("Running Tunnel in Detached Mode")
let tunnelStatus = runBinaryV2_(self, maxBinaryExecutionRetries)
if (tunnelStatus){
return Promise.resolve(true)
}
});
return Promise.reject()

} else{
runBinary_(self, maxBinaryExecutionRetries, function(e, response) {
if (e) {
return reject(e);
} else {
return resolve(response);
}
});

}

});
});
});
Expand Down Expand Up @@ -160,7 +175,17 @@ function Tunnel() {
self.binaryPath = binaryPath;
// Run binary after getting this and attempting atmost 5 times
console.log(`Starting tunnel`);
runBinary_(self, 5, fnCallback);
if (options.detachedMode === 'true') {
console.log("Running Tunnel in Detached Mode")
let tunnelStatus = runBinaryV2_(self, maxBinaryExecutionRetries)
if (tunnelStatus){
return Promise.resolve(true)
}
return Promise.reject()

} else{
runBinary_(self, maxBinaryExecutionRetries, fnCallback);
}
});
});
});
Expand Down Expand Up @@ -936,5 +961,85 @@ function sleep(sleepDuration) {
/* do nothing */
}
}
/**
* getTunnelID is used to Run Tunnel Binary
* @param {number} infoAPIPort running local Server Port.
* @param {number} retries max attempt, retries to get port.
* @return {number/boolean} Return id/false whether tunnel is Running Successfully or
* not.
*/
async function getTunnelID(infoAPIPort, retries){
try {
if (retries > 0) {
var url = 'http://127.0.0.1:' + infoAPIPort + '/api/v1.0/info';

try {
const response = await axios.get(url)

if (response.data && response.data.data && response.data.data.id){
let tunnelID = response.data.data.id
console.log("tunnel started with ID: ", tunnelID)
return tunnelID

}

} catch (_) {
console.log("Tunnel is not yet started. Retrying in 2 seconds")
}
await new Promise(r => setTimeout(r, 2000));
console.log("check tunnel status attempts left: ", retries - 1);
return getTunnelID(infoAPIPort, retries - 1)
}
} catch (error){
console.error(error)
}
return false

}

/**
* runBinaryV2_ is used to Run Tunnel Binary
* @param {Tunnel} self is Current Tunnel Instance.
* @param {number} retries max attempt try to Run tunnel.
* @return {boolean} Return true/false whether tunnel is Running Successfully or
* not.
*/
async function runBinaryV2_(self, retries) {
console.log("Detached Mode Retries Left: ", retries)
try{
if (retries >= 0) {
// addArguments_ method return formatted argumants or error if any.
// Run Binary with argumants in spawn process.
let binaryArguments = []
await addArguments_(self, (bool, data)=>{
binaryArguments = [...data]
})
var subprocess = childProcess.spawn(self.binaryPath, binaryArguments, {
detached:true,
stdio:'ignore'
});
subprocess.unref()

let indexOfInfoAPIPort = binaryArguments.indexOf("--infoAPIPort")

if (indexOfInfoAPIPort != -1 && indexOfInfoAPIPort < binaryArguments.length){
let infoAPIPortValue = binaryArguments[indexOfInfoAPIPort + 1]
let tunnelID = await getTunnelID(infoAPIPortValue, maxCheckTunnelStatusRetries)
if (tunnelID){
console.log("You can start testing now. Tunnel Started Succesfully with ID: ", tunnelID)
return true
}

} else {
return Error("Invalid Arguments")
}
}

} catch (error){
console.log("Error while starting tunnel, Retrying ....")
runBinaryV2_(self, retries - 1)

}
}
module.exports = Tunnel;
module.exports.Tunnel = Tunnel;
Loading

0 comments on commit d97e193

Please sign in to comment.