- Platform information:
- Hardware: Raspberry PI 4B
- OS: OpenHabianOS 4.1.1
- Java Runtime Environment: OpenJDK 64 bit 17.0.13
- openHAB version: 4.1.1
- Issue of the topic: Rules calling the same Jruby script from an inline Jruby script,
shared_cache
becomes undefined - Descriptions:
The jruby addon is running in “ThreadSafe” context, while other settings are left at default.
The inline script that is put in the rule’s “action”, can use the functionshared_cache
for a pseudo mutex variable and when calling the external scriptnotification_built_in
, it will also useshared_cache
to rate limit via a quota system also saved in theshared_cache
.
But in my testing scenario, the first rule that runs, can complete it successfully. But the other rules that follow through will somehow lose the ability to call the function only when running that external script to fetch the shared caches for the quota system.
- Please post configurations (if applicable):
- Rule inline script:
def processNotification()
logger.info('running rule 4')
shared_cache[:mutexQueue] ||= []
mutexQueue = shared_cache[:mutexQueue]
count = 1
while shared_cache[:mutexQueue].length > 0 do
count += 1
if count > 8
logger.info('rule 4 exceeded sleep')
return
end
sleep 4
mutexQueue = shared_cache[:mutexQueue]
end
shared_cache[:mutexQueue] << {'date' => Time.now.iso8601}
data = ["+2507"]
mode = "whatsapp"
logger.info(data)
rule_manager = OpenHAB::OSGi.service("org.openhab.core.automation.RuleManager");
result = rule_manager.run_now('notification_built_in', true, {'data' => data, 'mode' => mode});
logger.info('finish running rule 4')
logger.info(result)
shared_cache[:mutexQueue].shift
return
end
processNotification()
- Script that they are calling:
require 'json'
require 'time'
def notification_quota_check(target, user_id)
max_quota = 5
time_limit = 20
shared_cache[:notification_quota] ||= []
curr_time = Time.now
logger.info( "-=-=-=-=-= Start of notification Quota -=-=-=-=-")
logger.info( "notificationQuota: #{notification_quota.inspect}")
if shared_cache[:notification_quota].length >= max_quota
logger.info( "+++ notificationQuota is full +++")
allow = false
old_quota = shared_cache[:notification_quota].dup
old_quota.each do |entry|
logger.info( "full Data: #{entry}")
oldest_time_in_quota = Time.parse(entry['date'])
minutes_from_curr = (curr_time - oldest_time_in_quota) / 60
logger.info( "how long ago was it: #{(curr_time - minutes_from_curr * 60).iso8601}")
if minutes_from_curr > time_limit
allow = true
logger.info( "already more than #{time_limit} minutes, we can remove it from Quota")
shared_cache[:notification_quota].shift
else
break
end
end
if allow
shared_cache[:notification_quota] << { 'date' => curr_time.iso8601, 'number' => target, 'userId' => user_id }
end
return false
else
logger.info( "--- notificationQuota is not full ---")
shared_cache[:notification_quota] << { 'date' => curr_time.iso8601, 'number' => target, 'userId' => user_id }
return true
end
end
def trigger_notification(data, mode)
require 'net/http'
require 'uri'
logger.info( "trigger notification for #{data}")
uri = URI.parse("https://www.uchat.com.au/api/iwh/")
case mode
when "whatsapp", "application"
request = Net::HTTP::Post.new(uri)
request.content_type = "application/json"
request.body = JSON.dump({ data: data })
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
http.request(request)
end
return response
when "email"
response = Net::HTTP.get_response(uri)
return response
else
return nil
end
end
data = { message: "Your notification message" }
mode = "whatsapp"
if notification_quota_check(data, 12)
logger.info( "Notification Sending")
trigger_notification(data, mode)
end
- Logs generated (theres 7 rules that run the same inline script, triggered by the same thing):
2024-12-17 18:10:40.023 [INFO ] [c4c321db-bfac-4a13-a122-f45c1acb5c0a] - running rule 1
2024-12-17 18:10:40.023 [INFO ] [ion.jrubyscripting.script.2a24776804] - running rule 6
2024-12-17 18:10:40.037 [INFO ] [ion.jrubyscripting.script.2a24776804] - ["+22222222222"]
2024-12-17 18:10:40.038 [INFO ] [ion.jrubyscripting.script.5b8badcc16] - running rule 4
2024-12-17 18:10:40.049 [INFO ] [b47692e2-743b-4f7e-9be4-c95a9de596ab] - running rule 7
2024-12-17 18:10:40.053 [INFO ] [ion.jrubyscripting.script.d08da3248d] - running rule 3
2024-12-17 18:10:40.056 [INFO ] [ion.jrubyscripting.script.815fbdbaf2] - running rule 2
2024-12-17 18:10:40.058 [INFO ] [ion.jrubyscripting.script.088201d9c0] - running rule 5
2024-12-17 18:10:40.182 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'notification_built_in' failed: Error during evaluation of Ruby in <script> at line 7: (NameError) undefined local variable or method `shared_cache' for main:Object
2024-12-17 18:10:40.186 [INFO ] [ion.jrubyscripting.script.2a24776804] - running rule 6
2024-12-17 18:10:40.191 [INFO ] [ion.jrubyscripting.script.2a24776804] - {data=["+22222222222"], mode=whatsapp}
==> /var/log/openhab/events.log <==
2024-12-17 18:10:39.942 [INFO ] [openhab.event.ItemCommandEvent ] - Item 'moorgen_0821ffffffff_01ff_switch' received command ON
2024-12-17 18:10:39.947 [INFO ] [openhab.event.ItemCommandEvent ] - Item 'yo835_switch_gang2_e4e112a09fda' received command ON
2024-12-17 18:10:39.960 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'moorgen_0821ffffffff_01ff_switch' predicted to become ON
2024-12-17 18:10:39.986 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'moorgen_0821ffffffff_01ff_switch' changed from OFF to ON
2024-12-17 18:10:40.011 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'yo835_switch_gang2_e4e112a09fda' predicted to become ON
==> /var/log/openhab/openhab.log <==
2024-12-17 18:10:44.044 [INFO ] [c4c321db-bfac-4a13-a122-f45c1acb5c0a] - ["random"]
2024-12-17 18:10:44.164 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'notification_built_in' failed: Error during evaluation of Ruby in <script> at line 7: (NameError) undefined local variable or method `shared_cache' for main:Object
2024-12-17 18:10:44.169 [INFO ] [c4c321db-bfac-4a13-a122-f45c1acb5c0a] - finish running rule 1
2024-12-17 18:10:44.172 [INFO ] [c4c321db-bfac-4a13-a122-f45c1acb5c0a] - {data=["random"], mode=whatsapp}
2024-12-17 18:10:48.046 [INFO ] [ion.jrubyscripting.script.5b8badcc16] - ["+2507"]
2024-12-17 18:10:48.067 [INFO ] [ripting.script.notification_built_in] - -=-=-=-=-= Start of notification Quota -=-=-=-=-
2024-12-17 18:10:48.083 [INFO ] [ripting.script.notification_built_in] - notificationQuota: #<OpenHAB::Core::Items::StringItem notification_quota "notification_quota" state=NULL category="">
2024-12-17 18:10:48.087 [INFO ] [ripting.script.notification_built_in] - --- notificationQuota is not full ---
2024-12-17 18:10:48.092 [INFO ] [ripting.script.notification_built_in] - Notification Sending
2024-12-17 18:10:48.096 [INFO ] [ripting.script.notification_built_in] - trigger notification for {:message=>"Your notification message"}
2024-12-17 18:10:48.673 [INFO ] [ion.jrubyscripting.script.5b8badcc16] - finish running rule 4
2024-12-17 18:10:48.676 [INFO ] [ion.jrubyscripting.script.5b8badcc16] - {mode=whatsapp, script.result=#<Net::HTTPNotFound:0x49863e80>, data=["+2507"]}
2024-12-17 18:10:52.058 [INFO ] [b47692e2-743b-4f7e-9be4-c95a9de596ab] - ["+44444444"]
2024-12-17 18:10:52.195 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'notification_built_in' failed: Error during evaluation of Ruby in <script> at line 7: (NameError) undefined local variable or method `shared_cache' for main:Object
2024-12-17 18:10:52.200 [INFO ] [b47692e2-743b-4f7e-9be4-c95a9de596ab] - finish running rule 7
2024-12-17 18:10:52.203 [INFO ] [b47692e2-743b-4f7e-9be4-c95a9de596ab] - {data=["+44444444"], mode=whatsapp}
2024-12-17 18:10:56.063 [INFO ] [ion.jrubyscripting.script.815fbdbaf2] - ["+9999"]
2024-12-17 18:10:56.171 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'notification_built_in' failed: Error during evaluation of Ruby in <script> at line 7: (NameError) undefined local variable or method `shared_cache' for main:Object
2024-12-17 18:10:56.178 [INFO ] [ion.jrubyscripting.script.815fbdbaf2] - finish running rule 2
2024-12-17 18:10:56.182 [INFO ] [ion.jrubyscripting.script.815fbdbaf2] - {data=["+9999"], mode=whatsapp}
2024-12-17 18:11:00.065 [INFO ] [ion.jrubyscripting.script.088201d9c0] - ["+111111111"]
2024-12-17 18:11:01.432 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'notification_built_in' failed: Error during evaluation of Ruby in <script> at line 7: (NameError) undefined local variable or method `shared_cache' for main:Object
2024-12-17 18:11:01.439 [INFO ] [ion.jrubyscripting.script.088201d9c0] - finish running rule 5
2024-12-17 18:11:01.443 [INFO ] [ion.jrubyscripting.script.088201d9c0] - {data=["+111111111"], mode=whatsapp}
2024-12-17 18:11:04.067 [INFO ] [ion.jrubyscripting.script.d08da3248d] - ["+6809"]
2024-12-17 18:11:05.803 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'notification_built_in' failed: Error during evaluation of Ruby in <script> at line 7: (NameError) undefined local variable or method `shared_cache' for main:Object
2024-12-17 18:11:05.811 [INFO ] [ion.jrubyscripting.script.d08da3248d] - finish running rule 3
2024-12-17 18:11:05.816 [INFO ] [ion.jrubyscripting.script.d08da3248d] - {data=["+333"], mode=whatsapp}