Class: Cosmos::ReactionBase
- Defined in:
- lib/cosmos/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.
37 38 39 40 41 42 43 |
# File 'lib/cosmos/microservices/reaction_microservice.rb', line 37 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
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/cosmos/microservices/reaction_microservice.rb', line 127 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
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/cosmos/microservices/reaction_microservice.rb', line 62 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
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/cosmos/microservices/reaction_microservice.rb', line 46 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.
154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/cosmos/microservices/reaction_microservice.rb', line 154 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
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/cosmos/microservices/reaction_microservice.rb', line 81 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.
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/cosmos/microservices/reaction_microservice.rb', line 102 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 end end |
#update(reaction:) ⇒ Object
Updates a reaction to the in memory database. This current does not update the lookup Hash for the triggers.
146 147 148 149 150 151 |
# File 'lib/cosmos/microservices/reaction_microservice.rb', line 146 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.
116 117 118 119 120 121 122 123 124 |
# File 'lib/cosmos/microservices/reaction_microservice.rb', line 116 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 end end |