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

[jsscripting] Reimplement timer creation method of ScriptExecution #13695

Merged
merged 14 commits into from
Nov 20, 2022

Conversation

florian-h05
Copy link
Contributor

@florian-h05 florian-h05 commented Nov 11, 2022

Description

This PR reimplements the openHAB Core API for creating timers to make this also threadsafe.

As discussed on the forum https://community.openhab.org/t/js-scripting-why-are-we-deprecated-createtimer/140748, the setTimeout and setInterval methods do not provide the functionality that advanced users need, and to avoid that those users and users coming from Rules DSL leave JS Scripting because of this missing API, we have to provide thread-safe access to it.

Furthermore, JSScriptServiceUtil was introduced to provide easy access from to script services provided by core. This replaces the usage of the JSScriptServiceUtil from the Rules DSL parts of core and therefore reduces dependency on a part that might become an addon in the future.

openhab-js is also upgraded to 2.1.1.

Testing

Use the following file-based script to test:

const { time} = require('openhab');

// Note that all timers are scheduled to nearly the same time, so without the synchronization mechanism in place, this would lead to a multi-thread exception
ThreadsafeTimers.createTimer(time.toZDT(), () => {
    console.info("Hello world from createTimer without identifier")
});

ThreadsafeTimers.createTimer('createTimer', time.toZDT(), () => {
    console.info("Hello world from createTimer with identifier")
});

…ead of in an extra methods

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
…ript services

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
… for thread-safety

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
@florian-h05 florian-h05 added enhancement An enhancement or new feature for an existing add-on awaiting other PR Depends on another PR labels Nov 11, 2022
@florian-h05
Copy link
Contributor Author

@rkoshak
PR is now open.

@jlaur @jpg0 Could you please give your feedback on this?

@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/js-scripting-why-are-we-deprecated-createtimer/140748/18

@jlaur jlaur requested review from a team and digitaldan and removed request for jlaur November 11, 2022 15:27
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>
@florian-h05
Copy link
Contributor Author

florian-h05 commented Nov 12, 2022

@jpg0 I addressed your review, could you please re-review?

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

Copy link
Contributor

@jpg0 jpg0 left a comment

Choose a reason for hiding this comment

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

Only issue I have is with the use of OSGi. If it's a common pattern in openHAB I'm not concerned about it though.

