Class: SimpleWS::Jobs

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

Defined Under Namespace

Modules: Scheduler Classes: Aborted, JobNotFound, ResultNotFound

Constant Summary collapse

@@tasks =
{}

Constants inherited from SimpleWS

VERSION

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SimpleWS

get_driver, get_wsdl, #serve, #wsdl

Constructor Details

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

Returns a new instance of Jobs.



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/simplews/jobs.rb', line 355

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

  @workdir = workdir || "/tmp/#{ name }"
  Scheduler.workdir = @workdir
  @results = {}
  @@tasks.each{|task, values|
    serve task.to_s, values[:params] + ['suggested_name'], values[:types].merge(:suggested_name => 'string', :return => :string) do |*args|
      Scheduler.run task, *args
    end
  }

  serve :status, ['job'], :job => :string, :return => :string do |job|
    Scheduler.job_info(job)[:status].to_s
  end

  serve :messages, ['job'], :job => :string, :return => :array do |job|
    Scheduler.job_info(job)[:messages]
  end

  serve :info, ['job'], :job => :string, :return => :string do |job|
    Scheduler.job_info(job)[:info].to_yaml
  end

  serve :abort, %w(job), :job => :string do |job|
    Scheduler.abort(job)
  end

  serve :done, %w(job), :job => :string, :return => :boolean do |job|
    [:done, :error, :aborted].include? Scheduler.job_info(job)[:status].to_sym
  end

  serve :error, %w(job), :job => :string, :return => :boolean do |job|
    Scheduler.job_info(job)[:status] == :error
  end

  serve :aborted, %w(job), :job => :string, :return => :boolean do |job|
    Scheduler.job_info(job)[:status] == :aborted
  end

  serve :results, %w(job), :return => :array do |job|
    results = Scheduler.job_results(job)    
    @results.merge! Hash[*results.flatten]
    results.collect{|p| p[0]}
  end

  serve :result, %w(result), :return => :binary do |result|
    path = @results[result]
    raise ResultNotFound unless File.exist? path
    File.open(path).read
  end

end

Class Method Details

.configure(name, value) ⇒ Object



325
326
327
# File 'lib/simplews/jobs.rb', line 325

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

.helper(name, &block) ⇒ Object



317
318
319
# File 'lib/simplews/jobs.rb', line 317

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

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



341
342
343
344
# File 'lib/simplews/jobs.rb', line 341

def self.task(name, params=[], types={}, results =[], &block)
  Scheduler.task name, results, block
  @@tasks[name] = {:params => params, :types => types};
end

Instance Method Details

#abort_jobsObject



346
347
348
# File 'lib/simplews/jobs.rb', line 346

def abort_jobs
  Scheduler.abort_jobs
end

#configure(name, value) ⇒ Object



329
330
331
# File 'lib/simplews/jobs.rb', line 329

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

#helper(name, &block) ⇒ Object



321
322
323
# File 'lib/simplews/jobs.rb', line 321

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

#old_shutdownObject



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

alias_method :old_shutdown, :shutdown

#old_startObject



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

alias_method :old_start, :start

#shutdown(*args) ⇒ Object



416
417
418
419
# File 'lib/simplews/jobs.rb', line 416

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

#start(*args) ⇒ Object



410
411
412
413
# File 'lib/simplews/jobs.rb', line 410

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

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



333
334
335
336
337
338
# File 'lib/simplews/jobs.rb', line 333

def task(name, params=[], types={}, results = [], &block)
  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



351
352
353
# File 'lib/simplews/jobs.rb', line 351

def workdir
  @workdir
end