Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After "call" command is successfully done, script is still running and never stops #71

Open
tafel opened this issue Jun 21, 2023 · 4 comments

Comments

@tafel
Copy link

tafel commented Jun 21, 2023

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.

@tafel
Copy link
Author

tafel commented Jun 21, 2023

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 :)

@icebob
Copy link
Member

icebob commented Jul 11, 2023

Nice catch, I will fix it, or if you have time, could you create a PR with the fix?

@tafel
Copy link
Author

tafel commented Jul 17, 2023

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?

@icebob
Copy link
Member

icebob commented Aug 2, 2023

Yeah, I see, it's strange. Exit code 130 means it's interrupted by SIGINT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants