Trying to execute the same ECMA script in parallel at the same time fails (sometimes)

  • Platform information:
    • Hardware: I7/8Gb/500G+_
    • OS: Windows server 2019
    • Java Runtime Environment: Zulu11.41+23-CA (build 11.0.8+10-LTS)
    • openHAB version: 3.1.0 stable

I have multiple RollerBlind items that run the same script when they receive a command (UP/DOWN/STOP etc)
The script get the Blind Address from the Item Tags and then sends the received command to the Blind Controller Item.
When I send a command to an individual rollerBlind Item then the script works as expected.
When I send a command to some/all of the items quickly then the script fails with the below error in the logs

2021-07-03 22:16:10.622 [ERROR] [e.automation.internal.RuleEngineImpl] - Failed to execute rule ‘8e2b779b7f' with status 'RUNNING'

I have a separate script which closes all blinds and I have to put a sleep between each trigger of the RollerBlind Item for it to reliably work.

var Thread = Java.type("java.lang.Thread");
// close all blinds
        var items = itemRegistry.getItemsByTag("Blinds");
        for (var i in items)
        {
          var blind = items[i];
          events.sendCommand(blind.getName(), "DOWN");
        //Thread.sleep(1000);
         
        }

In itself this sleep is not a big issue when using OpenHAB rules to close all blinds but when I ask google to close all blinds it sends a command to each Item individually and most of the time I get the “RUNNING” error in the logs.

I’ve looked around on this forum and did find there were issues in a milestone build that produced this error but from what I can tell it was fixed.

So my questions are:
Can I run multiple threads of the same script and if so what am I doing wrong?
if not then Is there a non blocking way to queue a script if its already running.

Just for completeness this is the script that is being called by the RollerBlind Items but I dont think its really relevant

// can't run multiple instances of scripts due to openhab bug?.....need delay on calling script

var logger = Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.model.script.Rules.Blinds");


var cmd = command.toString();                               // Get Command that triggered rule
var bl = ir.getItem(event.itemName);                        // Get Item that triggered Rule
var ba = bl.getTags().toString().substr(1,3);               // Get Blind address from Tag, works but ugly and relies on the blind address being the 1st tag.
var finalcmd = 'm'+"000".substring(0,3-cmd.length)+cmd+';'  // assume command is a number and pad it to 3 digits.
  
    switch(cmd)
   {
      case "UP":   finalcmd = 'm000;'
        break;
      case "DOWN": finalcmd = 'm100;'
        break;
      case "STOP": finalcmd = 's;'
   }

finalcmd="!"+ba+finalcmd;
logger.info("Blind Automation - ItemName = "+event.itemName+",  Recieved Command = "+cmd+",  Sent to Blind = "+finalcmd);
events.sendCommand('nrBlinds_Command',finalcmd);

Whilst I cannot directly help with this problem, I can say this:

  • I have been developing the next version of JS support which uses an entirely new engine (GraalVM vs. Nashorn)
  • The new engine will immediately fail if it detects any concurrent execution (I believe that the old one is optimistic - it will try to allow it, but may fail)
  • I regularly saw failures using the new engine and had to prevent any concurrent access to a single script because of this
  • This restriction is not in place for the old engine

So it kinda does surprise me that exceptions arise in this case. What I would suggest is to try to get more information about the actual error - this may help pin down the problem.

HI @jpg0. thanks for replying.

It looks like 3.1 stable comes out of the box with Nashorn still which is fine as everything still works as before but I also still have the problem with running the same script at the same time.

I’ve done some research and found that there is an automation add-on “JSScripting” which enables GraalVM that i think is yours.
I think this will probably fix my problem but theres currently issues.
I’ve tried using it on a test system and found some of my scripts dont work and this is mostly because i’m not clear about the differences between Nashorn and GraalVM. (i’m not really a programmer but know enough to be dangerous :slight_smile: )

I can access most OpenHAB specific objects with

var { itemRegistry, things, rules, events} = require('@runtime');

But i’m having trouble with anything that is specific to an OpenHAB Class.

Both of these fail

var ScriptExecution = Java.type('org.openhab.core.model.script.actions.ScriptExecution');
var voice = java.type('org.openhab.core.model.script.actions.Voice');

where these work OK

var ZonedDateTime = Java.type('java.time.ZonedDateTime');  
var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.model.script.Rules.Garage');

with error in log similar to this.

Script execution of rule with UID 'e2dc57e953' failed: org.graalvm.polyglot.PolyglotException: TypeError: Access to host class org.openhab.core.model.script.actions.ScriptExecution is not allowed or does not exist.

Looks to me to be Class Path issue with GraalVM (At least in windows) but i dont really know where to look to find the root cause.

I have seen that you have an unofficial OHJ helper library for GraalVM but i’m reluctant to use it (and any other helper library really) as these libraries may (and have in the past) break.

Not expecting an answer but i think the Docs for the JSScripting add-on need to be more througher.