-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
[jsscripting] Reimplement timer polyfills to conform standard JS #13623
[jsscripting] Reimplement timer polyfills to conform standard JS #13623
Conversation
Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
…oc for `clearAll` Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
@jpg0 @digitaldan |
To Do after Mergeopenhab-js PR is now open: openhab/openhab-js#169. Update the docs at openhab-js repo:
Update the openhab-js codebase:
|
@florian-h05 I would suggest that the better way to inject the ThreadsafeTimers object is the same way that is used for Oh and one other thing - ThreadsafeTimers is not thread safe itself (by concurrent accessors). I'm guessing that this is probably ok, but wanted to make sure you are expecting this. |
Is it still possible then to have its own instance for each script engine?
I’m not sure what you mean by that. Could you help me a little bit? |
Yes, this is the same as per-script. You can use
I mean that if two or more threads concurrently access an instance of ThreadsafeTimers, then it may fail. If access is always done by a single thread it should be fine. |
...ng/src/main/java/org/openhab/automation/jsscripting/internal/threading/ThreadsafeTimers.java
Outdated
Show resolved
Hide resolved
...ng/src/main/java/org/openhab/automation/jsscripting/internal/threading/ThreadsafeTimers.java
Outdated
Show resolved
Hide resolved
…loaded Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
…all & Use long primitive type instead of Integer Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
@jpg0 I also synchronized another entrypoint to the context using the lock object; this entrypoint was accessed when the script file was reloaded.
Got it, thanks. Until now, I've never seen a case where this happened. |
The problem with the current mechanism (rather than how SharedCache is done), is that each new piece of functionality that we need added to the environment is a custom job of explicitly modifying the creation of each environment. This is exactly the problem that the alternative mechanism solves - it is a framework for allowing new code to add new things to the environment without modifying the environment creation itself. |
Regarding thread safely: you are very unlikely to see it happen because it requires precise timing. I do believe that it's unsafe though, especially as recurring jobs are rescheduled on new threads. What it means is that very occasionally something will fail with a ConcurrentModificationException. It's not hard to protect against however, I'm happy to add some suggested changes to fix it. |
Thanks for the help. I'll wait with pushing new commits until you are finished. |
@jpg0 |
I was thinking a bit about this fact, and I see your point but the timer polyfills are core functionality that is also provided by NodeJS, while the Given that, I would regard the current implementation as okay. |
You're right about the scope of these things, so it's not really clear that it belongs in the same place as |
Other than the minor comment above, it looks good. Happy with the scheduler change too. As before, you'll have to get someone else such as @digitaldan to actually approve & merge as I don't have permission. |
Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
@openhab/add-ons-maintainers Can you merge? Please see the last comment of jpg0 for review. |
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.
First, thanks for the contribution! And kudos also to @jpg0 for the review and guidance. I'm not confident in this area, but nevertheless provided a few minor comments for the changed code.
The PR is tagged as a breaking change, how can we ensure that users are warned about this, and which changes will they need to do? Perhaps @kaikreuzer could also have a quick look since also involved in the previous iteration.
...jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/JSRuntimeFeatures.java
Show resolved
Hide resolved
...jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/JSRuntimeFeatures.java
Outdated
Show resolved
Hide resolved
...ng/src/main/java/org/openhab/automation/jsscripting/internal/OpenhabGraalJSScriptEngine.java
Outdated
Show resolved
Hide resolved
...ng/src/main/java/org/openhab/automation/jsscripting/internal/threading/ThreadsafeTimers.java
Outdated
Show resolved
Hide resolved
Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
@jlaur
Yes, I've added this tag as the change of the implementation of the polyfills for
By adding a breaking change note to the release notes and adding an alert to https://github.com/openhab/openhab-distro/blob/main/distributions/openhab/src/main/resources/bin/update.lst.
By adding a breaking change note to the release notes and adding an alert to https://github.com/openhab/openhab-distro/blob/main/distributions/openhab/src/main/resources/bin/update.lst.
For the rare case that a user relies on the polyfills returning an openHAB Timer, he has to update his code to e.g. cancel the timer by running
If you like to @jlaur, no problem. I'm just not sure if that's required since Kai onyl left a few short comments (the same sort that your comments are) and merged the PR, the changes itself were also guided and reviewed by jpg0. |
Thank you for the detailed explanations. Mostly I was hoping to get a second opinion about this small breaking change, which to me seems like is for the better as otherwise we would block ourselves from moving forward, and it would only get worse. Let's not rush it too much, but still aim for the upcoming milestone. If no one else will provide feedback today or tomorrow, I will merge. |
@florian-h05 - I assume this would be a PR to be merged into openhab-js after this PR is merged? |
FYI also see #13594 (comment) for what @digitaldan said about reimplementing the polyfills.
👍
Yes, exactly. I'll prepare a PR in openhab-js to complete the tasks I mentioned at the beginning of this thead. The new release also needs to go into the milestone, so I'll ping you @jlaur for merging. (Technically, it does not need to, but I'd like to have up-to-date docs and the warning.) |
Reference openhab/openhab-addons#13623. Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
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.
LGTM
Reference openhab/openhab-addons#13623. Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
@jlaur |
This pull request has been mentioned on openHAB Community. There might be relevant details there: https://community.openhab.org/t/openhab-3-4-milestone-discussion/138093/91 |
…openhab-addons#13623 Signed-off-by: Raphael Mack <ramack@raphael-mack.de>
…nhab#13623) * [jsscripting] Reimplement timers to conform standard JS * [jsscripting] Name scheduled jobs by loggerName + id * [jsscripting] Update timer identifiers * [jsscripting] Update identifiers for scheduled jobs * [jsscripting] Synchronize method that is called when the script is reloaded * [jsscripting] Cancel all scheduled jobs when the engine is closed * [jsscripting] Ensure that a timerId is never reused by a subsequent call & Use long primitive type instead of Integer * [jsscripting] Use an abstraction class to inject features into the JS runtime * [jsscripting] Make ThreadsafeTimers threadsafe for concurrent access to the class itself * [jsscripting] Move the locking for `invokeFunction` to `OpenhabGraalJSScriptEngine` Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
…nhab#13623) * [jsscripting] Reimplement timers to conform standard JS * [jsscripting] Name scheduled jobs by loggerName + id * [jsscripting] Update timer identifiers * [jsscripting] Update identifiers for scheduled jobs * [jsscripting] Synchronize method that is called when the script is reloaded * [jsscripting] Cancel all scheduled jobs when the engine is closed * [jsscripting] Ensure that a timerId is never reused by a subsequent call & Use long primitive type instead of Integer * [jsscripting] Use an abstraction class to inject features into the JS runtime * [jsscripting] Make ThreadsafeTimers threadsafe for concurrent access to the class itself * [jsscripting] Move the locking for `invokeFunction` to `OpenhabGraalJSScriptEngine` Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
…nhab#13623) * [jsscripting] Reimplement timers to conform standard JS * [jsscripting] Name scheduled jobs by loggerName + id * [jsscripting] Update timer identifiers * [jsscripting] Update identifiers for scheduled jobs * [jsscripting] Synchronize method that is called when the script is reloaded * [jsscripting] Cancel all scheduled jobs when the engine is closed * [jsscripting] Ensure that a timerId is never reused by a subsequent call & Use long primitive type instead of Integer * [jsscripting] Use an abstraction class to inject features into the JS runtime * [jsscripting] Make ThreadsafeTimers threadsafe for concurrent access to the class itself * [jsscripting] Move the locking for `invokeFunction` to `OpenhabGraalJSScriptEngine` Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Fixes #13594.
Description
This PR reimplements the polyfills for the standard JS timers (
setTimeout
andsetInterval
) in the Java layer of the binding to conform standard JS.This brings several improvements:
TimerImpl
from core is no longer required; instead the scheduler is accessed directlytimeoutId
orintervalId
when the according funtion is run. FYI: Standard JS shares the same pool of integers for both, the binding does therefore the same.timeout
orinterval
) and the id (an Integer).Breaking Changes
createTimer
,createTimerWithArgument
) are not recommended to be used anymore as they are not threadsafe. But it is still possible to access them via theScriptExecution
actions from core.ScriptExecution
action from the openhab-js library.Therefore, the polyfills for the standard JS timer methods should be the only way taken for creating timers in JS Scripting!
Testing