Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rules] Add support for pre-compilation of conditions and actions #4289

Merged
merged 5 commits into from
Jul 9, 2024
Merged

[rules] Add support for pre-compilation of conditions and actions #4289

merged 5 commits into from
Jul 9, 2024

Conversation

florian-h05
Copy link
Contributor

@florian-h05 florian-h05 commented Jun 30, 2024

This PR adds the ability for the compilation of rule script conditions and script actions on openHAB start-up or rule creation.

This is achieved by:

  • Creating the ScriptEngine on start-up or rule-creation and check if the ScriptEngine implements Compilable.
  • Call Compilable.compile(script) and store the returned Compiledscript on openHAB start-up or rule creation for ScriptConditionHandler and ScriptActionHandler.
  • Execute the CompiledScript instead of evaluating the script string if available.

Script automation add-ons need to make sure that their ScriptEngine implements the Compilable interface.

The motivation behind this PR is to allow JS scripts (as for example created by Blockly) to be compiled before they are executed, so there is no delay for the first run, which is a huge UX improvement.
From a performance POV, the compilation on start-up behaves like all rules with pre-compilable script conditions/actions would have start level triggers and would run on start-up, but without actually executing the script.

JS Scripting implementation: openhab/openhab-addons#16970

@florian-h05 florian-h05 marked this pull request as ready for review June 30, 2024 22:08
@florian-h05 florian-h05 requested a review from a team as a code owner June 30, 2024 22:08
@wborn wborn added rebuild Triggers the Jenkins PR build and removed rebuild Triggers the Jenkins PR build labels Jul 1, 2024
@florian-h05
Copy link
Contributor Author

@digitaldan FYI, while you are working on the great notification enhancements, I am not resting ;-)
Would be great if you could have a look and let me know what you think!

@digitaldan
Copy link
Contributor

Awesome ! I actually saw #16970 first and asked a dumb question there, which could have been answered if i bothered reading that this was a dependency to it ;-)

I'll take a look today !

@holgerfriedrich holgerfriedrich added rebuild Triggers the Jenkins PR build and removed rebuild Triggers the Jenkins PR build labels Jul 1, 2024
Copy link
Contributor

@digitaldan digitaldan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@kaikreuzer kaikreuzer added the enhancement An enhancement or new feature of the Core label Jul 3, 2024
*
* @return true if pre-compilation is supported, else false
*/
default boolean supportsPreCompilation() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need this method? Wouldn't it be sufficient to check if the script engine implements Compilable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would work as well, but then I would need to always initialize the script engine when the script action/condition is created, even if it does not implement Compilable. At the moment, the script engine is initialised on first use.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that really an issue? If the engine implements Compilable there is no extra-cost. If it doesn't it just shifts the time when the engine is created from first usage to startup (which probably also improves performance when the script is actually called).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good argument.
I thought there might be a reason it was implemented this way, but creating the engine at start up actually seems like a good idea. This also avoids that the memory usage seems to constantly grow over time.

I'll implement that.

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
…lation of scripts

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
…ctivating a rule

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
@florian-h05
Copy link
Contributor Author

@J-N-K I implemented the requested changes.
Is there a way to delay the RuleEngine start-up until automation add-ons are ready?

Otherwise the log will get spammed with:

13:49:05.117 [ERROR] [ript.internal.ScriptEngineManagerImpl] - ScriptEngine for language 'application/javascript' could not be found for identifier: bd2f2674-6fa4-4f2c-bf8c-f79409d8b692
13:49:23.622 [INFO ] [re.automation.internal.RuleEngineImpl] - Rule engine started.

It of course would be possible to add a check to the AbstractScriptModuleHandler class to early return from compileScript if no engine factory is available.

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
@florian-h05
Copy link
Contributor Author

I went ahead and do now skip the compilation if no script engine is avalailable for the language.

@J-N-K J-N-K merged commit 918b4fa into openhab:main Jul 9, 2024
5 checks passed
@florian-h05 florian-h05 deleted the script-precompilation branch July 9, 2024 17:13
@J-N-K J-N-K added this to the 4.3 milestone Jul 9, 2024
@florian-h05
Copy link
Contributor Author

