Thursday, April 7, 2016

Cross platform sleep with eScript

Actually this post should be split in two, but I mixing them here... anyway, you can adapt it to your specific needs.

Now and then you might find yourself in the situation that you're doing some processing with eScript and got some kind of contention that forbids the code to continue processing... could be a resource missing (delayed generation) or other conditions.

In those cases (specially if you're doing some kind of long batch processing and don't want to abort everything), you wish that the eScript related OS process just wait a bit.

eScript has this deficiency of not providing a function for that. You don't want also to put your code in a loop doing something just to expend time because it will use CPU time for that. You want it to just sit down for a while and check later if the condition regarding the contention changed.

For that, you could use SElib.dinamicLink method. This allows to load a library and execute a function from it. It is not efficient by any mean (to load an external library) but you will (hopefully) put your process to a sleep state without expending much CPU while doing it.

The library you're going to invoke depends highly on the OS you running the Siebel Server. For MS Windows, it should be "kernel32.dll". For Linux, "libc.so" will do it for you.

If you have a mixed composition of Siebel Server on different OS's, creating a reusable function gets more complicated. There are several workarounds to solve this, but I'm specially inclined in using environment variables to check for that. Those variables are easy to use and can be checked from eScript by using the Clib.getenv method.

Here is an example:

function sleep(time) {

    try {
        if ( Clib.getenv("OS") == "Windows_NT" ) {
            var miliTime = time * 1000;
            SElib.dynamicLink("kernel32.dll", "Sleep", STDCALL, militime);
        } else {
            SElib.dynamicLink("libc.so", "sleep", time);
        }
    catch(e)
    {
        TheApplication().RaiseErrorText("Failed to invoke sleep: " + e.toString() + e.errText());
        return false;
    }
    
    return true;

}

At least my MS Windows 7 box here has this "OS" environment variable by default. Linux doesn't have one, but you could create it as well.

And then you can this function whenever you need to put your code to sleep for a while, probably inside a loop.

You could also use Clib to invoke an external program that will sleep for you, but this can not be as efficient as using the OS library function for that. You should test if you find SElib to be too slow for your case.

No comments:

Post a Comment