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.
-
#reloaded_system(sys_id) ⇒ Object
- TODO
-
we also need the system class to contact each of the threads.
-
#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.
-
#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
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 |
# File 'lib/orchestrator/status.rb', line 142 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 |
#reloaded_system(sys_id) ⇒ Object
- TODO
-
we also need the system class to contact each of the threads
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/orchestrator/status.rb', line 101 def reloaded_system(sys_id) subscriptions = @systems[sys_id] if subscriptions sys = ::Orchestrator::System.get(@system) 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 # 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 |
#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 |