…th an injection mechanism

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
@florian-h05 florian-h05 force-pushed the jsscripting-reimplement-createtimer branch from 21c04be to fb0ca2d Compare November 12, 2022 19:26
…raalJSScriptEngineFactory`

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

jlaur commented Nov 12, 2022

@kaikreuzer - it seems that @jpg0 is missing as member of the addons contributors group, could you add him?

J-N-K and others added 2 commits November 12, 2022 22:13
* minor improvements

* some more

Signed-off-by: Jan N. Klug <github@klug.nrw>
…y of `JSRuntimeFeatures`

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

FYI: DCO is failing because of @J-N-K GitHub username is not his real name, which he used to signoff.

@florian-h05 florian-h05 removed the awaiting other PR Depends on another PR label Nov 19, 2022
@florian-h05 florian-h05 changed the title [jsscripting] Reimplement timer creation methods of ScriptExecution [WIP] [jsscripting] Reimplement timer creation methods of ScriptExecution Nov 19, 2022
@J-N-K J-N-K closed this Nov 20, 2022
@J-N-K J-N-K reopened this Nov 20, 2022
@florian-h05 florian-h05 force-pushed the jsscripting-reimplement-createtimer branch from 78a41e0 to b447b57 Compare November 20, 2022 10:34
@florian-h05 florian-h05 added the awaiting other PR Depends on another PR label Nov 20, 2022
@florian-h05 florian-h05 changed the title [WIP] [jsscripting] Reimplement timer creation methods of ScriptExecution [jsscripting] Reimplement timer creation methods of ScriptExecution Nov 20, 2022
@jlaur jlaur added rebuild Triggers Jenkins PR build and removed awaiting other PR Depends on another PR rebuild Triggers Jenkins PR build labels Nov 20, 2022
@florian-h05
Copy link
Contributor Author

@jlaur This is ready for your review and merging now.

FYI: digitaldan and jpg0 approved at some point, after that I’ve changed the implementation of JSScriptServiceUtil (as jpg0 and J-N-K asked me to). J-N-K gave his approval in #13695 (comment).
After all that reviews, I only upgraded the included openhab-js version and removed some unused code.

Copy link
Contributor

@jlaur jlaur left a comment

Choose a reason for hiding this comment

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

Thanks! Only a small simplification/optimization suggested.

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

florian-h05 commented Nov 20, 2022

@jlaur I addressed your review, let's merge.

@florian-h05 florian-h05 changed the title [jsscripting] Reimplement timer creation methods of ScriptExecution [jsscripting] Reimplement timer creation method of ScriptExecution Nov 20, 2022
Copy link
Contributor

@jlaur jlaur left a comment

Choose a reason for hiding this comment

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

LGTM

@florian-h05
Copy link
Contributor Author

@jlaur All checks have passed.

@jlaur jlaur merged commit bfff07b into openhab:main Nov 20, 2022
@jlaur jlaur added this to the 3.4 milestone Nov 20, 2022
@florian-h05 florian-h05 deleted the jsscripting-reimplement-createtimer branch November 20, 2022 21:12
@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/js-scripting-why-are-we-deprecated-createtimer/140748/22

andrasU pushed a commit to andrasU/openhab-addons that referenced this pull request Dec 24, 2022
…penhab#13695)

* [jsscripting] Refactor ThreadsafeTimers to create futures inline instead of in an extra methods
* [jsscripting] Introduce utility class for providing easy access to script services
* [jsscripting] Reimplement timer creation methods from ScriptExecution for thread-safety
* [jsscripting] Add missing JavaDoc for reimplement timer creation methods
* [jsscripting] Remove the future from the map when setTimeout expires
* [jsscripting] Rename `GraalJSScriptServiceUtil` to `JSScriptServiceUtil`
* [jsscripting] Remove the `createTimerWithArgument` method
* [jsscripting] Replace the OSGi workaround of `JSScriptServiceUtil` with an injection mechanism
* [jsscripting] Use constructor to inject `JSScriptServiceUtil` into `GraalJSScriptEngineFactory`
* [jsscripting] Minor improvements by @J-N-K (#1)
* [jsscripting] Minor changes related to last commit to keep flexibility of `JSRuntimeFeatures`
* [jsscripting] Upgrade openhab-js to v2.1.1
* [jsscripting] Remove unused code

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Co-authored-by: Jan N. Klug <github@klug.nrw>
Signed-off-by: Andras Uhrin <andras.uhrin@gmail.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/typeerror-timer-reschedule-is-not-a-function/141518/10

borazslo pushed a commit to borazslo/openhab-mideaac-addon that referenced this pull request Jan 8, 2023
…penhab#13695)

* [jsscripting] Refactor ThreadsafeTimers to create futures inline instead of in an extra methods
* [jsscripting] Introduce utility class for providing easy access to script services
* [jsscripting] Reimplement timer creation methods from ScriptExecution for thread-safety
* [jsscripting] Add missing JavaDoc for reimplement timer creation methods
* [jsscripting] Remove the future from the map when setTimeout expires
* [jsscripting] Rename `GraalJSScriptServiceUtil` to `JSScriptServiceUtil`
* [jsscripting] Remove the `createTimerWithArgument` method
* [jsscripting] Replace the OSGi workaround of `JSScriptServiceUtil` with an injection mechanism
* [jsscripting] Use constructor to inject `JSScriptServiceUtil` into `GraalJSScriptEngineFactory`
* [jsscripting] Minor improvements by @J-N-K (openhab#1)
* [jsscripting] Minor changes related to last commit to keep flexibility of `JSRuntimeFeatures`
* [jsscripting] Upgrade openhab-js to v2.1.1
* [jsscripting] Remove unused code

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Co-authored-by: Jan N. Klug <github@klug.nrw>
psmedley pushed a commit to psmedley/openhab-addons that referenced this pull request Feb 23, 2023
…penhab#13695)

* [jsscripting] Refactor ThreadsafeTimers to create futures inline instead of in an extra methods
* [jsscripting] Introduce utility class for providing easy access to script services
* [jsscripting] Reimplement timer creation methods from ScriptExecution for thread-safety
* [jsscripting] Add missing JavaDoc for reimplement timer creation methods
* [jsscripting] Remove the future from the map when setTimeout expires
* [jsscripting] Rename `GraalJSScriptServiceUtil` to `JSScriptServiceUtil`
* [jsscripting] Remove the `createTimerWithArgument` method
* [jsscripting] Replace the OSGi workaround of `JSScriptServiceUtil` with an injection mechanism
* [jsscripting] Use constructor to inject `JSScriptServiceUtil` into `GraalJSScriptEngineFactory`
* [jsscripting] Minor improvements by @J-N-K (#1)
* [jsscripting] Minor changes related to last commit to keep flexibility of `JSRuntimeFeatures`
* [jsscripting] Upgrade openhab-js to v2.1.1
* [jsscripting] Remove unused code

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Co-authored-by: Jan N. Klug <github@klug.nrw>
nemerdaud pushed a commit to nemerdaud/openhab-addons that referenced this pull request Feb 28, 2023
…penhab#13695)

* [jsscripting] Refactor ThreadsafeTimers to create futures inline instead of in an extra methods
* [jsscripting] Introduce utility class for providing easy access to script services
* [jsscripting] Reimplement timer creation methods from ScriptExecution for thread-safety
* [jsscripting] Add missing JavaDoc for reimplement timer creation methods
* [jsscripting] Remove the future from the map when setTimeout expires
* [jsscripting] Rename `GraalJSScriptServiceUtil` to `JSScriptServiceUtil`
* [jsscripting] Remove the `createTimerWithArgument` method
* [jsscripting] Replace the OSGi workaround of `JSScriptServiceUtil` with an injection mechanism
* [jsscripting] Use constructor to inject `JSScriptServiceUtil` into `GraalJSScriptEngineFactory`
* [jsscripting] Minor improvements by @J-N-K (#1)
* [jsscripting] Minor changes related to last commit to keep flexibility of `JSRuntimeFeatures`
* [jsscripting] Upgrade openhab-js to v2.1.1
* [jsscripting] Remove unused code

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Co-authored-by: Jan N. Klug <github@klug.nrw>
andrasU pushed a commit to andrasU/openhab-addons that referenced this pull request Jan 6, 2024
…penhab#13695)

* [jsscripting] Refactor ThreadsafeTimers to create futures inline instead of in an extra methods
* [jsscripting] Introduce utility class for providing easy access to script services
* [jsscripting] Reimplement timer creation methods from ScriptExecution for thread-safety
* [jsscripting] Add missing JavaDoc for reimplement timer creation methods
* [jsscripting] Remove the future from the map when setTimeout expires
* [jsscripting] Rename `GraalJSScriptServiceUtil` to `JSScriptServiceUtil`
* [jsscripting] Remove the `createTimerWithArgument` method
* [jsscripting] Replace the OSGi workaround of `JSScriptServiceUtil` with an injection mechanism
* [jsscripting] Use constructor to inject `JSScriptServiceUtil` into `GraalJSScriptEngineFactory`
* [jsscripting] Minor improvements by @J-N-K (#1)
* [jsscripting] Minor changes related to last commit to keep flexibility of `JSRuntimeFeatures`
* [jsscripting] Upgrade openhab-js to v2.1.1
* [jsscripting] Remove unused code

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Co-authored-by: Jan N. Klug <github@klug.nrw>
Signed-off-by: Andras Uhrin <andras.uhrin@gmail.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 for an existing add-on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants