Class: OodCore::Job::Adapters::PSIJ

Inherits:
OodCore::Job::Adapter show all
Defined in:
lib/ood_core/job/adapters/psij.rb

Defined Under Namespace

Classes: Batch

Constant Summary collapse

STATE_MAP =
{
  'NEW'           => :undetermined,
  'QUEUED'        => :queued,
  'HELD'          => :queued_held,
  'ACTIVE'        => :running,
  'COMPLETED'     => :completed,
}

Instance Method Summary collapse

Methods inherited from OodCore::Job::Adapter

#info_all_each, #info_historic, #info_where_owner_each, #job_name_illegal_chars, #nodes, #queues, #sanitize_job_name, #supports_job_arrays?

Constructor Details

#initialize(opts = {}) ⇒ PSIJ

Returns a new instance of PSIJ.



122
123
124
125
126
# File 'lib/ood_core/job/adapters/psij.rb', line 122

def initialize(opts = {})
  o = opts.to_h.symbolize_keys

  @psij = o.fetch(:psij) { raise ArgumentError, "No psij object specified. Missing argument: psij" }
end

Instance Method Details

#accountsObject



282
283
# File 'lib/ood_core/job/adapters/psij.rb', line 282

def accounts
end

#cluster_infoObject



279
280
# File 'lib/ood_core/job/adapters/psij.rb', line 279

def cluster_info
end

#delete(id) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
# File 'lib/ood_core/job/adapters/psij.rb', line 285

def delete(id)
  id = id.to_s.strip()
  params = {
    id: id,
    executor: @psij.executor,
  }
  args = params.map { |k, v| "--#{k}=#{v}" }
  @psij.delete_job(args: args)
rescue Batch::Error => e
  raise JobAdapterError, e.message unless /Invalid job id specified/ =~ e.message
end

#directive_prefixObject



367
368
# File 'lib/ood_core/job/adapters/psij.rb', line 367

def directive_prefix
end

#hold(id) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
# File 'lib/ood_core/job/adapters/psij.rb', line 297

def hold(id)
  id = id.to_s.strip()
  params = {
    id: id,
    executor: @psij.executor,
  }
  args = params.map { |k, v| "--#{k}=#{v}" }
  @psij.hold_job(args: args)
rescue Batch::Error => e
  raise JobAdapterError, e.message unless /Invalid job id specified/ =~ e.message
end

#info(id) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/ood_core/job/adapters/psij.rb', line 322

def info(id)
  id = id.to_s

  job_infos = @psij.get_jobs(id: id).map do |v|
    parse_job_info(v)
  end

  if job_infos.empty?
    Info.new(id: id, status: :completed)
  else
    job_infos.first
  end
rescue Batch::Error => e
  # set completed status if can't find job id
  if /Invalid job id specified/ =~ e.message
    Info.new(
      id: id,
      status: :completed
    )
  else
    raise JobAdapterError, e.message
  end
end

#info_all(attrs: nil) ⇒ Object



346
347
348
349
350
351
352
# File 'lib/ood_core/job/adapters/psij.rb', line 346

def info_all(attrs: nil)
  @psij.get_jobs.map do |v|
    parse_job_info(v)
  end
rescue Batch::Error => e
  raise JobAdapterError, e.message
end

#info_where_owner(owner, attrs: nil) ⇒ Object



354
355
356
357
358
359
360
361
# File 'lib/ood_core/job/adapters/psij.rb', line 354

def info_where_owner(owner, attrs: nil)
  owner = Array.wrap(owner).map(&:to_s).join(',')
  @psij.get_jobs(owner: owner).map do |v|
    parse_job_info(v)
  end
rescue Batch::Error => e
  raise JobAdapterError, e.message
end

#release(id) ⇒ Object



309
310
311
312
313
314
315
316
317
318
319
# File 'lib/ood_core/job/adapters/psij.rb', line 309

def release(id)
  id = id.to_s.strip()
  params = {
    id: id,
    executor: @psij.executor,
  }
  args = params.map { |k, v| "--#{k}=#{v}" }
  @psij.release_job(args: args)
rescue Batch::Error => e
  raise JobAdapterError, e.message unless /Invalid job id specified/ =~ e.message
end

#status(id) ⇒ Object



363
364
365
# File 'lib/ood_core/job/adapters/psij.rb', line 363

def status(id)
  info(id.to_s).status
end

#submit(script, after: [], afterok: [], afternotok: [], afterany: []) ⇒ Object

The `submit` method saves a job script as a file and prepares a command to submit the job. Each optional argument specifies job dependencies (after, afterok, afternotok, afterany).



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/ood_core/job/adapters/psij.rb', line 131

