Class: BatchManager
- Inherits:
-
Object
- Object
- BatchManager
- 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
-
#batch_duration ⇒ Fixnum
Return the batch duration for this TaskBatch.
-
#batch_duration=(seconds) ⇒ Object
Set the batch duration for this TaskBatch.
-
#initialize(name = nil, callback = nil, duration = nil, &block) ⇒ BatchManager
constructor
Create a BatchManager to wrap calls to TaskBatcher.
-
#process_batch ⇒ Object
Generally, the #process_batch method will be called by Event Machine when the batch duration expires.
-
#task(task_params) ⇒ Object
Add a job to the TaskBatch, to be executed when the batch duration expires.
Constructor Details
#initialize(name = nil, callback = nil, duration = nil, &block) ⇒ BatchManager
Create a BatchManager to wrap calls to TaskBatcher.
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_duration ⇒ Fixnum
Return the batch duration for this TaskBatch
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.
284 285 286 |
# File 'lib/task_batcher.rb', line 284 def batch_duration=(seconds) TaskBatcher.set_batch_duration(@name, seconds) end |
#process_batch ⇒ Object
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 |