Thanks for merging!

@florian-h05
Copy link
Contributor Author

@jimtng FYI, not sure if JRuby Scripting need adjustments.
I have seen that it implements Compilable, but I am not sure if it needs a change because of its call interception to save the context into a global variable.

@jlaur
Copy link
Contributor

jlaur commented Jul 14, 2024

@florian-h05 - I'm starting to see this in logs, is this expected?

2024-07-14 15:33:06.940 [ERROR] [.handler.AbstractScriptModuleHandler] - Script engine of rule with UID 'script' does not implement Compilable but claims to support pre-compilation

I think it happens when loading DSL rules, I tried saving an empty rule:

2024-07-14 15:38:25.269 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'eds.rules'
2024-07-14 15:38:25.327 [ERROR] [.handler.AbstractScriptModuleHandler] - Script engine of rule with UID 'script' does not implement Compilable but claims to support pre-compilation

@florian-h05
Copy link
Contributor Author

No, this seems like a left-over from a previous version of this PR.
I will provide a PR to clean that up.

@florian-h05
Copy link
Contributor Author

See #4319.

J-N-K pushed a commit that referenced this pull request Jul 24, 2024
Fixes an issue, where an error that compilation failed for disabled rules.
Reported on the community: https://community.openhab.org/t/oh-4-2-snapshot-disabled-rules-failed-to-compile-error-in-opehab-log/157402.
Follow-up for #4289.

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
florian-h05 added a commit to florian-h05/openhab-addons that referenced this pull request Jul 28, 2024
Fixes openhab#17165.
Caused by openhab/openhab-core#4289.

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
florian-h05 added a commit to florian-h05/openhab-js that referenced this pull request Jul 28, 2024
Caused by openhab/openhab-core#4289.

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
florian-h05 added a commit to openhab/openhab-js that referenced this pull request Jul 28, 2024
Caused by openhab/openhab-core#4289.

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
jlaur pushed a commit to openhab/openhab-addons that referenced this pull request Jul 29, 2024
…UI scripts (#17171)

* [jsscripting] Fix console logger name is missing for UI scripts
* [jsscripting] Fix timer identifier "missing" for setTimeout/setInterval for UI scripts

Fixes #17165.
Caused by openhab/openhab-core#4289.

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
digitaldan pushed a commit to digitaldan/openhab-addons that referenced this pull request Aug 29, 2024
…UI scripts (openhab#17171)

* [jsscripting] Fix console logger name is missing for UI scripts
* [jsscripting] Fix timer identifier "missing" for setTimeout/setInterval for UI scripts

Fixes openhab#17165.
Caused by openhab/openhab-core#4289.

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
@openhab-bot
Copy link
Collaborator

This pull request has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/blockly-historic-state-average-ending-with-type-error/158191/11

pgfeller pushed a commit to pgfeller/openhab-addons that referenced this pull request Sep 29, 2024
…UI scripts (openhab#17171)

* [jsscripting] Fix console logger name is missing for UI scripts
* [jsscripting] Fix timer identifier "missing" for setTimeout/setInterval for UI scripts

Fixes openhab#17165.
Caused by openhab/openhab-core#4289.

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Signed-off-by: Patrik Gfeller <patrik.gfeller@proton.me>
joni1993 pushed a commit to joni1993/openhab-addons that referenced this pull request Oct 15, 2024
…UI scripts (openhab#17171)

* [jsscripting] Fix console logger name is missing for UI scripts
* [jsscripting] Fix timer identifier "missing" for setTimeout/setInterval for UI scripts

Fixes openhab#17165.
Caused by openhab/openhab-core#4289.

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
matchews pushed a commit to matchews/openhab-addons that referenced this pull request Oct 18, 2024
…UI scripts (openhab#17171)

* [jsscripting] Fix console logger name is missing for UI scripts
* [jsscripting] Fix timer identifier "missing" for setTimeout/setInterval for UI scripts

Fixes openhab#17165.
Caused by openhab/openhab-core#4289.

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement An enhancement or new feature of the Core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants