Class: OpenC3::ReactionBase
- Defined in:
- lib/openc3/microservices/reaction_microservice.rb
Overview
This should remain a thread safe implamentation. This is the in memory cache that should mirror the database. This will update two hash variables and will track triggers to lookup what triggers link to what reactions.
Instance Method Summary collapse
-
#add(reaction:) ⇒ Object
Add a reaction to the in memory database.
-
#get_reactions(trigger_name:) ⇒ Object
RETURNS an Array of active and not snoozed reactions.
-
#get_snoozed ⇒ Object
RETURNS an Array of active and not snoozed reactions.
-
#initialize(scope:) ⇒ ReactionBase
constructor
A new instance of ReactionBase.
-
#remove(reaction:) ⇒ Object
Removes a reaction to the in memory database.
-
#setup(reactions:) ⇒ Object
Update the memeory database with a HASH of reactions from the external database.
-
#sleep(name:) ⇒ Object
Pulls the latest reaction name from the in memory database to see if the reaction should be put to sleep.
-
#update(reaction:) ⇒ Object
Updates a reaction to the in memory database.
-
#wake(name:) ⇒ Object
Pulls the latest reaction name from the in memory database to see if the reaction should be awaken.
Constructor Details
#initialize(scope:) ⇒ ReactionBase
Returns a new instance of ReactionBase.
40 41 42 43 44 45 46 |
# File 'lib/openc3/microservices/reaction_microservice.rb', line 40 def initialize(scope:) @scope = scope @reactions_mutex = Mutex.new @reactions = Hash.new @lookup_mutex = Mutex.new @lookup = Hash.new end |
Instance Method Details
#add(reaction:) ⇒ Object
Add a reaction to the in memory database
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/openc3/microservices/reaction_microservice.rb', line 130 def add(reaction:) reaction_name = reaction['name'] @reactions_mutex.synchronize do @reactions[reaction_name] = reaction end reaction['triggers'].each do | trigger | trigger_name = trigger['name'] @lookup_mutex.synchronize do if @lookup[trigger_name].nil? @lookup[trigger_name] = [reaction_name] else @lookup[trigger_name] << reaction_name end end end end |
#get_reactions(trigger_name:) ⇒ Object
RETURNS an Array of active and not snoozed reactions
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/openc3/microservices/reaction_microservice.rb', line 65 def get_reactions(trigger_name:) array_value = nil @lookup_mutex.synchronize do array_value = Marshal.load( Marshal.dump(@lookup[trigger_name]) ) end ret = Array.new return ret unless array_value array_value.each do | name | @reactions_mutex.synchronize do data = Marshal.load( Marshal.dump(@reactions[name]) ) reaction = ReactionModel.from_json(data, name: data['name'], scope: data['scope']) ret << reaction if reaction.active && reaction.snoozed_until.nil? end end return ret end |
#get_snoozed ⇒ Object
RETURNS an Array of active and not snoozed reactions
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/openc3/microservices/reaction_microservice.rb', line 49 def get_snoozed data = nil @reactions_mutex.synchronize do data = Marshal.load( Marshal.dump(@reactions) ) end ret = Array.new return ret unless data data.each do | _name, r_hash | data = Marshal.load( Marshal.dump(r_hash) ) reaction = ReactionModel.from_json(data, name: data['name'], scope: data['scope']) ret << reaction if reaction.active && reaction.snoozed_until end return ret end |
#remove(reaction:) ⇒ Object
Removes a reaction to the in memory database.
157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/openc3/microservices/reaction_microservice.rb', line 157 def remove(reaction:) reaction_name = reaction['name'] @reactions_mutex.synchronize do @reactions.delete(reaction_name) end reaction['triggers'].each do | trigger | trigger_name = trigger['name'] @lookup_mutex.synchronize do @lookup[trigger_name].delete(reaction_name) end end end |
#setup(reactions:) ⇒ Object
Update the memeory database with a HASH of reactions from the external database
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/openc3/microservices/reaction_microservice.rb', line 84 def setup(reactions:) @reactions_mutex.synchronize do @reactions = Marshal.load( Marshal.dump(reactions) ) end @lookup_mutex.synchronize do @lookup = Hash.new reactions.each do | reaction_name, reaction | reaction['triggers'].each do | trigger | trigger_name = trigger['name'] if @lookup[trigger_name].nil? @lookup[trigger_name] = [reaction_name] else @lookup[trigger_name] << reaction_name end end end end end |
#sleep(name:) ⇒ Object
Pulls the latest reaction name from the in memory database to see if the reaction should be put to sleep.
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/openc3/microservices/reaction_microservice.rb', line 105 def sleep(name:) @reactions_mutex.synchronize do data = Marshal.load( Marshal.dump(@reactions[name]) ) return unless data reaction = ReactionModel.from_json(data, name: data['name'], scope: data['scope']) if reaction.snoozed_until.nil? || Time.now.to_i >= reaction.snoozed_until reaction.sleep() end @reactions[name] = reaction.as_json(:allow_nan => true) end end |
#update(reaction:) ⇒ Object
Updates a reaction to the in memory database. This current does not update the lookup Hash for the triggers.
149 150 151 152 153 154 |
# File 'lib/openc3/microservices/reaction_microservice.rb', line 149 def update(reaction:) reaction_name = reaction['name'] @reactions_mutex.synchronize do @reactions[reaction_name] = reaction end end |
#wake(name:) ⇒ Object
Pulls the latest reaction name from the in memory database to see if the reaction should be awaken.
119 120 121 122 123 124 125 126 127 |
# File 'lib/openc3/microservices/reaction_microservice.rb', line 119 def wake(name:) @reactions_mutex.synchronize do data = Marshal.load( Marshal.dump(@reactions[name]) ) return unless data reaction = ReactionModel.from_json(data, name: data['name'], scope: data['scope']) reaction.awaken() @reactions[name] = reaction.as_json(:allow_nan => true) end end |