Class: SimpleWS::Jobs

Inherits:
SimpleWS
  • Object
show all
Defined in:
lib/simplews/jobs.rb

Defined Under Namespace

Modules: Scheduler Classes: Aborted, JobNotFound, Notifier, ResultNotFound

Constant Summary collapse

SLEEP_TIMES =
{
  :job_info => 1,
  :monitor => 2,
}
INHERITED_TASKS =
{}
@@tasks =
{}

Constants inherited from SimpleWS

INHERITED_METHODS

Instance Attribute Summary

Attributes inherited from SimpleWS

#description

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SimpleWS

desc, #desc, #documentation, get_driver, get_wsdl, #name=, param_desc, #param_desc, serve, #serve, #wsdl

Constructor Details

#initialize(name = nil, description = nil, host = nil, port = nil, workdir = nil, *args) ⇒ Jobs


405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/simplews/jobs.rb', line 405

def initialize(name = nil, description = nil, host = nil, port = nil, workdir = nil, *args)
  super(name, description, host, port, *args)

  @workdir = workdir || "/tmp/#{ name }"
  Scheduler.workdir = @workdir
  @results = {}
  INHERITED_TASKS.each{|task,info|
    @@last_description = info[:description]
    @@last_param_description = info[:param_description]
    task(task, info[:params], info[:types], info[:results], &info[:block])
  }


  desc "Job management: Return the names of the jobs in the queue"
  param_desc :return => "Array of job names"
  serve :queue, [], :return => :array do 
    Scheduler.queue.collect{|info| info[:name]}
  end

  desc "Job management: Check the status of a job"
  param_desc :job => "Job identifier", :return => "Status code. Special status codes are: 'queue', 'done', 'error', and 'aborted'"
  serve :status, ['job'], :job => :string, :return => :string do |job|
    Scheduler.job_info(job)[:status].to_s
  end

  desc "Job management: Return an array with the messages issued by the job"
  param_desc :job => "Job identifier", :return => "Array with message strings"
  serve :messages, ['job'], :job => :string, :return => :array do |job|
    Scheduler.job_info(job)[:messages]
  end

  desc "Job management: Return a YAML string containing arbitrary information set up by the job"
  param_desc :job => "Job identifier", :return => "Hash with arbitrary values in YAML format"
  serve :info, ['job'], :job => :string, :return => :string do |job|
    Scheduler.job_info(job)[:info].to_yaml
  end

  desc "Job management: Abort the job"
  param_desc :job => "Job identifier"
  serve :abort, %w(job), :job => :string, :return => false do |job|
    Scheduler.abort(job)
  end

  desc "Job management: Check if the job is done. Could have finished successfully, with error, or have been aborted"
  param_desc :job => "Job identifier", :return => "True if the job has status 'done', 'error' or 'aborted'"
  serve :done, %w(job), :job => :string, :return => :boolean do |job|
    [:done, :error, :aborted].include? Scheduler.job_info(job)[:status].to_sym
  end

  desc "Job management: Check if the job has finished with error. The last message is the error message"
  param_desc :job => "Job identifier", :return => "True if the job has status 'error'"
  serve :error, %w(job), :job => :string, :return => :boolean do |job|
    Scheduler.job_info(job)[:status] == :error
  end

  desc "Job management: Check if the job has been aborted"
  param_desc :job => "Job identifier", :return => "True if the job has status 'aborted'"
  serve :aborted, %w(job), :job => :string, :return => :boolean do |job|
    Scheduler.job_info(job)[:status] == :aborted
  end

  desc "Job management: Return an array with result identifiers to be used with the 'result' operation. The content of the results depends
  on the task"
  param_desc :job => "Job identifier", :return => "Array of result identifiers"
  serve :results, %w(job), :return => :array do |job|
    results = Scheduler.job_results(job)    
    @results.merge! Hash[*results.flatten]
    results.collect{|p| p[0]}
  end

  desc "Job management: Return the content of the result specified by the result identifier. These identifiers are retrieve using the 'results' operation. Results are Base64 encoded to allow binary data"
  param_desc :result => "Result identifier", :return => "Content of the result file, in Base64 encoding for compatibility"
  serve :result, %w(result), :return => :binary do |result|
    path = @results[result]
    raise ResultNotFound if path.nil? || ! File.exist?(path)
    Base64.encode64 File.open(path).read
  end

end

Class Method Details

.configure(name, value) ⇒ Object


369
370
371
# File 'lib/simplews/jobs.rb', line 369

def self.configure(name, value)
  Scheduler.configure(name, value)
end

.helper(name, &block) ⇒ Object


361
362
363
# File 'lib/simplews/jobs.rb', line 361

def self.helper(name, &block)
  Scheduler.helper name, block
end

.task(name, params = [], types = {}, results = [], &block) ⇒ Object


388
389
390
391
392
393
394
# File 'lib/simplews/jobs.rb', line 388

def self.task(name, params=[], types={}, results =[], &block)
  INHERITED_TASKS[name] = {:params => params, :types => types, :results => results, :block => block,
  :description => @@last_description, :param_description => @@last_param_description};

  @@last_description = nil
  @@last_param_description = nil
end

Instance Method Details

#abort_jobsObject


396
397
398
# File 'lib/simplews/jobs.rb', line 396

def abort_jobs
  Scheduler.abort_jobs
end

#configure(name, value) ⇒ Object


373
374
375
# File 'lib/simplews/jobs.rb', line 373

def configure(name, value)
  self.class.configure(name, value)
end

#helper(name, &block) ⇒ Object


365
366
367
# File 'lib/simplews/jobs.rb', line 365

def helper(name,&block)
  Scheduler.helper name, block
end

#old_shutdownObject


491
# File 'lib/simplews/jobs.rb', line 491

alias_method :old_shutdown, :shutdown

#old_startObject


485
# File 'lib/simplews/jobs.rb', line 485

alias_method :old_start, :start

#shutdown(*args) ⇒ Object


492
493
494
495
# File 'lib/simplews/jobs.rb', line 492

def shutdown(*args)
  Scheduler.abort_jobs
  old_shutdown(*args)
end

#start(*args) ⇒ Object


486
487
488
489
# File 'lib/simplews/jobs.rb', line 486

def start(*args)
  Scheduler.job_monitor
  old_start(*args)
end

#task(name, params = [], types = {}, results = [], &block) ⇒ Object


377
378
379
380
381
382
383
384
385
# File 'lib/simplews/jobs.rb', line 377

def task(name, params=[], types={}, results = [], &block)
  @@last_param_description['return'] ||= 'Job identifier' if @@last_param_description
  @@last_param_description['suggested_name'] ||= 'Suggested job id' if @@last_param_description
  
  Scheduler.task name, results, block
  serve name.to_s, params + ['suggested_name'], types.merge(:suggested_name => 'string', :return => :string) do |*args|
    Scheduler.run name, *args
  end
end

#workdirObject


401
402
403
# File 'lib/simplews/jobs.rb', line 401

def workdir
  @workdir
end