spring-projects / spring-shell

Spring based shell
http://projects.spring.io/spring-shell/
Apache License 2.0
723 stars 395 forks source link

Spring Shell: Long running Command execution interruption #296

Open nagraj321 opened 4 years ago

nagraj321 commented 4 years ago

I am using Spring Shell to run some long running command. Is there anyway to provide some text input to interrupt long running command?

e.g. i'm writing some data to file which is very time consuming task. By providing pause text i would like to pause writing data to file.

How can we achieve this using spring shell while command is invoked?

ericbottard commented 4 years ago

If you really want input text to be the cause of interruption, then you just need to have your command periodically poll for text input. If you want a signal to interrupt your command, then you need to somehow reconfigure the CTRL-C handling installed by Spring Shell and have it notify your command logic to stop.

Hope that helps

nagraj321 commented 4 years ago

How can I pool for the text input while command is in progress? As command is in progress it is not taking any inputs until command action is completes. If possible Can you provide any sudo code to pool for the text?

ericbottard commented 4 years ago

Polling means that the code of the long running task must periodically look for input. In pseudo code, it means that it looks like

while (thereIsSomeWorkToDo()) {
    lookForTextInput();
    doATinyBitOfWork();
}
aPonza commented 4 years ago

If you want a signal to interrupt your command, then you need to somehow reconfigure the CTRL-C handling installed by Spring Shell and have it notify your command logic to stop.

Could you elaborate a bit here? Where should I look for if I were to need CTRL-C to stop my ShellMethod and not the whole application? At the moment it's not intercepted at all during a loop from the method, by the way.

nagraj321 commented 4 years ago

while (thereIsSomeWorkToDo()) { lookForTextInput(); doATinyBitOfWork(); }

I have tried this and now getting another problem. Its prompting the options two times.

Operation is paused. Enter to continue. Enter to exit the utility. [resume, quit] [resume, quit]