module OscMacheteRails::Workflow::BuilderMethods

depends on jobs_active_record_relation being defined

Public Class Methods

included(obj) click to toggle source

Methods run when this module is included

# File lib/osc_machete_rails/workflow.rb, line 33
def self.included(obj)
  # before we destroy ActiveRecord we stop all jobs and delete staging dir
  # prepend: true tells this to run before any of the jobs are destroyed
  # so we can stop them first and recover if there is a problem
  if obj.respond_to?(:before_destroy)
    obj.before_destroy prepend: true do |sim|
      sim.stop ? sim.delete_staging : false
    end
  end
end

Public Instance Methods

after_stage(staged_dir) click to toggle source

Actions to perform after staging

@param [String] staged_dir The staged directory path.

# File lib/osc_machete_rails/workflow.rb, line 137
def after_stage(staged_dir)
end
build_jobs(staged_dir, jobs = []) click to toggle source

Unimplemented method for building jobs.

@param [String] staged_dir The staged directory path. @param [Array, Nil] jobs An array of jobs to be built.

@raise [NotImplementedError] The method is currently not implemented

# File lib/osc_machete_rails/workflow.rb, line 146
def build_jobs(staged_dir, jobs = [])
  raise NotImplementedError, "Objects including OSC::Machete::SimpleJob::Workflow must implement build_jobs"
end
delete_staging() click to toggle source

Deletes the staged directory if it exists

# File lib/osc_machete_rails/workflow.rb, line 106
def delete_staging
  FileUtils.rm_rf(staged_dir) if respond_to?(:staged_dir) && staged_dir
end
ood_dataroot() click to toggle source
# File lib/osc_machete_rails/workflow.rb, line 44
def ood_dataroot
  if defined? OodAppkit
    OodAppkit.dataroot
  elsif ENV['OOD_DATAROOT'] || ENV['RAILS_DATAROOT']
    Pathname.new(ENV['OOD_DATAROOT'] || ENV['RAILS_DATAROOT'])
  else
    raise "no ood_dataroot specified"
  end
end
render_mustache_files(staged_dir, template_view) click to toggle source

Creates a new location and renders the mustache files

@param [String] staged_dir The staging target directory path. @param [Hash] template_view The template options to be rendered.

@return [Location] The location of the staged and rendered template.

# File lib/osc_machete_rails/workflow.rb, line 130
def render_mustache_files(staged_dir, template_view)
  OSC::Machete::Location.new(staged_dir).render(template_view)
end
save_jobs(jobs, staged_dir) click to toggle source

Saves a Hash of jobs to a staged directory

@param [Hash] jobs A Hash of Job objects to be saved. @param [Location] staged_dir The staged directory as Location object.

# File lib/osc_machete_rails/workflow.rb, line 192
def save_jobs(jobs, staged_dir)
  self.staged_dir = staged_dir.to_s if self.respond_to?(:staged_dir=)
  self.save if self.id.nil? || self.respond_to?(:staged_dir=)

  jobs.each do |job|
    self.jobs_active_record_relation.create(job: job)
  end
end
stage() click to toggle source

Creates a new staging target job directory on the system Copies the staging template directory to the staging target job directory

@return [Pathname] The staged directory path.

# File lib/osc_machete_rails/workflow.rb, line 97
def stage
  staged_dir = OSC::Machete::JobDir.new(staging_target_dir).new_jobdir
  FileUtils.mkdir_p staged_dir
  FileUtils.cp_r staging_template_dir.to_s + "/.", staged_dir

  staged_dir
end
staging_target_dir(dataroot: ood_dataroot) click to toggle source

Gets the staging target directory path. Joins the AwesimRails.dataroot and the staging target directory name.

@raise [Exception] “override staging_target_dir or include awesim_rails gem”

@return [String] The staging target directory path.

# File lib/osc_machete_rails/workflow.rb, line 81
def staging_target_dir(dataroot: ood_dataroot)
  Pathname.new(dataroot).join(staging_target_dir_name)
end
staging_target_dir_name() click to toggle source

Returns the name of a staging directory that has been underscored and pluralized.

@example

Simulation => simulations

@example

FlowratePerformanceRun => flowrate_performance_runs

@return [String] The staging template directory name

# File lib/osc_machete_rails/workflow.rb, line 71
def staging_target_dir_name
  staging_template_name.pluralize
end
staging_template_dir() click to toggle source

Gets the staging template directory path. Joins the { rails root }/jobs/{ staging_template_name } into a path.

@return [String] The staging template directory path.

# File lib/osc_machete_rails/workflow.rb, line 89
def staging_template_dir
  Rails.root.join("jobs", staging_template_name)
end
staging_template_name() click to toggle source

@example Underscore a class

FlowratePerformanceRun => flowrate_performance_run

@return [String] The template name

# File lib/osc_machete_rails/workflow.rb, line 59
def staging_template_name
  self.class.name.underscore
end
stop() click to toggle source

Stops all jobs, updating each active job to status “failed” returns true if all jobs were stopped, false otherwise

# File lib/osc_machete_rails/workflow.rb, line 112
def stop
  jobs_active_record_relation.to_a.each(&:stop)

  true
rescue PBS::Error => e
  msg = "A PBS::Error occurred when trying to stop jobs for simulation #{id}: #{e.message}"
  errors[:base] << msg
  Rails.logger.error(msg)

  false
end
stop_machete_jobs(jobs) click to toggle source

given an array of OSC::Machete::Job objects, qdel them all and handle any errors. not to be confused with stop which stops all actual jobs of the workflow

# File lib/osc_machete_rails/workflow.rb, line 176
def stop_machete_jobs(jobs)
  jobs.each do |job|
    begin
      job.delete
    rescue PBS::Error
      msg = "A PBS::Error occurred when deleting a job from the batch system with pbsid: #{job.pbsid} and message: #{e.message}"
      errors[:base] << msg
      Rails.logger.error(msg)
    end
  end
end
submit(template_view=self) click to toggle source

Perform the submit actions.

Sets the staged_dir Renders the mustache files. Calls after_stage. Calls build_jobs. Submits the jobs. Saves the jobs.

@param [Hash, nil] template_view (self) The template options to be rendered.

# File lib/osc_machete_rails/workflow.rb, line 211
def submit(template_view=self)
  staged_dir = stage
  render_mustache_files(staged_dir, template_view)
  after_stage(staged_dir)
  jobs = build_jobs(staged_dir)
  if submit_jobs(jobs)
    save_jobs(jobs, staged_dir)
  else
    FileUtils.rm_rf staged_dir.to_s
    false
  end
end
submit_jobs(jobs) click to toggle source

Call the submit method on each job in a hash.

@param [Hash] jobs A Hash of Job objects to be submitted.

# File lib/osc_machete_rails/workflow.rb, line 153
def submit_jobs(jobs)
  jobs.each(&:submit)
  true
rescue OSC::Machete::Job::ScriptMissingError => e
  stop_machete_jobs(jobs)

  msg = "A OSC::Machete::Job::ScriptMissingError occurred when submitting jobs for simulation #{id}: #{e.message}"
  errors[:base] << msg
  Rails.logger.error(msg)
  false
rescue PBS::Error => e
  stop_machete_jobs(jobs)

  msg = "A PBS::Error occurred when submitting jobs for simulation #{id}: #{e.message}"
  errors[:base] << msg
  Rails.logger.error(msg)

  false
end