tags:

views:

34

answers:

1

Is it possible to restrict the amount of time the user code runs in JRuby?

+1  A: 

Not sure how you're embedding the Ruby code, but you could easily have a background timer thread that evaluates

raise YourTimeLimitExceededException, 'no more time'

after a period of time. You'd also want to disable Thread.new and Thread.start (as well as java.lang.Thread.new) for the user code, or they could easily circumvent your timer.

Nick Sieger
Can you elaborate on your answer, please? (I'm using JRuby-embed)
instantsetsuna
Have a background thread measure the time spent, and call `scriptingContainer.evalScriptlet("raise Timeout::Timeout")` from the timer thread. You could use java.util.concurrent futures to help with making sure that the timeout doesn't get raised if the script completes in time.
Nick Sieger
Thanks for the answer, I'll look into it! :)
instantsetsuna