-
-
Notifications
You must be signed in to change notification settings - Fork 428
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
PersistenceExtensions: Support state as string for persist method #4268
PersistenceExtensions: Support state as string for persist method #4268
Conversation
Signed-off-by: Florian Hotze <[email protected]>
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
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.
Thanks! useful addition.
@florian-h05 - I'm not sure if related, but will this fix the following issue? With this rule snippet (where spotPrice is a items.Energi_Data_Service_Total_Price.persistence.persist(spotPrice.timestamp, totalPrice); I get this (with JDBC fixed by openhab/openhab-addons#16845):
Whereas the following works: var { QuantityType } = require("@runtime");
const ZonedDateTime = Java.type('java.time.ZonedDateTime');
var state = new QuantityType(totalPrice.toString());
var timestamp = ZonedDateTime.parse(spotPrice.timestamp.toString());
items.Energi_Data_Service_Total_Price.persistence.persist(timestamp, state); |
@florian-h05 - I just repeated the test with a newly built items.Energi_Data_Service_Total_Price.persistence.persist(spotPrice.timestamp, totalPrice); to: items.Energi_Data_Service_Total_Price.persistence.persist(spotPrice.timestamp, totalPrice.toString()); it works as expected. So I'm guessing this will be fixed when openhab-js is refactored to use the new overload taking state as string? |
The problem is (as the message indicates), that the persistence service persist asynchronously and attempts to access the passed in Quantity, which itself is owned by the GraalJS ScriptEngine - this then causes the thrown exception because GraalJS doesn’t allow multi-threaded access (for good reasons) and still runs in the script thread.
Exactly. As primitives aren’t passed by reference, we can fix the multi-threading issue by converting the Quantity to string, pass that to core, which then creates a new QuantityType from it and passes that one to the persistence service. openhab-js already contains the code for the to string conversion (already done for postUpdate and sendCommand), I just need to use it as well for the persist call. |
… method (#339) This fixes an issue, where the asynchronous way persistence services store the passed in state causes a IllegalStateException by GraalJS, because multithreaded access to the script's context is not allowed. See openhab/openhab-core#4268 (comment). --------- Signed-off-by: Florian Hotze <[email protected]>
This is a very useful addition for scripts & rules, e.g. for JS Scripting because in JS Scripting no native (Java) types are used (nearly everything is converted to JS tyes).