Class: OpenTox::Task

Inherits:
Object show all
Defined in:
lib/task.rb

Overview

Class for handling asynchronous tasks

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#observer_pidObject

Returns the value of attribute observer_pid


7
8
9
# File 'lib/task.rb', line 7

def observer_pid
  @observer_pid
end

#pidObject

Returns the value of attribute pid


7
8
9
# File 'lib/task.rb', line 7

def pid
  @pid
end

Class Method Details

.run(description, creator = nil) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/task.rb', line 13

def self.run(description, creator=nil)

  task = Task.new nil
  task[RDF::OT.created_at] = DateTime.now
  task[RDF::OT.hasStatus] = "Running"
  task[RDF::DC.description] = description.to_s
  task[RDF::DC.creator] = creator.to_s
  task.put
  pid = fork do
    begin
      task.completed yield
    rescue => e
      # wrap non-opentox-errors first
      e = OpenTox::Error.new(500,e.message,nil,e.backtrace) unless e.is_a?(OpenTox::Error)
      $logger.error "error in task #{task.uri} created by #{creator}" # creator is not logged because error is logged when thrown
      RestClientWrapper.put(File.join(task.uri,'Error'),{:errorReport => e.to_ntriples},{:content_type => 'text/plain'})
      task.kill
    end
  end
  Process.detach(pid)
  task.pid = pid

  # watch if task has been cancelled 
  observer_pid = fork do
    task.wait
    begin
      Process.kill(9,task.pid) if task.cancelled?
    rescue
      $logger.warn "Could not kill process of task #{task.uri}, pid: #{task.pid}"
    end
  end
  Process.detach(observer_pid)
  task.observer_pid = observer_pid
  task

end

Instance Method Details

#cancelObject


64
65
66
67
68
69
# File 'lib/task.rb', line 64

def cancel
  kill
  self.[]=(RDF::OT.hasStatus, "Cancelled")
  self.[]=(RDF::OT.finished_at, DateTime.now)
  put
end

#completed(uri) ⇒ Object


71
72
73
74
75
76
# File 'lib/task.rb', line 71

def completed(uri)
  self.[]=(RDF::OT.resultURI, uri)
  self.[]=(RDF::OT.hasStatus, "Completed")
  self.[]=(RDF::OT.finished_at, DateTime.now)
  put
end

#creatorObject


60
61
62
# File 'lib/task.rb', line 60

def creator
  self.[](RDF::DC.creator)
end

#descriptionObject


56
57
58
# File 'lib/task.rb', line 56

def description
  self.[](RDF::DC.description)
end

#killObject


50
51
52
53
54
# File 'lib/task.rb', line 50

def kill
  Process.kill(9,@pid)
  Process.kill(9,@observer_pid)
rescue # no need to raise an exception if processes are not running
end

#metadataObject


9
10
11
# File 'lib/task.rb', line 9

def 
  super true # always update metadata
end

#waitObject

waits for a task, unless time exceeds or state is no longer running


79
80
81
82
83
84
85
86
87
88
# File 'lib/task.rb', line 79

def wait
  start_time = Time.new
  due_to_time = start_time + DEFAULT_TASK_MAX_DURATION
  dur = 0.2
  while running? 
    sleep dur
    dur = [[(Time.new - start_time)/20.0,0.3].max,300.0].min
    request_timeout_error "max wait time exceeded ("+DEFAULT_TASK_MAX_DURATION.to_s+"sec), task: '"+@uri.to_s+"'" if (Time.new > due_to_time)
  end
end