How to handle timeout when executing sleep script from java code

10 views
Skip to first unread message

Alen

unread,
Oct 16, 2008, 8:42:47 AM10/16/08
to Sleep Developers
Hi,

I am executing a sleep script from Java code using bridges as shown
below:

=======================================================================
ScriptLoader loader = new ScriptLoader();

loader.addSpecificBridge(new MyLoadableBridge());
StringBuffer jarPath = new StringBuffer(<sleep_jar_path>);

ParserConfig.setSleepClasspath(jarPath.toString());

String sleepScript = <sleep_script_path>;

ScriptInstance scriptLoad = loader.loadScript();
scriptLoad.runScript();

Scalar result = scriptLoad.callFunction("&"+"func_name", stack);

=======================================================================

My question is how can I set some kind of timeout for executing the
script.
I meant if the script gets stuck (hangs) while execution, it should
wait for the given timeout. If the script execution doesn't get
completed within the timeout, the flow should come back to Java code.

Please provide me your valuable suggestions.


Thanks in advance
- Alen

Raphael Mudge

unread,
Oct 16, 2008, 11:17:58 AM10/16/08
to sleep-de...@googlegroups.com
Hi Alen,
The core Sleep interpreter doesn't support this feature. Your best
option is to execute the Sleep code in a separate thread and then
call join on that thread with your timeout specified in
milliseconds. You can force the script in the thread to stop
executing by flagging a return in the script environment yourself.
Here is a skeleton of how I'd do this:

public class SafeSleep implements Runnable
{
protected Scalar result = null;
protected ScriptInstance scriptLoader;

public Scalar callSleepScript(long timeout)
{


> ScriptLoader loader = new ScriptLoader();
>
> loader.addSpecificBridge(new MyLoadableBridge());
> StringBuffer jarPath = new StringBuffer(<sleep_jar_path>);
>
> ParserConfig.setSleepClasspath(jarPath.toString());
>
> String sleepScript = <sleep_script_path>;

> scriptLoad = loader.loadScript();

Thread fred = new Thread(this);
fred.run();
fred.join(timeout);

// this may not be necessary so comment it out if it isn't...
and if a timeout occurs you'll need to reload the script
// this line forces the script to exit... this is how &exit()
tells the interpreter to exit FYI

scriptLoad.getScriptEnvironment().flagReturn(null,
ScriptEnvironment.FLOW_CONTROL_THROW);

return result;
> }


public void run()
{
> scriptLoad.runScript();


> result = scriptLoad.callFunction("&"+"func_name", stack);

}
}

Now when you want to call a SafeSleep with a timeout you can do new
SafeSleep().callSleepScript(10000);

I haven't tested this code but I assume the skeleton of it should
suit your purposes.

Good luck.

-- Raphael

P.S. you're not flagged to receive email from the mailing list so I
BCC'd you this reply.

Andreas Ravnestad

unread,
Oct 16, 2008, 1:44:27 PM10/16/08
to sleep-de...@googlegroups.com
I think this could be a nice-to-have general feature, especially in
the context of web applications.

-Andreas

Raphael Mudge

unread,
Oct 16, 2008, 3:01:01 PM10/16/08
to sleep-de...@googlegroups.com
I could see adding an API like SleepUtils.runScript(..., long
timeout). Unfortunately I have so many combinations of
SleepUtils.runScript(...) from unmanaged organic API growth that
adding it will be lots of extra functions in the SleepUtils class.
*sigh*

At least there is a work around :)

-- Raphael

P.S. now that all the memory issues are under control, any interest
in another release of the Moconti Web App server? Its hosting
www.polishmywriting.com, jircii.dashnine.org, and
sleep.dashnine.org. Its been running for a few weeks with no problems.

Reply all
Reply to author
Forward
0 new messages