Class: BatchManager

Inherits:
Object
  • Object
show all
Defined in:
lib/task_batcher.rb

Overview

The BatchManager class provides a more OO-style API to the functionality of TaskBatcher. Multiple BatchManagers can be instantiated, and there is no need to repeat the callback and block parameters multiple times in your code.

This is a cleaner interface if your batched jobs get added in a reasonably localized scope (where your BatchManager can live). If batched jobs get added all over the code base, the TaskBatcher interface is probably a better fit, even though you’ll have to repeat the callback and block every time you add a job.

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, callback = nil, duration = nil, &block) ⇒ BatchManager

Create a BatchManager to wrap calls to TaskBatcher.

Parameters:

  • name (String, Symbol) (defaults to: nil)

    arbitrary task identifier, or nil to auto-generate one

  • callback (#call) (defaults to: nil)

    callable which takes one parameter (of whatever type is returned by the block), or nil to discard block’s return value

  • duration (Fixnum) (defaults to: nil)

    batch duration, or nil to use the default

  • block (#call)

    callable which takes one parameter (list of parameter-groups, one per #add invocation) and batch-processes them



256
257
258
259
260
261
# File 'lib/task_batcher.rb', line 256

def initialize(name=nil, callback=nil, duration=nil, &block)
  @name = name || "ObjectID:#{self.object_id}"
  @callback = callback
  @block = block
  (self.batch_duration = duration) if duration
end

Instance Method Details

#batch_durationFixnum

Return the batch duration for this TaskBatch

Returns:

  • (Fixnum)

    number of seconds to delay before running this batch



291
292
293
# File 'lib/task_batcher.rb', line 291

def batch_duration
  TaskBatcher.batch_duration(@name)
end

#batch_duration=(seconds) ⇒ Object

Set the batch duration for this TaskBatch.

Parameters:

  • seconds (Fixnum)

    number of seconds to delay before running the batch



284
285
286
# File 'lib/task_batcher.rb', line 284

def batch_duration=(seconds)
  TaskBatcher.set_batch_duration(@name, seconds)
end

#process_batchObject

Generally, the #process_batch method will be called by Event Machine when the batch duration expires. However, this method is public because the caller may want to explicitly process a batch.



298
299
300
# File 'lib/task_batcher.rb', line 298

def process_batch
  TaskBatcher.process_batch(@name)
end

#task(task_params) ⇒ Object

Add a job to the TaskBatch, to be executed when the batch duration expires.

Once the batch duration expires, all tasks in the batch will be run, i.e. the block will be passed a list of task_args_hash values and the block is expected to process that entire list.

Usage: batch = TaskBatch.new(‘mybatch’, mycallback) {|data_list| myfn(data_list)} batch.task(params1) batch.task(params2) … # after the batch duration expires, any params in the batch will be # processed all at once



277
278
279
# File 'lib/task_batcher.rb', line 277

def task(task_params)
  TaskBatcher.task(@name, task_params, @callback, &@block)
end