Class: Sq::Dbsync::Pipeline::ThreadedContext

Inherits:
Object
  • Object
show all
Defined in:
lib/sq/dbsync/pipeline/threaded_context.rb

Overview

A computational context for passing a number of tasks through a set of stages, where each stage uses resources independent of the other stages. For example, stage one may be able to execute a maximum of two tasks at once, and stage two may also have a maximum of two, but it is optimum that a total of four tasks to be processing at any one time.

Constant Summary collapse

FINISH =

Tracer object to mark the end of a stream of tasks.

Object.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tasks, stages, process) ⇒ ThreadedContext

Returns a new instance of ThreadedContext.



20
21
22
23
24
25
# File 'lib/sq/dbsync/pipeline/threaded_context.rb', line 20

def initialize(tasks, stages, process)
  self.tasks   = tasks
  self.stages  = stages
  self.process = process
  self.threads = []
end

Class Method Details

.call(*args, &block) ⇒ Object



16
17
18
# File 'lib/sq/dbsync/pipeline/threaded_context.rb', line 16

def self.call(*args, &block)
  new(*args, &block).run
end

Instance Method Details

#runObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sq/dbsync/pipeline/threaded_context.rb', line 27

def run
  initial_queue, final_queue = build_pipeline(stages, tasks.length)

  tasks.each.with_index do |task, i|
    initial_queue << [i, task]
  end

  result = ordered (0...tasks.length).map { final_queue.pop }
  flush_threads(initial_queue)
  result
end