Class: Bluth::Gob

Inherits:
Storable
  • Object
show all
Includes:
Familia, Familia::Stamps
Defined in:
lib/bluth.rb

Constant Summary collapse

MAX_ATTEMPTS =
3.freeze

Instance Method Summary collapse

Instance Method Details

#attempt!Object



249
250
251
# File 'lib/bluth.rb', line 249

def attempt!
  @attempts = attempts + 1
end

#attempt?Boolean

Returns:

  • (Boolean)


246
247
248
# File 'lib/bluth.rb', line 246

def attempt?
  attempts < MAX_ATTEMPTS
end

#clear!Object



236
237
238
239
240
# File 'lib/bluth.rb', line 236

def clear!
  @attempts = 0
  @messages = []
  save
end

#current_queueObject



252
253
254
# File 'lib/bluth.rb', line 252

def current_queue
  @current_queue
end

#delayed?Boolean

Returns:

  • (Boolean)


268
269
270
271
# File 'lib/bluth.rb', line 268

def delayed?
  start = @stime || 0
  start > Time.now.utc.to_f
end

#dequeue!Object



293
294
295
296
# File 'lib/bluth.rb', line 293

def dequeue!
  Familia.ld "Deleting #{self.jobid} from #{queue.rediskey}" if Familia.debug?
  queue.remove 0, self.jobid
end

#durationObject



285
286
287
288
289
# File 'lib/bluth.rb', line 285

def duration
  return 0 if @stime.nil?
  et = @etime || Time.now.utc.to_i
  et - @stime
end

#failure!(msg = nil) ⇒ Object



275
276
277
278
279
# File 'lib/bluth.rb', line 275

def failure!(msg=nil)
  @etime = Time.now.utc.to_i
  self.handler.failure!
  move! :failed, msg
end

#handlerObject



255
256
257
# File 'lib/bluth.rb', line 255

def handler
  eval "::#{@handler}" if @handler
end

#jobidObject



233
234
235
# File 'lib/bluth.rb', line 233

def jobid
  Gibbler::Digest.new(@jobid)
end

#move!(to, msg = nil) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/bluth.rb', line 300

def move!(to, msg=nil)
  @thread_id = $$
  #if to.to_s == current_queue.to_s
  #  raise Bluth::Buster, "Cannot move job to the queue it's in: #{to}"
  #end
  from, to = Bluth.queue(current_queue), Bluth.queue(to)
  Familia.ld "Moving #{self.jobid} from #{from.rediskey} to #{to.rediskey}" if Familia.debug?
  @messages << msg unless msg.nil? || msg.empty?
  # We push first to make sure we never lose a Gob ID. Instead
  # there's the small chance of a job ID being in two queues. 
  to << @jobid
  ret = from.remove @jobid, 0
  @current_queue = to.name
  save # update messages
end

#performObject



258
259
260
261
262
263
264
265
266
267
# File 'lib/bluth.rb', line 258

def perform
  @attempts += 1
  Familia.ld "PERFORM: #{self.to_hash.inspect}" if Familia.debug?
  @stime = Time.now.utc.to_f
  save # update the time
  self.handler.prepare if self.class.respond_to?(:prepare)
  self.handler.perform @data
  @etime = Time.now.utc.to_f
  save # update the time
end

#preprocessObject



241
242
243
244
245
# File 'lib/bluth.rb', line 241

def preprocess
  @attempts ||= 0
  @messages ||= []
  @create_time ||= Time.now.utc.to_f
end

#queueObject



290
291
292
# File 'lib/bluth.rb', line 290

def queue
  Bluth.queue(current_queue)
end

#retry!(msg = nil) ⇒ Object



272
273
274
# File 'lib/bluth.rb', line 272

def retry!(msg=nil) 
  move! :high, msg
end

#running!Object



297
298
299
# File 'lib/bluth.rb', line 297

def running!
  move! :running
end

#success!(msg = nil) ⇒ Object



280
281
282
283
284
# File 'lib/bluth.rb', line 280

def success!(msg=nil)
  @etime = Time.now.utc.to_i
  self.handler.success!
  move! :successful, msg
end