Class: Paraduct::Runner

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Runner

Returns a new instance of Runner.

Parameters:

  • args (defaults to: {})

Options Hash (args):

  • :script (String, Array<String>)

    script file, script(s)

  • :params (Hash{String => String})

    key is capitalized and value is quoted (ex. foo=1 => FOO="1" )

  • :base_job_dir (String)


11
12
13
14
15
16
# File 'lib/paraduct/runner.rb', line 11

def initialize(args={})
  @script       = args[:script]
  @params       = args[:params]
  @base_job_dir = args[:base_job_dir]
  @job_id       = args[:job_id]
end

Instance Attribute Details

#base_job_dirObject (readonly)

Returns the value of attribute base_job_dir.



5
6
7
# File 'lib/paraduct/runner.rb', line 5

def base_job_dir
  @base_job_dir
end

#paramsObject (readonly)

Returns the value of attribute params.



5
6
7
# File 'lib/paraduct/runner.rb', line 5

def params
  @params
end

#scriptObject (readonly)

Returns the value of attribute script.



5
6
7
# File 'lib/paraduct/runner.rb', line 5

def script
  @script
end

Class Method Details

.capitalize_keys(params) ⇒ Object



63
64
65
66
67
68
# File 'lib/paraduct/runner.rb', line 63

def self.capitalize_keys(params)
  params.inject({}) do |res, (key, value)|
    res[key.upcase] = value
    res
  end
end

Instance Method Details

#formatted_paramsObject



49
50
51
# File 'lib/paraduct/runner.rb', line 49

def formatted_params
  @params.map{ |key, value| "#{key}=#{value}" }.join(", ")
end

#job_dirObject



37
38
39
# File 'lib/paraduct/runner.rb', line 37

def job_dir
  Pathname(@base_job_dir).join(job_name)
end

#job_nameObject



41
42
43
# File 'lib/paraduct/runner.rb', line 41

def job_name
  key_capitalized_params.map { |key, value| "#{key}_#{value}" }.join("_").gsub(%r([/ ]), "_")
end

#key_capitalized_paramsObject



45
46
47
# File 'lib/paraduct/runner.rb', line 45

def key_capitalized_params
  self.class.capitalize_keys(@params)
end

#loggerObject



53
54
55
56
57
58
59
60
61
# File 'lib/paraduct/runner.rb', line 53

def logger
  unless @logger
    stdout_logger = Paraduct::ColoredLabelLogger.new(object_id)
    file_logger   = Logger.new(Pathname(@base_job_dir).join("#{job_name}.log"))
    @logger       = stdout_logger.extend(ActiveSupport::Logger.broadcast(file_logger))
  end

  @logger
end

#performString

run script with params

Returns:

  • (String)

    stdout

Raises:



27
28
29
30
31
32
33
34
35
# File 'lib/paraduct/runner.rb', line 27

def perform
  export_variables = key_capitalized_params.merge("PARADUCT_JOB_ID" => @job_id, "PARADUCT_JOB_NAME" => job_name)
  variable_string = export_variables.map{ |key, value| %(export #{key}="#{value}";) }.join(" ")

  Array.wrap(@script).inject("") do |stdout, command|
    stdout << run_command("#{variable_string} #{command}")
    stdout
  end
end

#setup_dirObject



18
19
20
21
22
# File 'lib/paraduct/runner.rb', line 18

def setup_dir
  FileUtils.mkdir_p(job_dir) unless job_dir.exist?
  Paraduct::SyncUtils.copy_recursive(Paraduct.config.root_dir, job_dir)
  Dir.chdir(job_dir)
end