Class: Cosmos::TriggerBase

Inherits:
Object show all
Defined in:
lib/cosmos/microservices/trigger_group_microservice.rb

Overview

Stored in the TriggerGroupShare this should be a thread safe hash that triggers will be added, updated, and removed from.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope:) ⇒ TriggerBase

Returns a new instance of TriggerBase.



75
76
77
78
79
80
81
82
# File 'lib/cosmos/microservices/trigger_group_microservice.rb', line 75

def initialize(scope:)
  @scope = scope
  @autonomic_topic = "#{@scope}__cosmos_autonomic".freeze
  @triggers_mutex = Mutex.new
  @triggers = Hash.new
  @lookup_mutex = Mutex.new
  @lookup = Hash.new
end

Instance Attribute Details

#autonomic_topicObject (readonly)

Returns the value of attribute autonomic_topic.



73
74
75
# File 'lib/cosmos/microservices/trigger_group_microservice.rb', line 73

def autonomic_topic
  @autonomic_topic
end

Instance Method Details

#add(trigger:) ⇒ Object

add a trigger from TriggerBase



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/cosmos/microservices/trigger_group_microservice.rb', line 189

def add(trigger:)
  @triggers_mutex.synchronize do
    @triggers[trigger['name']] = Marshal.load( Marshal.dump(trigger) )
  end
  t = TriggerModel.from_json(trigger, name: trigger['name'], scope: trigger['scope'])
  @lookup_mutex.synchronize do 
    t.generate_topics.each do | topic |
      if @lookup[topic].nil?
        @lookup[topic] = { t.name => 1 }
      else
        @lookup[topic][t.name] = 1
      end
    end
  end
end

#get_triggers(topic:) ⇒ Object

Get triggers to evaluate based on the topic. IF the topic is the equal to the autonomic topic it will return only triggers with roots



87
88
89
90
91
92
93
# File 'lib/cosmos/microservices/trigger_group_microservice.rb', line 87

def get_triggers(topic:)
  if @autonomic_topic == topic
    return triggers_with_roots()
  else
    return triggers_from(topic: topic)
  end
end

#remove(trigger:) ⇒ Object

remove a trigger from TriggerBase



206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/cosmos/microservices/trigger_group_microservice.rb', line 206

def remove(trigger:)
  @triggers_mutex.synchronize do
    @triggers.delete(trigger['name'])
  end
  t = TriggerModel.from_json(trigger, name: trigger['name'], scope: trigger['scope'])
  @lookup_mutex.synchronize do 
    t.generate_topics.each do | topic |
      unless @lookup[topic].nil?
        @lookup[topic].delete(t.name)
      end
    end
  end
end

#topicsObject

get all topics group is working with



162
163
164
165
166
# File 'lib/cosmos/microservices/trigger_group_microservice.rb', line 162

def topics
  @lookup_mutex.synchronize do
    return Marshal.load( Marshal.dump(@lookup.keys()) )
  end
end

#triggersObject

returns a Hash of ALL active Trigger objects



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/cosmos/microservices/trigger_group_microservice.rb', line 116

def triggers
  val = nil
  @triggers_mutex.synchronize do
    val = Marshal.load( Marshal.dump(@triggers) )
  end
  ret = Hash.new
  val.each do | name, data |
    trigger = TriggerModel.from_json(data, name: data['name'], scope: data['scope'])
    ret[name] = trigger if trigger.active
  end
  return ret
end

#triggers_from(topic:) ⇒ Object

returns an Array of active Trigger objects that use a topic



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/cosmos/microservices/trigger_group_microservice.rb', line 144

def triggers_from(topic:)
  val = nil
  @lookup_mutex.synchronize do
    val = Marshal.load( Marshal.dump(@lookup[topic]) )
  end
  return [] if val.nil?
  ret = []
  @triggers_mutex.synchronize do
    val.each do | trigger_name, _v |
      data = Marshal.load( Marshal.dump(@triggers[trigger_name]) )
      trigger = TriggerModel.from_json(data, name: data['name'], scope: data['scope'])
      ret << trigger if trigger.active
    end
  end
  return ret
end

#triggers_with_rootsObject

returns an Array of active Trigger objects that have roots to other triggers



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/cosmos/microservices/trigger_group_microservice.rb', line 130

def triggers_with_roots
  val = nil
  @triggers_mutex.synchronize do
    val = Marshal.load( Marshal.dump(@triggers) )
  end
  ret = []
  val.each do | _name, data |
    trigger = TriggerModel.from_json(data, name: data['name'], scope: data['scope'])
    ret << trigger if trigger.active && ! trigger.roots.empty?
  end
  return ret
end

#update(triggers:) ⇒ Object

database update of all triggers in the group



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/cosmos/microservices/trigger_group_microservice.rb', line 169

def update(triggers:)
  @triggers_mutex.synchronize do
    @triggers = Marshal.load( Marshal.dump(triggers) )
  end
  @lookup_mutex.synchronize do
    @lookup = {@autonomic_topic => {}}
    triggers.each do | _name, data |
      trigger = TriggerModel.from_json(data, name: data['name'], scope: data['scope'])
      trigger.generate_topics.each do | topic |
        if @lookup[topic].nil?
          @lookup[topic] = { trigger.name => 1 }
        else
          @lookup[topic][trigger.name] = 1
        end
      end
    end
  end
end

#update_state(name:, value:) ⇒ Object

update trigger state after evaluated -1 (the value is considered an error used to disable the trigger)

0 (the value is considered as a false value)
1 (the value is considered as a true value)


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/cosmos/microservices/trigger_group_microservice.rb', line 99

def update_state(name:, value:)
  @triggers_mutex.synchronize do
    data = @triggers[name]
    return unless data
    trigger = TriggerModel.from_json(data, name: data['name'], scope: data['scope'])
    if value == -1 && trigger.active
      trigger.deactivate()
    elsif value == 1 && trigger.state == false
      trigger.enable()
    elsif value == 0 && trigger.state == true
      trigger.disable()
    end
    @triggers[name] = trigger.as_json
  end
end