Class: OpenC3::ReactionBase

Inherits:
Object show all
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

Instance Method Summary collapse

Constructor Details

#initialize(scope:) ⇒ ReactionBase

Returns a new instance of ReactionBase.



39
40
41
42
43
44
45
# File 'lib/openc3/microservices/reaction_microservice.rb', line 39

def initialize(scope:)
  @scope = scope
  @reactions_mutex = Mutex.new
  @reactions = Hash.new
  @lookup_mutex = Mutex.new
  @lookup = Hash.new
end

Instance Attribute Details

#reactionsObject (readonly)

Returns the value of attribute reactions.



37
38
39
# File 'lib/openc3/microservices/reaction_microservice.rb', line 37

def reactions
  @reactions
end

Instance Method Details

#add(reaction:) ⇒ Object

Add a reaction to the in memory database



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/openc3/microservices/reaction_microservice.rb', line 128

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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/openc3/microservices/reaction_microservice.rb', line 64

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_snoozedObject

RETURNS an Array of actively snoozed reactions



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/openc3/microservices/reaction_microservice.rb', line 48

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.



156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/openc3/microservices/reaction_microservice.rb', line 156

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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/openc3/microservices/reaction_microservice.rb', line 82

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.



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/openc3/microservices/reaction_microservice.rb', line 103

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.



147
148
149
150
151
152
153
# File 'lib/openc3/microservices/reaction_microservice.rb', line 147

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.



117
118
119
120
121
122
123
124
125
# File 'lib/openc3/microservices/reaction_microservice.rb', line 117

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