def submit(script, after: [], afterok: [], afternotok: [], afterany: [])
  # convert OOD interfaces to PSI/J interfaces.
  # Conterted variables are shown as follows:
  #       OOD           |   PSI/J(JobSpec)
  # --------------------+----------------------------------------------------
  # submit_as_hold      |       X (not support)
  # rerunnable          |       X
  # email_on_started    |       X
  # email_on_terminated |       X
  # args                |   JobAttributes.custom_attributes
  # job_environment     |   environment
  # workdir             |   directory
  # email               |       X
  # job_name            |   name
  # shell_path          |   #!<shell_path>
  # input_path          |   stdin_path
  # output_path         |   stdout_path
  # error_path          |   stderr_path
  # reservation_id      |   JobAttributes.reservation_id
  # queue_name          |   JobAttributes.queue_name
  # priority            |       X
  # start_time          |       X
  # wall_time           |   JobAttributes.duration
  # accounting_id       |   JobAttributes.account or project_name(duplicated)
  # job_array_request   |       X
  # qos                 |       X
  # gpus_per_node       |   ResourceSpec.gpu_cores_per_process
  # native              |   executable (join script.content)
  # copy_environment    |   inherit_envrionment
  # cores               |   ResourceSpec.cpu_cores_per_process
  # after               |       X
  # afterok             |       X
  # afternotok          |       X
  # afterany            |       X
  # OOD does not have following PSI/J's interfaces.
  #   JobSpec class:
  #     pre_launch, post_launch, launcher
  #   ResourceSpec class:
  #     node_count, process_count, processes_per_node, exclusive_node_use

  content = if script.shell_path.nil?
    script.content
  else
    "#!#{script.shell_path}\n#{script.content}"
  end

  if ! script.native.nil?
    native = script.native.join("\n") unless script.native.nil?
    script.content.concat(native)
  end

  relative_path = "~/ood_tmp/run.sh"
  full_path = File.expand_path("~/ood_tmp/run.sh")
  FileUtils.mkdir_p(File.dirname(full_path))
  File.open(full_path, "w") do |file|
    file.write(content)
  end

  File.chmod(0755, full_path)

  # convert OOD interfaces to PSI/J interfaces.
  params = {
    environment: script.job_environment,
    directory: script.workdir,
    name: script.job_name,
    executable: relative_path,
    stdin_path: script.input_path,
    stdout_path: script.output_path,
    stderr_path: script.error_path,
    inherit_environment: script.copy_environment,
    attributes: {queue_name: script.queue_name,
                 reservation_id: script.reservation_id,
                 account: script.accounting_id,
                 duration: script.wall_time,
                 custom_attributes: script.args},
    resources: {__version: 1,
                gpu_cores_per_process: script.gpus_per_node,
                cpu_cores_per_process: script.cores}
  }

  if params[:attributes][:queue_name].nil?
    params[:attributes][:queue_name] = @psij.queue_name
  end
  if params[:stdout_path].nil?
    params[:stdout_path] = File.join(Dir.pwd, "stdout.txt")
  end
  if params[:stderr_path].nil?
    params[:stderr_path] = File.join(Dir.pwd, "stderr.txt")
  end

  # add script.native to params[:attributes][:custom_attributes] of PSI/J.
  if script.native && !script.native.empty?
    if params[:attributes][:custom_attributes].nil?
      params[:attributes][:custom_attributes] = script.native
    else
      params[:attributes][:custom_attributes].concat(script.native)
    end
  end
  # Add script.native to params[:attributes][:cutsom_attributes] of PSI/J.
  # Convert script.native array to hash.
  # ['--<name>', 'value'] -> {name: value}
  # ['--<name1>', '--<name2>'] -> {name1: "", name2: ""}
  if ! params[:attributes][:custom_attributes].nil? 
    hash = {}
    skip = false
    len = params[:attributes][:custom_attributes].length()-1
    for index in 0..len do
      if skip
        skip = false
        next
      end
      v = params[:attributes][:custom_attributes][index]
      has_hyphen = false
      if v.start_with?("--")
        name = v[2..-1]
        has_hyphen = true
      elsif v.start_with?("-")
        name = v[1..-1]
        has_hyphen = true
      else   
        name = v
      end
      if index == len || !has_hyphen || params[:attributes][:custom_attributes][index+1].start_with?("-")
        # if next value is not exist or start with "-", set empty string
        hash[name] = ""
      else
        # if next value is exist and not start with "-", set value
        hash[name] = params[:attributes][:custom_attributes][index+1]
        skip = true
      end
    end
    params[:attributes][:custom_attributes] = hash
  end

  # reject key which has nil value.
  params[:attributes] = params[:attributes].reject {|_, value |value.nil?}
  params[:resources] = params[:resources].reject {|_, value |value.nil?}
  data = params.reject {|_, value |value.nil?}
  
  # serialize params to JSON
  args = []
  args[0] = @psij.executor

  @psij.submit_job_path(args: args, chdir: script.workdir, stdin: JSON.generate(data))
rescue Batch::Error => e
  raise JobAdapterError, e
end