Class: OpenC3::ReactionBase
- Defined in:
- lib/openc3/microservices/reaction_microservice.rb
Overview
This should remain a thread safe implementation. 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 Attribute Summary collapse
-
#reactions ⇒ Object
readonly
Returns the value of attribute 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 actively NOT snoozed reactions.
-
#get_snoozed ⇒ Object
RETURNS an Array of actively 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 memory 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.
41 42 43 44 45 46 47 |
# File 'lib/openc3/microservices/reaction_microservice.rb', line 41 def initialize(scope:) @scope = scope @reactions_mutex = Mutex.new @reactions = Hash.new @lookup_mutex = Mutex.new @lookup = Hash.new end |
Instance Attribute Details
#reactions ⇒ Object (readonly)
Returns the value of attribute reactions.
39 40 41 |
# File 'lib/openc3/microservices/reaction_microservice.rb', line 39 def reactions @reactions 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 actively NOT snoozed reactions
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/openc3/microservices/reaction_microservice.rb', line 66 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.enabled && reaction.snoozed_until.nil? end end return ret end |
#get_snoozed ⇒ Object
RETURNS an Array of actively snoozed reactions
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/openc3/microservices/reaction_microservice.rb', line 50 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.enabled && reaction.snoozed_until end return ret end |
#remove(reaction:) ⇒ Object
Removes a reaction to the in memory database.
158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/openc3/microservices/reaction_microservice.rb', line 158 def remove(reaction:) @reactions_mutex.synchronize do @reactions.delete(reaction['name']) ReactionModel.delete(name: reaction['name'], scope: reaction['scope']) 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 memory 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 155 |
# File 'lib/openc3/microservices/reaction_microservice.rb', line 149 def update(reaction:) @reactions_mutex.synchronize do model = ReactionModel.from_json(reaction, name: reaction['name'], scope: reaction['scope']) model.update() @reactions[reaction['name']] = model.as_json(:allow_nan => true) 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 |