I'm very new to node. I've written a script which does a telnet. If the telnet works we do a traceroute and exit properly (this works). When the telnet doesn't work I still do a traceroute and try to exit with exit status -1. This does not work. The traceroute is not shown and the process.exit(1) appears too early/does not wait till the output of the traceroute.
This is my script (it's a lambda):
'use strict';
const Traceroute = require('nodejs-traceroute');
const isPortReachable = require('is-port-reachable');
console.log('Loading function');
process.env.PATH = `${process.env.PATH}:${process.env.LAMBDA_TASK_ROOT}/bin`;
async function doTraceroute() {
try {
const tracer = new Traceroute();
tracer
.on('pid', (pid) => {
console.log(`pid: ${pid}`);
})
.on('destination', (destination) => {
console.log(`destination: ${destination}`);
})
.on('hop', (hop) => {
console.log(`hop: ${JSON.stringify(hop)}`);
})
.on('close', (code) => {
console.log(`close: code ${code}`);
});
tracer.trace('100.216.125.9');
} catch (ex) {
console.log(ex);
}
}
exports.handler = (event, context, callback) => {
(async () => {
console.log('Is port 9092 reachable of MC Kafka host? True or False')
let output = await isPortReachable(9092, {host: '100.216.125.9'})
if (output) {
console.log('Telnet worked: do traceroute')
await doTraceroute();
} else {
console.log('Telnet failed: do traceroute')
await doTraceroute();
console.log("Exit with error")
process.exit(1)
}
})();
};
How can I solve it? The current output when the telnet failed is:
START RequestId: 315b0ac5-09d7-4b72-8ff7-746b28241f79 Version: $LATEST
2020-11-09T07:25:07.841Z 315b0ac5-09d7-4b72-8ff7-746b28241f79 INFO Is port 9092 reachable of MC Kafka host? True or False
2020-11-09T07:25:08.851Z 315b0ac5-09d7-4b72-8ff7-746b28241f79 INFO Telnet failed: do traceroute
2020-11-09T07:25:08.909Z 315b0ac5-09d7-4b72-8ff7-746b28241f79 INFO pid: 20
2020-11-09T07:25:08.910Z 315b0ac5-09d7-4b72-8ff7-746b28241f79 INFO Exit with error
END RequestId: 315b0ac5-09d7-4b72-8ff7-746b28241f79
REPORT RequestId: 315b0ac5-09d7-4b72-8ff7-746b28241f79 Duration: 1150.28 ms Billed Duration: 1200 ms Memory Size: 128 MB Max Memory Used: 70 MB Init Duration: 176.80 ms
RequestId: 315b0ac5-09d7-4b72-8ff7-746b28241f79 Error: Runtime exited with error: exit status 1
Runtime.ExitError
without showing the traceroute. The traceroute is shown when the telnet succeeds and there is no exit 1.
You can wrap the traceroute processing in a promise and await this promise:
function doTraceroute() {
return new Promise((resolve, reject) => {
try {
const tracer = new Traceroute();
tracer
.on('pid', (pid) => {
console.log(`pid: ${pid}`);
})
.on('destination', (destination) => {
console.log(`destination: ${destination}`);
})
.on('hop', (hop) => {
console.log(`hop: ${JSON.stringify(hop)}`);
})
.on('close', (code) => {
console.log(`close: code ${code}`);
resolve();
});
tracer.trace('100.216.125.9');
} catch (ex) {
console.log(ex);
reject(ex);
}
});
}
You can then await
the returned promise:
await doTraceroute();