Module: Datacraft::Runner

Included in:
Datacraft
Defined in:
lib/datacraft/runner.rb

Overview

The runner for the whole process

Instance Method Summary collapse

Instance Method Details

#build(consumers) ⇒ Object

build and close consumers



83
84
85
86
87
88
# File 'lib/datacraft/runner.rb', line 83

def build(consumers)
  consumers.each do |consumer|
    consumer.build if consumer.respond_to? :build
    consumer.close if consumer.respond_to? :close
  end
end

#pprocess_rows(providers, tweakers, consumers) ⇒ Object

process rows in parallel



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/datacraft/runner.rb', line 64

def pprocess_rows(providers, tweakers, consumers)
  threads = providers.map do |provider|
    Thread.new(provider) do |p|
      p.each do |row|
        tweakers.each do |tweaker|
          row = tweaker.tweak row
          break unless row
        end
        next unless row
        consumers.each do |consumer|
          consumer << row
        end
      end
    end
  end
  threads.each(&:join)
end

#process_rows(providers, tweakers, consumers) ⇒ Object

process rows sequentially



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/datacraft/runner.rb', line 47

def process_rows(providers, tweakers, consumers)
  providers.each do |provider|
    provider.each do |row|
      tweakers.each do |tweaker|
        row = tweaker.tweak row
        break unless row
      end
      # nil means to dismiss the row
      next unless row
      consumers.each do |consumer|
        consumer << row
      end
    end
  end
end

#report(measurements) ⇒ Object

output benchmark results



38
39
40
41
42
43
44
# File 'lib/datacraft/runner.rb', line 38

def report(measurements)
  width = measurements.max_by { |m| m.label.size }.label.size + 1
  puts "#{' ' * width}#{Benchmark::CAPTION}"
  measurements.each do |m|
    puts "#{m.label.to_s.ljust width} #{m}"
  end
end

#run(instruction) ⇒ Object

run the instruction



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/datacraft/runner.rb', line 7

def run(instruction)
  context = instruction.context
  measurements = []
  measurements << Benchmark.measure('pre build:') do
    context.pre_hooks.each(&:call)
  end
  measurements << Benchmark.measure('process rows:') do
    if context.options[:parallel]
      pprocess_rows(
        context.providers,
        context.tweakers,
        context.consumers)
    else
      process_rows(
        context.providers,
        context.tweakers,
        context.consumers)
    end
  end

  measurements << Benchmark.measure('build:') do
    build context.consumers
  end

  measurements << Benchmark.measure('post build:') do
    context.post_hooks.each(&:call)
  end
  report measurements if context.options[:benchmark]
end