Class: CommandRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/protk/command_runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment) ⇒ CommandRunner

Returns a new instance of CommandRunner.


21
22
23
# File 'lib/protk/command_runner.rb', line 21

def initialize(environment)
  @env=environment
end

Instance Attribute Details

#envObject (readonly)

The protk environment in which to run commands


16
17
18
# File 'lib/protk/command_runner.rb', line 16

def env
  @env
end

Instance Method Details

#run_batch(command_string, job_params, jobscript_path, autodelete) ⇒ Object

Runs the given command as a background job At present this sends the job to a PBS system, but in future we might support other types of background jobs


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/protk/command_runner.rb', line 55

def run_batch(command_string,job_params,jobscript_path,autodelete)
  @env.log("Creating batch file for command: #{command_string}",:info)

  if ( autodelete )
#    command_string<<";rm #{jobscript_path}"
  end

  jobid=job_params[:jobid]
  if ( job_params[:vmem]==nil)
    job_params[:vmem]="900mb"
  end
  if (job_params[:queue] ==nil )
    job_params[:queue]="lowmem"
  end

  job_script="#!/bin/bash
  #PBS -N #{jobid}
  #PBS -e pbs.#{jobid}.err 
  #PBS -o pbs.#{jobid}.log
  #PBS -l nodes=1:ppn=1,vmem=#{job_params[:vmem]}
  #PBS -q #{job_params[:queue]}
  #{command_string}"

  p File.open(jobscript_path, 'w') {|f| f.write(job_script) }

  self.run_local("qsub #{jobscript_path}")
  
end

#run_local(command_string) ⇒ Object

Runs the given command in a local shell


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/protk/command_runner.rb', line 30

def run_local(command_string)
  @env.log("Command: #{command_string} started",:info)
  status = Open4::popen4("#{command_string} ") do |pid, stdin, stdout, stderr|
    puts "PID #{pid}" 
    
    stdout.each { |line| @env.log(line.chomp,:info) }

    stderr.each { |line| @env.log(line.chomp,:warn) }

  end
  if ( status!=0 )
    # We terminated with some error code so log as an error
    @env.log( "Command: #{command_string} exited with status #{status.to_s}",:error)
  else
    @env.log( "Command: #{command_string} exited with status #{status.to_s}",:info)      
  end
  status     
end