Class: Orchestrator::Device::CommandQueue
- Inherits:
-
Object
- Object
- Orchestrator::Device::CommandQueue
- Defined in:
- lib/orchestrator/device/command_queue.rb
Constant Summary collapse
- OFFLINE_MSG =
Error::CommandCanceled.new 'command canceled as module went offline'
Instance Attribute Summary collapse
-
#pause ⇒ Object
readonly
Returns the value of attribute pause.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#waiting ⇒ Object
Returns the value of attribute waiting.
Instance Method Summary collapse
- #cancel_all(msg) ⇒ Object
-
#initialize(loop, callback) ⇒ CommandQueue
constructor
init -> mod.load -> post_init So config can be set in on_load if desired.
- #length ⇒ Object
- #offline(clear = false) ⇒ Object
-
#online ⇒ Object
If offline we’ll only maintain named command state and queue.
- #push(command, priority) ⇒ Object
- #shift ⇒ Object
- #shift_next_tick ⇒ Object
Constructor Details
#initialize(loop, callback) ⇒ CommandQueue
init -> mod.load -> post_init So config can be set in on_load if desired
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/orchestrator/device/command_queue.rb', line 26 def initialize(loop, callback) @loop = loop @callback = callback @named_commands = { # name: [[priority list], command] # where command may be nil } @comparison = method(:comparison) @pending_commands = Containers::Heap.new(&@comparison) @waiting = nil # Last command sent that was marked as waiting @pause = 0 @state = :online # online / offline @pause_shift = method(:pause_shift) @move_forward = method(:move_forward) end |
Instance Attribute Details
#pause ⇒ Object (readonly)
Returns the value of attribute pause.
21 22 23 |
# File 'lib/orchestrator/device/command_queue.rb', line 21 def pause @pause end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
20 21 22 |
# File 'lib/orchestrator/device/command_queue.rb', line 20 def state @state end |
#waiting ⇒ Object
Returns the value of attribute waiting.
19 20 21 |
# File 'lib/orchestrator/device/command_queue.rb', line 19 def waiting @waiting end |
Instance Method Details
#cancel_all(msg) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/orchestrator/device/command_queue.rb', line 162 def cancel_all(msg) while length > 0 cmd = @pending_commands.pop if cmd.is_a? Symbol res = @named_commands[cmd] if res res[1][:defer].reject(msg) @named_commands.delete(cmd) end else cmd[:defer].reject(msg) end end end |
#length ⇒ Object
116 117 118 |
# File 'lib/orchestrator/device/command_queue.rb', line 116 def length @pending_commands.size end |
#offline(clear = false) ⇒ Object
130 131 132 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 |
# File 'lib/orchestrator/device/command_queue.rb', line 130 def offline(clear = false) @state = :offline if clear @waiting[:defer].reject(OFFLINE_MSG) if @waiting cancel_all(OFFLINE_MSG) @waiting = nil else # Keep named commands new_queue = Containers::Heap.new(&@comparison) while length > 0 cmd = @pending_commands.pop if cmd.is_a? Symbol res = @named_commands[cmd][0] pri = res.shift res << pri queue_push(new_queue, cmd, pri) else cmd[:defer].reject(OFFLINE_MSG) end end @pending_commands = new_queue # clear waiting if it is not a named command. # The processor will re-queue it if retry on disconnect is set if @waiting && @waiting[:name].nil? @waiting = nil end end end |
#online ⇒ Object
If offline we’ll only maintain named command state and queue
122 123 124 125 126 127 128 |
# File 'lib/orchestrator/device/command_queue.rb', line 122 def online @state = :online # next tick is important as it allows the module time to updated # any named commands that it desires in the connected callback shift_next_tick end |
#push(command, priority) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/orchestrator/device/command_queue.rb', line 84 def push(command, priority) if @state == :offline && command[:name].nil? return end if command[:name] name = command[:name].to_sym current = @named_commands[name] ||= [[], nil] # Chain the promises if the named command is already in the queue cmd = current[1] cmd[:defer].resolve(command[:defer].promise) if cmd current[1] = command # replace the old command priors = current[0] # Only add commands of higher priority to the queue if priors.empty? || priors[-1] < priority priors << priority queue_push(@pending_commands, name, priority) end else queue_push(@pending_commands, command, priority) end if @waiting.nil? && @state == :online shift # This will trigger the callback end end |
#shift ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/orchestrator/device/command_queue.rb', line 49 def shift return if @pause > 0 # we are waiting for the next_tick? @waiting = nil # Discard the current command if length > 0 next_cmd = @pending_commands.pop if next_cmd.is_a? Symbol # (named command) result = @named_commands[next_cmd] result[0].shift cmd = result[1] if cmd.nil? shift_next_tick if length > 0 return # command already executed, this is a no-op else result[1] = nil end else cmd = next_cmd end @waiting = cmd if cmd[:wait] shift_promise = @callback.call cmd if shift_promise.is_a? ::Libuv::Q::Promise @pause += 1 shift_promise.finally do # NOTE:: This schedule may not be required... @loop.schedule @move_forward end else shift_next_tick if length > 0 end end end |
#shift_next_tick ⇒ Object
44 45 46 47 |
# File 'lib/orchestrator/device/command_queue.rb', line 44 def shift_next_tick @pause += 1 @loop.next_tick @pause_shift end |