moleculer-cli
moleculer-cli copied to clipboard
After "call" command is successfully done, script is still running and never stops
I'm using this command to run a script from a CRON:
#!/bin/sh
moleculer call \
--ns="localdev" \
-t "nats://nats:4222" \
"myservice.test"
RESULT=$?
echo "Done"
exit $RESULT
The test action is as simple as:
module.exports = {
name: 'myservice',
actions: {
test(ctx) {
return 0;
}
}
};
When running the script locally, logs show the returned value (here 0), but script doesn't stop. It's still runnnig.
Checking the call/index.js code of this repo, I see that when an error is thrown, process.exit() is called and script ends. But when everything goes right, the broker is stopped, but no process.exit() is called.
https://github.com/moleculerjs/moleculer-cli/blob/master/src/call/index.js#LL75C24-L75C24
Because of that, CRON job can't detect the end of the script. After a few days, I have tons of CRON jobs running in the wild.
Should the successful call be followed by an exit code of zero, or should I explore an other way to say to my CRON "ok, job's done"?
Thanks.
Just digging in the code, and trying some alternatives, I've found that using a config file with stopped() method defined was doing the trick.
#!/bin/sh
moleculer call \
-c "/testconfig.js" \
--ns="localdev" \
-t "nats://nats:4222" \
"myservice.test"
And the testconfig.js file:
module.exports = {
stopped() {
console.log('broker stopped');
process.exit(0);
}
};
Is this a valid practice in this case? It is working anyway for my usecase :)
Nice catch, I will fix it, or if you have time, could you create a PR with the fix?
Maybe it is not as easy as it seems. I just added process.exit(0) after the broker stops. It surely stops, but with code 130 (using #!/bin/sh as 1st line of my script).
When using the config file, with process.exit(0) in stopped() method, it exits with code 0.
Any idea?
Yeah, I see, it's strange. Exit code 130 means it's interrupted by SIGINT