Class: Orchestrator::Status
- Inherits:
-
Object
- Object
- Orchestrator::Status
- Defined in:
- lib/orchestrator/status.rb
Instance Attribute Summary collapse
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
Instance Method Summary collapse
-
#exec_unsubscribe(sub) ⇒ Object
- NOTE
-
Only to be called from subscription thread.
-
#initialize(thread) ⇒ Status
constructor
A new instance of Status.
-
#move(mod_id, to_thread) ⇒ Object
Used to maintain subscriptions where module is moved to another thread or even another server.
-
#reloaded_system(sys_id, sys) ⇒ Object
The System class contacts each of the threads to let them know of an update.
-
#subscribe(opt) ⇒ Object
Subscribes to updates from a system module Modules do not have to exist and updates will be triggered as soon as they are.
- #transfer(mod_id, subs) ⇒ Object
-
#unsubscribe(sub) ⇒ Object
Removes subscription callback from the lookup.
-
#update(mod_id, status, value) ⇒ Object
Triggers an update to be sent to listening callbacks.
Constructor Details
#initialize(thread) ⇒ Status
Returns a new instance of Status.
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/orchestrator/status.rb', line 34 def initialize(thread) @thread = thread @controller = ::Orchestrator::Control.instance @find_subscription = method(:find_subscription) # {:mod_id => {status => Subscriptions}} @subscriptions = {} # {:system_id => Subscriptions} @systems = {} end |
Instance Attribute Details
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
47 48 49 |
# File 'lib/orchestrator/status.rb', line 47 def thread @thread end |
Instance Method Details
#exec_unsubscribe(sub) ⇒ Object
- NOTE
-
Only to be called from subscription thread
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/orchestrator/status.rb', line 177 def exec_unsubscribe(sub) # Update the system lookup if a system was specified if sub.sys_id subscriptions = @systems[sub.sys_id] if subscriptions subscriptions.delete(sub) if subscriptions.empty? @systems.delete(sub.sys_id) end end end # Update the module lookup statuses = @subscriptions[sub.mod_id] if statuses subscriptions = statuses[sub.status] if subscriptions subscriptions.delete(sub) if subscriptions.empty? statuses.delete(sub.status) if statuses.empty? @subscriptions.delete(sub.mod_id) end end end end end |
#move(mod_id, to_thread) ⇒ Object
Used to maintain subscriptions where module is moved to another thread or even another server.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/orchestrator/status.rb', line 102 def move(mod_id, to_thread) return if to_thread == self @thread.schedule do subs = @subscriptions.delete(mod_id) if subs # Remove the system references subs.each do |sub| @systems[sub.sys_id].delete(sub) if sub.sys_id end # Transfer the subscriptions to_thread.transfer(mod_id, subs) end end end |
#reloaded_system(sys_id, sys) ⇒ Object
The System class contacts each of the threads to let them know of an update
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/orchestrator/status.rb', line 133 def reloaded_system(sys_id, sys) subscriptions = @systems[sys_id] if subscriptions subscriptions.each do |sub| old_id = sub.mod_id # re-index the subscription mod = sys.get(sub.mod_name, sub.index - 1) sub.mod_id = mod ? mod.settings.id.to_sym : nil # Check for changes (order, removal, replacement) if old_id != sub.mod_id @subscriptions[old_id][sub.status].delete(sub) # Update to the new module if sub.mod_id @subscriptions[sub.mod_id] ||= {} @subscriptions[sub.mod_id][sub.status] ||= Set.new @subscriptions[sub.mod_id][sub.status].add(sub) # Check for existing status to send to subscriber value = mod.status[sub.status] sub.notify(value) unless value.nil? end # Transfer the subscription if on a different thread if mod.thread != @thread move(sub.mod_id.to_sym, mod.thread) end # Perform any required cleanup if @subscriptions[old_id][sub.status].empty? @subscriptions[old_id].delete(sub.status) if @subscriptions[old_id].empty? @subscriptions.delete(old_id) end end end end end end |
#subscribe(opt) ⇒ Object
Subscribes to updates from a system module Modules do not have to exist and updates will be triggered as soon as they are
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/orchestrator/status.rb', line 52 def subscribe(opt) # sys_name, mod_name, index, status, callback, on_thread if opt[:sys_name] && !opt[:sys_id] @thread.work(proc { id = ::Orchestrator::ControlSystem.bucket.get("sysname-#{sys_name}") opt[:sys_id] = id # Grabbing system here as thread-safe and has the potential to block ::Orchestrator::System.get(id) }).then(proc { |sys| mod = sys.get(opt[:mod_name], opt[:index] - 1) if mod opt[:mod_id] = mod.settings.id.to_sym opt[:mod] = mod end do_subscribe(opt) }) else do_subscribe(opt) end end |
#transfer(mod_id, subs) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/orchestrator/status.rb', line 119 def transfer(mod_id, subs) @thread.schedule do @subscriptions[mod_id] = subs subs.each do |sub| if sub.sys_id @systems[sub.sys_id] ||= Set.new @systems[sub.sys_id] << sub end end end end |
#unsubscribe(sub) ⇒ Object
Removes subscription callback from the lookup
75 76 77 78 79 80 81 |
# File 'lib/orchestrator/status.rb', line 75 def unsubscribe(sub) if sub.is_a? ::Libuv::Q::Promise sub.then @find_subscription else find_subscription(sub) end end |
#update(mod_id, status, value) ⇒ Object
Triggers an update to be sent to listening callbacks
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/orchestrator/status.rb', line 84 def update(mod_id, status, value) mod = @subscriptions[mod_id] if mod subscribed = mod[status] if subscribed subscribed.each do |subscription| begin subscription.notify(value) rescue => e @controller.log_unhandled_exception(e) end end end end end |