Class: PrettyMultitask::Runner

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

Overview

This class will run multiple callables in parallel using RunCallable which add a nice format

Instance Method Summary collapse

Constructor Details

#initialize(jobs) ⇒ Runner

Returns a new instance of Runner.



7
8
9
10
11
12
13
14
# File 'lib/pretty_multitask/runner.rb', line 7

def initialize(jobs)
  @jobs = jobs
  @jobs.each do |j|
    i[name cmd].each { |o| raise "#{o} must be specified for job #{j}" unless j[o] }
    j[:out_file] = "/tmp/#{j[:name]}-#{Time.now.strftime('%s.%N')}"
    FileUtils.touch j[:out_file]
  end
end

Instance Method Details

#longest_jobnameObject



49
50
51
52
53
# File 'lib/pretty_multitask/runner.rb', line 49

def longest_jobname
  longest = 0
  @jobs.each { |job| longest = job[:name].length if job[:name].length > longest }
  longest
end


34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pretty_multitask/runner.rb', line 34

def print_out
  @jobs.each do |j|
    label = "[ #{j[:name]} ]"
    width = IO.console.winsize[-1]
    width = 80 unless width > 0
    left = '=' * ((width - label.length) / 2)
    right = j[:name].length.even? ? left : left + '='
    puts "\n"
    puts Color.yellow left + label + right
    puts File.read j[:out_file]
    puts Color.yellow '=' * width
    File.delete j[:out_file]
  end
end

#runObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pretty_multitask/runner.rb', line 16

def run
  exec = Tlopo::Executor.new 10

  @jobs.each { |job| job[:padding] = longest_jobname }
  @jobs.each do |j|
    task = proc { j[:exit_status] = RunCallable.new(j).run }
    exec.schedule task
  end
  errors = exec.run.errors

  print_out

  unless errors.empty?
    errors.each { |e| LOGGER.error e }
    raise 'Found errors'
  end
end