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

[automation] Provide equivalent of scriptUnloaded for UI generated rules #2173

Closed
rkoshak opened this issue Feb 1, 2021 · 8 comments
Closed
Labels
enhancement An enhancement or new feature of the Core

Comments

@rkoshak
Copy link

rkoshak commented Feb 1, 2021

This is a feature request.

When writing rules in Python and JavaScript (and probably Groovy as well) one can define a special function called scriptUnloaded (there is also a scriptLoaded which should be covered by this also. This function gets called when, as the name implies, the script file is unloaded.

This function is particularly useful for Timer management and working with rules that create other rules (an advanced but useful use case that is helpful for library developers), dynamically created Items, etc. In this function one can cancel, delete, and otherwise clean up all the stuff the rule created that exists outside the script itself. The most useful thing to do here would be to cancel any running timers created by the script.

This capability is missing from UI generated rules. See https://community.openhab.org/t/oh-3-examples-writing-and-using-javascript-libraries-in-mainui-created-rules/108526/33 for some discussion on this. So any Timers or other stuff created by a Script Action that exists outside of the Script Action gets orphaned and continues to exist. In the best case this will result in errors in the logs as orphaned Timers go off. In the worst case it can leave a looping Timer running forever until OH is restarted and a bunch of artifacts (Items, Rules, etc) that were dynamically created that need to be manually cleaned up afterwards.

To take one extreme case, there is a Community Library in the Helper Libraries that would consolidate the hourly forecast Items into a daily forecast. This script would create dozens of Items to accomplish this. Now that the daily forecast is supported by the API, if that capability were created in the UI it would require manually deletion of those dozens of no longer needed Items, and the links which could be quite tedious and result in problems like orphaned links which can cause problems themselves.

@openhab-bot
Copy link
Collaborator

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

https://community.openhab.org/t/oh-3-examples-writing-and-using-javascript-libraries-in-mainui-created-rules/108526/36

@jpg0
Copy link
Contributor

jpg0 commented Feb 8, 2021

FYI the way I achieve this is that I have a library (Java script extension) which allows a script to register code to run at specific times in it's lifecycle. I used to use multiple points in time, but now I only use 'unloaded'.

https://github.com/jpg0/ohj-support/blob/master/src/main/java/org/openhab/automation/module/script/extension/Lifecycle.java

Personally I believe that this method is superior to the 'special function' approach because:

  • It can work with any language that supports script extensions, not only native jsr233
  • It allows very straightforward addition of more 'hooks' - times or events that cause the code to run
  • It doesn't require dropping any 'special' names into the global namespace which can clash with other things

The actual boilerplate code that needs to be in place in either solution is similar.

@rkoshak
Copy link
Author

rkoshak commented Feb 8, 2021

I'm not tied to any specific solution or approach. I like the idea of an approach that works for all languages (including Rules DSL). The main need is to be able to clean up when a rule gets unloaded.

Though that Java script extension would need to be added to and built into the core, right? So using it would be out of reach for most users right now.

@jpg0
Copy link
Contributor

jpg0 commented Feb 8, 2021

I think we're agreed; my approach handles what you need plus is more flexible than the scriptUnloaded mechanism.

And yes, I'm not suggesting that users should install a new script extension, I agree this should go into core. I just wanted to link to something to demonstrate how it could work in case anyone wants to code it (or any individual really needs it now and can install it).

(Plus BTW I actually did submit a PR a while back related to this: #1635 although my code included some bits to handle OSGi too, which was maybe a little too far.)

@mhilbush
Copy link
Contributor

I like the idea of an approach that works for all languages (including Rules DSL). The main need is to be able to clean up when a rule gets unloaded.

And for UI DSL rules, there's another issue that may be related.

#2213

@jimtng
Copy link
Contributor

jimtng commented Nov 8, 2021

You could use lifecycleTracker.addDisposeHook(() => yourCleanupRoutine())

this lifecycleTracker is already merged in #1264

However, I don't know about JS, but it is not currently accessible / usable from within JRuby due to its internal scope. I've submitted #2565 to make it accessible/usable.

@J-N-K
Copy link
Member

J-N-K commented May 14, 2022

@rkoshak With regard to #2173 (comment), can this be closed? It looks like this can be used to achieve what you want.

@J-N-K J-N-K added the enhancement An enhancement or new feature of the Core label May 14, 2022
@rkoshak
Copy link
Author

rkoshak commented May 14, 2022

Yes, I think this can be closed now, though it's not clear that's available in Rules DSL. It was my intent to include that as well as the other languages.

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

No branches or pull requests

6 participants