Module: OscMacheteRails::Workflow::BuilderMethods

Defined in:
lib/osc_machete_rails/workflow.rb

Overview

depends on jobs_active_record_relation being defined

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(obj) ⇒ Object

Methods run when this module is included



33
34
35
36
37
38
39
40
41
42
# 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

Instance Method Details

#after_stage(staged_dir) ⇒ Object

Actions to perform after staging

Parameters:

  • staged_dir (String)

    The staged directory path.



137
138
# File 'lib/osc_machete_rails/workflow.rb', line 137

def after_stage(staged_dir)
end

#build_jobs(staged_dir, jobs = []) ⇒ Object

Unimplemented method for building jobs.

Parameters:

  • staged_dir (String)

    The staged directory path.

  • jobs (Array, Nil) (defaults to: [])

    An array of jobs to be built.

Raises:

  • (NotImplementedError)

    The method is currently not implemented



146
147
148
# 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_stagingObject

Deletes the staged directory if it exists



106
107
108
# 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_datarootObject



44
45
46
47
48
49
50
51
52
# 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) ⇒ Location

Creates a new location and renders the mustache files

Parameters:

  • staged_dir (String)

    The staging target directory path.

  • template_view (Hash)

    The template options to be rendered.

Returns:

  • (Location)

    The location of the staged and rendered template.



130
131
132
# 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) ⇒ Object

Saves a Hash of jobs to a staged directory

Parameters:

  • jobs (Hash)

    A Hash of Job objects to be saved.

  • staged_dir (Location)

    The staged directory as Location object.



192
193
194
195
196
197
198
199
# 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

#stagePathname

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

Returns:

  • (Pathname)

    The staged directory path.



97
98
99
100
101
102
103
# 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) ⇒ String

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

Returns:

  • (String)

    The staging target directory path.

Raises:

  • (Exception)

    “override staging_target_dir or include awesim_rails gem”



81
82
83
# 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_nameString

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

Examples:

Simulation => simulations
FlowratePerformanceRun => flowrate_performance_runs

Returns:

  • (String)

    The staging template directory name



71
72
73
# File 'lib/osc_machete_rails/workflow.rb', line 71

def staging_target_dir_name
  staging_template_name.pluralize
end

#staging_template_dirString

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

Returns:

  • (String)

    The staging template directory path.



89
90
91
# File 'lib/osc_machete_rails/workflow.rb', line 89

def staging_template_dir
  Rails.root.join("jobs", staging_template_name)
end

#staging_template_nameString

Returns The template name.

Examples:

Underscore a class

FlowratePerformanceRun => flowrate_performance_run

Returns:

  • (String)

    The template name



59
60
61
# File 'lib/osc_machete_rails/workflow.rb', line 59

def staging_template_name
  self.class.name.underscore
end

#stopObject

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



112
113
114
115
116
117
118
119
120
121
122
# 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) ⇒ Object

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



176
177
178
179
180
181
182
183
184
185
186
# 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) ⇒ Object

Perform the submit actions.

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

Parameters:

  • template_view (Hash, nil) (defaults to: self)

    (self) The template options to be rendered.



211
212
213
214
215
216
217
218
219
220
221
222
# 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) ⇒ Object

Call the #submit method on each job in a hash.

Parameters:

  • jobs (Hash)

    A Hash of Job objects to be submitted.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# 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