-
-
Notifications
You must be signed in to change notification settings - Fork 431
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
Add an access-tracking cache to be used in rules #2887
Conversation
@J-N-K tbh I have not been very careful with naming in the past because:
As you are probably aware I have been against auto-importing these symbols due to these issues - however as both (auto-import and common names) things have already happened, my view now is that to remediate the problem we now should try to be clear to the user when a clash actually occurs (most likely by hooking into the JS engine) if it becomes a problem. (This hasn't been done.) As for the immediate concern, I appreciate the concern of clashing with existing scripts but also fear needing to come up with more and more obscure preset names as we introduce additional ones. Not sure where we should draw the line though. |
@openhab/core-maintainers As this is a much requested feature it would be great if someone could find the time to review. About half of the changed lines are tests. |
This pull request has been mentioned on openHAB Community. There might be relevant details there: |
Could this make it into 3.4? |
This is also on our "wishlist" at JS Scripting, as well as it is closing 3 issues at once. @openhab/core-maintainers Would be great if it could make it into 3.4.0. |
I'll chime in. From the end user's perspective, this will be very important to universally support many use cases that are only possible through sharing variables between script actions and conditions or preserving variables between runs of a single script action/condition. I've been anticipating this PR's merge for some time. It would be awesome if we could get it into 3.4 release. |
Who do we need to ping? Perhaps this is overlooked? |
I already pinged the core maintainers, if we want to, we can also explicitely ping kaikreuzer, cweitkamp or wborn. |
Yes it will be a nice addition and it's not that much code too. 🙂
I think it is truly shared between all scripts so you can also use it between different scripting engines, right? Might be handy if you decide to migrate from one engine to another one day. |
Signed-off-by: Jan N. Klug <github@klug.nrw>
Signed-off-by: Jan N. Klug <github@klug.nrw>
Signed-off-by: Jan N. Klug <github@klug.nrw>
2595b79
to
d520ca2
Compare
It seems that there is an issue with using this new feature in JSScripting as well as DSL rules. @florian-h05, do you know how to import additional presets? According to the JSR223 documentation this should work via
|
@J-N-K you can reference it like this:
(assuming that it's in the default set of presets) |
@J-N-K const { sharedCache, privateCache } = require('@runtime/cache'); |
Thanks, I can confirm that
successfully logs |
I also found a way to make the cache available in DSL. It feels a bit like an ugly hack, so I would prefer to do that in another PR, as it‘ll most likely be a longer review process. |
So this is finished and ready for review now? |
Yes, it‘s also rebased and confirmed working with latest changes to automation. |
...t/src/main/java/org/openhab/core/automation/module/script/rulesupport/shared/ValueCache.java
Outdated
Show resolved
Hide resolved
…src/main/java/org/openhab/core/automation/module/script/rulesupport/shared/ValueCache.java
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice! 👍 Let's do some testing with a bigger audience. 🚀
Can you also create a PR to add some docs for this?
Is the plan now also to remove the existing SharedCache from the jsscripting add-on to prevent "user cache confusion"? |
Yes, but the coexistence should not lead to real user confusion, as the SharedCache is not documented directly and only exposed through the helper library. |
@J-N-K Does the cancellation of timers work for you in JS Scripting? I am using the following script, and I can edit it and save it, openHAB reloads the script, but the timer runs (you need to have a look at the log timestamps, there should run no timer 15 seconds after the first reload of the script): const { privateCache } = require('@runtime/cache');
const { actions, time } = require('openhab');
console.log(privateCache.get('timer'));
const timer = actions.ScriptExecution.createTimer(time.toZDT().plusSeconds(15), () => {
console.warn('Timer ran.');
})
privateCache.put('timer', timer); Log:
|
Can't work, because the import is wrong. |
Upgrades the included openhab-js version to 3.1.0, which uses the new caches from core (introduced in openhab/openhab-core#2887) and provides many doc improvements. Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Removes the SharedCache from the addon because this functionality is now provided by core (see openhab/openhab-core#2887). Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Upgrades the included openhab-js version to 3.1.0, which uses the new caches from core (introduced in openhab/openhab-core#2887) and provides many doc improvements. Removes the SharedCache from the addon because this functionality is now provided by core (see openhab/openhab-core#2887). Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Upgrades the included openhab-js version to 3.1.0, which uses the new caches from core (introduced in openhab/openhab-core#2887) and provides many doc improvements. Removes the SharedCache from the addon because this functionality is now provided by core (see openhab/openhab-core#2887). Signed-off-by: Florian Hotze <florianh_dev@icloud.com> Signed-off-by: Ben Rosenblum <rosenblumb@gmail.com>
Upgrades the included openhab-js version to 3.1.0, which uses the new caches from core (introduced in openhab/openhab-core#2887) and provides many doc improvements. Removes the SharedCache from the addon because this functionality is now provided by core (see openhab/openhab-core#2887). Signed-off-by: Florian Hotze <florianh_dev@icloud.com> Signed-off-by: Andras Uhrin <andras.uhrin@gmail.com>
Upgrades the included openhab-js version to 3.1.0, which uses the new caches from core (introduced in openhab/openhab-core#2887) and provides many doc improvements. Removes the SharedCache from the addon because this functionality is now provided by core (see openhab/openhab-core#2887). Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Upgrades the included openhab-js version to 3.1.0, which uses the new caches from core (introduced in openhab/openhab-core#2887) and provides many doc improvements. Removes the SharedCache from the addon because this functionality is now provided by core (see openhab/openhab-core#2887). Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Upgrades the included openhab-js version to 3.1.0, which uses the new caches from core (introduced in openhab/openhab-core#2887) and provides many doc improvements. Removes the SharedCache from the addon because this functionality is now provided by core (see openhab/openhab-core#2887). Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Signed-off-by: Jan N. Klug <github@klug.nrw> GitOrigin-RevId: 028724a
Upgrades the included openhab-js version to 3.1.0, which uses the new caches from core (introduced in openhab/openhab-core#2887) and provides many doc improvements. Removes the SharedCache from the addon because this functionality is now provided by core (see openhab/openhab-core#2887). Signed-off-by: Florian Hotze <florianh_dev@icloud.com> Signed-off-by: Andras Uhrin <andras.uhrin@gmail.com>
Closes #2084
Closes #2881
Closes #2943
Depends on #2886
This implements option 1 that was discussed in #2084 (which seems to be what most comments prefer). It is remotely based on the code here: https://github.com/openhab/openhab-addons/blob/4f4dfcca20f39c2d8ed059b67e1dba09c9c83957/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scope/SharedCache.java.
Two separate key-value-caches are implemented:
privateCache
: It is created perscriptIdentifier
and can only be accessed from the same script. It is not cleared between two subsequent runs of a rule but removed when the script is unloaded. This can be used e.g. to store timers.sharedCache
: It is shared between all scripts. Every access to a certain key in the cache is tracked. The key is removed from the map if all scripts that ever accessed (put
orget
) that key have been unloaded.Since script unloading is tracked by the framework nothing special needs to be done in the script itself. That makes it usable also in UI rules, where
scriptUnloaded
is not available.I am currently investigating if I can auto-cancelScheduledFutures
andTimer
when a key is removed (i.e. the last script that used the key was unloaded).ScheduledFuture<?>
andTimer
objects are cancelled when the script is unloaded in a private cache or if the last accessor was removed in the shared cache. They are not cancelled onremove
because I might be that the script wants to use it or take special actions before cancellation.@jpg0: do you think the preset name
cache
should be changed, so that we do not run into conflicts? I changed the case of thesharedcache
to be more consistent with the camel-case we use in general. As an alternative, I could implementsharedcache
as an alias tosharedCache
and log a warning if that is used instead of the new name. The old name could then be removed in a later version.Signed-off-by: Jan N. Klug github@klug.nrw