Class: Bumbleworks::Worker::Info
- Inherits:
-
Ruote::Worker::Info
- Object
- Ruote::Worker::Info
- Bumbleworks::Worker::Info
- Extended by:
- Enumerable, Forwardable
- Defined in:
- lib/bumbleworks/worker/info.rb
Instance Attribute Summary collapse
-
#worker ⇒ Object
readonly
Returns the value of attribute worker.
Class Method Summary collapse
- .[](worker_id) ⇒ Object
- .all ⇒ Object
- .each ⇒ Object
- .filter ⇒ Object
- .forget_worker(id_to_delete) ⇒ Object
- .from_hash(hash) ⇒ Object
- .purge_stale_worker_info ⇒ Object
- .purge_worker_info(&block) ⇒ Object
- .raw_hash ⇒ Object
- .where(options) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #constant_worker_info_hash ⇒ Object
- #in_stopped_state? ⇒ Boolean
-
#initialize(worker) ⇒ Info
constructor
A new instance of Info.
- #pause(options = {}) ⇒ Object
- #processed_last_hour ⇒ Object
- #processed_last_minute ⇒ Object
- #raw_hash ⇒ Object
- #record_new_state(state) ⇒ Object
- #reload ⇒ Object
- #responding?(options = {}) ⇒ Boolean
- #save ⇒ Object
- #save_control_message(message) ⇒ Object
- #send_command(command, options = {}) ⇒ Object
- #shutdown(options = {}) ⇒ Object
- #stalling? ⇒ Boolean
- #storage ⇒ Object
- #unpause(options = {}) ⇒ Object (also: #run)
- #updated_at ⇒ Object
- #updated_recently?(options = {}) ⇒ Boolean
- #updated_since?(latest_time) ⇒ Boolean
- #uptime ⇒ Object
- #wait_time_last_hour ⇒ Object
- #wait_time_last_minute ⇒ Object
- #worker_class_name ⇒ Object
Constructor Details
#initialize(worker) ⇒ Info
Returns a new instance of Info.
73 74 75 76 77 78 |
# File 'lib/bumbleworks/worker/info.rb', line 73 def initialize(worker) @worker = worker @last_save = Time.now - 2 * 60 @msgs = [] unless worker.is_a?(Bumbleworks::Worker::Proxy) end |
Instance Attribute Details
#worker ⇒ Object (readonly)
Returns the value of attribute worker.
5 6 7 |
# File 'lib/bumbleworks/worker/info.rb', line 5 def worker @worker end |
Class Method Details
.[](worker_id) ⇒ Object
41 42 43 |
# File 'lib/bumbleworks/worker/info.rb', line 41 def [](worker_id) from_hash(raw_hash[worker_id]) end |
.all ⇒ Object
23 24 25 |
# File 'lib/bumbleworks/worker/info.rb', line 23 def all to_a end |
.each ⇒ Object
17 18 19 20 21 |
# File 'lib/bumbleworks/worker/info.rb', line 17 def each raw_hash.each { |k, v| yield from_hash(v) } end |
.filter ⇒ Object
36 37 38 39 |
# File 'lib/bumbleworks/worker/info.rb', line 36 def filter return [] unless block_given? select { |info| yield info.worker } end |
.forget_worker(id_to_delete) ⇒ Object
49 50 51 52 53 |
# File 'lib/bumbleworks/worker/info.rb', line 49 def forget_worker(id_to_delete) purge_worker_info do |id, info| id == id_to_delete end end |
.from_hash(hash) ⇒ Object
45 46 47 |
# File 'lib/bumbleworks/worker/info.rb', line 45 def from_hash(hash) new(Bumbleworks::Worker::Proxy.new(hash)) end |
.purge_stale_worker_info ⇒ Object
55 56 57 58 59 |
# File 'lib/bumbleworks/worker/info.rb', line 55 def purge_stale_worker_info purge_worker_info do |id, info| info['state'].nil? || info['state'] == 'stopped' end end |
.purge_worker_info(&block) ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/bumbleworks/worker/info.rb', line 61 def purge_worker_info(&block) doc = Bumbleworks.dashboard.storage.get('variables', 'workers') return unless doc doc['workers'] = doc['workers'].reject { |id, info| block.call(id, info) } result = Bumbleworks.dashboard.storage.put(doc) purge_stale_worker_info if result all end |
.raw_hash ⇒ Object
13 14 15 |
# File 'lib/bumbleworks/worker/info.rb', line 13 def raw_hash Bumbleworks.dashboard.worker_info || {} end |
.where(options) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/bumbleworks/worker/info.rb', line 27 def where() filter_proc = proc { |worker| .all? { |k, v| worker.send(k.to_sym) == v } } filter(&filter_proc) end |
Instance Method Details
#==(other) ⇒ Object
80 81 82 83 |
# File 'lib/bumbleworks/worker/info.rb', line 80 def ==(other) other.is_a?(Bumbleworks::Worker::Info) && other.worker == worker end |
#constant_worker_info_hash ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/bumbleworks/worker/info.rb', line 195 def constant_worker_info_hash { "id" => @worker.id, "class" => @worker.class_name, "name" => @worker.name, "ip" => @worker.ip, "hostname" => @worker.hostname, "pid" => @worker.pid, "system" => @worker.system, "launched_at" => @worker.launched_at, "state" => @worker.state } end |
#in_stopped_state? ⇒ Boolean
110 111 112 |
# File 'lib/bumbleworks/worker/info.rb', line 110 def in_stopped_state? worker.state.nil? || ["stopped", "stalled"].include?(worker.state) end |
#pause(options = {}) ⇒ Object
152 153 154 |
# File 'lib/bumbleworks/worker/info.rb', line 152 def pause( = {}) send_command("paused", ) end |
#processed_last_hour ⇒ Object
187 188 189 |
# File 'lib/bumbleworks/worker/info.rb', line 187 def processed_last_hour raw_hash["processed_last_hour"] end |
#processed_last_minute ⇒ Object
179 180 181 |
# File 'lib/bumbleworks/worker/info.rb', line 179 def processed_last_minute raw_hash["processed_last_minute"] end |
#raw_hash ⇒ Object
85 86 87 |
# File 'lib/bumbleworks/worker/info.rb', line 85 def raw_hash self.class.raw_hash[worker.id] end |
#record_new_state(state) ⇒ Object
93 94 95 96 |
# File 'lib/bumbleworks/worker/info.rb', line 93 def record_new_state(state) worker.state = state save end |
#reload ⇒ Object
89 90 91 |
# File 'lib/bumbleworks/worker/info.rb', line 89 def reload @worker = Bumbleworks::Worker::Proxy.new(raw_hash) end |
#responding?(options = {}) ⇒ Boolean
128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/bumbleworks/worker/info.rb', line 128 def responding?( = {}) [:since] ||= Time.now - Bumbleworks.timeout Bumbleworks::Worker.with_worker_state_enabled do Bumbleworks::Support.wait_until() do updated_since?([:since]) end end true rescue Bumbleworks::Support::WaitTimeout false end |
#save ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/bumbleworks/worker/info.rb', line 209 def save doc = Bumbleworks::Worker.info_document worker_info_hash = doc['workers'][@worker.id] || {} worker_info_hash.merge!(constant_worker_info_hash) worker_info_hash.merge!({ 'put_at' => Ruote.now_to_utc_s, 'uptime' => uptime, }) if defined?(@msgs) now = Time.now @msgs = @msgs.drop_while { |msg| Time.parse(msg['processed_at']) < now - 3600 } mm = @msgs.drop_while { |msg| Time.parse(msg['processed_at']) < now - 60 } hour_count = @msgs.size < 1 ? 1 : @msgs.size minute_count = mm.size < 1 ? 1 : mm.size worker_info_hash.merge!({ 'processed_last_minute' => mm.size, 'wait_time_last_minute' => mm.inject(0.0) { |s, m| s + m['wait_time'] } / minute_count.to_f, 'processed_last_hour' => @msgs.size, 'wait_time_last_hour' => @msgs.inject(0.0) { |s, m| s + m['wait_time'] } / hour_count.to_f }) end doc['workers'][@worker.id] = worker_info_hash r = storage.put(doc) @last_save = Time.now save if r != nil end |
#save_control_message(message) ⇒ Object
172 173 174 175 176 177 |
# File 'lib/bumbleworks/worker/info.rb', line 172 def () doc = Bumbleworks::Worker.control_document doc["workers"][id] ||= {} doc["workers"][id]["state"] = storage.put(doc) end |
#send_command(command, options = {}) ⇒ Object
162 163 164 165 166 167 168 169 170 |
# File 'lib/bumbleworks/worker/info.rb', line 162 def send_command(command, = {}) (command) Bumbleworks::Worker.with_worker_state_enabled do Bumbleworks::Support.wait_until() do raw_hash["state"] == command end end reload end |
#shutdown(options = {}) ⇒ Object
148 149 150 |
# File 'lib/bumbleworks/worker/info.rb', line 148 def shutdown( = {}) send_command("stopped", ) end |
#stalling? ⇒ Boolean
140 141 142 |
# File 'lib/bumbleworks/worker/info.rb', line 140 def stalling? !responding? end |
#storage ⇒ Object
144 145 146 |
# File 'lib/bumbleworks/worker/info.rb', line 144 def storage @worker.storage || Bumbleworks.dashboard.storage end |
#unpause(options = {}) ⇒ Object Also known as: run
156 157 158 |
# File 'lib/bumbleworks/worker/info.rb', line 156 def unpause( = {}) send_command("running", ) end |
#updated_at ⇒ Object
114 115 116 |
# File 'lib/bumbleworks/worker/info.rb', line 114 def updated_at Time.parse(raw_hash['put_at']) end |
#updated_recently?(options = {}) ⇒ Boolean
122 123 124 125 126 |
# File 'lib/bumbleworks/worker/info.rb', line 122 def updated_recently?( = {}) [:seconds_ago] ||= Bumbleworks.timeout latest_time = Time.now - [:seconds_ago] updated_since?(latest_time) end |
#updated_since?(latest_time) ⇒ Boolean
118 119 120 |
# File 'lib/bumbleworks/worker/info.rb', line 118 def updated_since?(latest_time) updated_at >= latest_time end |
#uptime ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/bumbleworks/worker/info.rb', line 102 def uptime if in_stopped_state? && worker.respond_to?(:uptime) worker.uptime else Time.now - worker.launched_at end end |
#wait_time_last_hour ⇒ Object
191 192 193 |
# File 'lib/bumbleworks/worker/info.rb', line 191 def wait_time_last_hour raw_hash["wait_time_last_hour"] end |
#wait_time_last_minute ⇒ Object
183 184 185 |
# File 'lib/bumbleworks/worker/info.rb', line 183 def wait_time_last_minute raw_hash["wait_time_last_minute"] end |
#worker_class_name ⇒ Object
98 99 100 |
# File 'lib/bumbleworks/worker/info.rb', line 98 def worker_class_name worker.class_name end |