JRuby - stop script execution

Is there a way to stop the script after it has completed a certain amount of time?

+3
source share
3 answers

This is a rather vague question. Here are a few ideas that come to mind:

  • Use the shell as a management process. Run the JRuby script, send it to the background, sleeping for a fixed time, and then kill $!.
  • At the beginning of your JRuby script, create a thread that sleeps for a fixed time, and then kill the entire script.
  • If you use the built-in JRuby, you can use Java threads to do exactly what you want.
0
source

. ( , ...):

// jruby-complete-1.6.0.RC2.jar
import org.jruby.Ruby;

class JRubyStop {
    public static void main(String[] args) throws InterruptedException {
        final Ruby jruby = Ruby.newInstance();
        Thread jrubyThread = new Thread() {
            public void run() {
                String scriptlet = "for i in 0..100; puts i; sleep(1); end";
                jruby.evalScriptlet(scriptlet);
            }
        };
        jrubyThread.start();
        Thread.sleep(5000);
        System.out.println("interrupt!");
        jrubyThread.interrupt();
        System.out.println("interrupted?!");
    }
}

"?!" , .

: Groovy Java SSCCE (http://sscce.org/).

0

super late answer, but this is what I use:

require 'timeout'
status = Timeout::timeout(5) 

{
  # Something that should be interrupted if it takes more than 5 seconds...
}
0
source

Source: https://habr.com/ru/post/1796209/


All Articles