Class: Bumbleworks::Worker::Info

Inherits:
Ruote::Worker::Info
  • Object
show all
Extended by:
Enumerable, Forwardable
Defined in:
lib/bumbleworks/worker/info.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#workerObject (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

.allObject



23
24
25
# File 'lib/bumbleworks/worker/info.rb', line 23

def all
  to_a
end

.eachObject



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

.filterObject



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_infoObject



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_hashObject



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(options)
  filter_proc = proc { |worker|
    options.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_hashObject



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

Returns:

  • (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(options = {})
  send_command("paused", options)
end

#processed_last_hourObject



187
188
189
# File 'lib/bumbleworks/worker/info.rb', line 187

def processed_last_hour
  raw_hash["processed_last_hour"]
end

#processed_last_minuteObject



179
180
181
# File 'lib/bumbleworks/worker/info.rb', line 179

def processed_last_minute
  raw_hash["processed_last_minute"]
end

#raw_hashObject



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

#reloadObject



89
90
91
# File 'lib/bumbleworks/worker/info.rb', line 89

def reload
  @worker = Bumbleworks::Worker::Proxy.new(raw_hash)
end

#responding?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
131
132
133
134
135
136
137
138
# File 'lib/bumbleworks/worker/info.rb', line 128

def responding?(options = {})
  options[:since] ||= Time.now - Bumbleworks.timeout
  Bumbleworks::Worker.with_worker_state_enabled do
    Bumbleworks::Support.wait_until(options) do
      updated_since?(options[:since])
    end
  end
  true
rescue Bumbleworks::Support::WaitTimeout
  false
end

#saveObject



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 save_control_message(message)
  doc = Bumbleworks::Worker.control_document
  doc["workers"][id] ||= {}
  doc["workers"][id]["state"] = message
  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, options = {})
  save_control_message(command)
  Bumbleworks::Worker.with_worker_state_enabled do
    Bumbleworks::Support.wait_until(options) 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(options = {})
  send_command("stopped", options)
end

#stalling?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/bumbleworks/worker/info.rb', line 140

def stalling?
  !responding?
end

#storageObject



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(options = {})
  send_command("running", options)
end

#updated_atObject



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

Returns:

  • (Boolean)


122
123
124
125
126
# File 'lib/bumbleworks/worker/info.rb', line 122

def updated_recently?(options = {})
  options[:seconds_ago] ||= Bumbleworks.timeout
  latest_time = Time.now - options[:seconds_ago]
  updated_since?(latest_time)
end

#updated_since?(latest_time) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/bumbleworks/worker/info.rb', line 118

def updated_since?(latest_time)
  updated_at >= latest_time
end

#uptimeObject



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_hourObject



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_minuteObject



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_nameObject



98
99
100
# File 'lib/bumbleworks/worker/info.rb', line 98

def worker_class_name
  worker.class_name
end