Class: OpenTox::Task

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

Constant Summary collapse

TASK_ATTRIBS =

due_to_time is only set in local tasks

[ :uri, :date, :title, :creator, :description, :hasStatus, :percentageCompleted, :resultURI, :due_to_time ]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#http_codeObject

Returns the value of attribute http_code.



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

def http_code
  @http_code
end

Class Method Details

.as_task(title, creator, max_duration = DEFAULT_TASK_MAX_DURATION, description = nil) ⇒ Object

returns the task uri catches halts and exceptions, task state is set to error then



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
# File 'lib/task.rb', line 143

def self.as_task( title, creator, max_duration=DEFAULT_TASK_MAX_DURATION, description=nil )
  #return yield nil
  
  params = {:title=>title, :creator=>creator, :max_duration=>max_duration, :description=>description }
  task = OpenTox::Task.create(params)
  task_pid = Spork.spork(:logger => LOGGER) do
    LOGGER.debug "Task #{task.uri} started #{Time.now}"
    $self_task = task
    
    begin
      result = catch(:halt) do
        yield task
      end
      # catching halt, set task state to error
      if result && result.is_a?(Array) && result.size==2 && result[0]>202
        LOGGER.error "task was halted: "+result.inspect
        task.error(result[1])
        return
      end
      LOGGER.debug "Task #{task.uri} done #{Time.now} -> "+result.to_s
      task.completed(result)
    rescue => ex
      LOGGER.error "task failed: "+ex.message
      LOGGER.error ": "+ex.backtrace.join("\n")
      task.error(ex.message)
    end
  end  
  task.pid = task_pid
  LOGGER.debug "Started task: "+task.uri.to_s
  task.uri
end

.find(uri, accept_header = nil) ⇒ Object



24
25
26
27
28
# File 'lib/task.rb', line 24

def self.find( uri, accept_header=nil )
  task = Task.new(uri)
  task.reload( accept_header )
  return task
end

.from_data(data, content_type, code, base_uri) ⇒ Object



30
31
32
33
34
35
# File 'lib/task.rb', line 30

def self.from_data(data, content_type, code, base_uri)
  task = Task.new(nil)
  task.http_code = code
  task.reload_from_data(data, content_type, base_uri)
  return task
end

Instance Method Details

#cancelObject



68
69
70
71
# File 'lib/task.rb', line 68

def cancel
  RestClientWrapper.put(File.join(@uri,'Cancelled'))
  reload
end

#check_stateObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/task.rb', line 124

def check_state
  begin
    raise "illegal task state, task is completed, resultURI is no URI: '"+@resultURI.to_s+
        "'" unless @resultURI and Utils.is_uri?(@resultURI) if completed?
    
    if @http_code == 202
      raise "illegal task state, code is 202, but hasStatus is not Running: '"+@hasStatus+"'" unless running?
    elsif @http_code == 201
      raise "illegal task state, code is 201, but hasStatus is not Completed: '"+@hasStatus+"'" unless completed?
      raise "illegal task state, code is 201, resultURI is no task-URI: '"+@resultURI.to_s+
          "'" unless @resultURI and Utils.task_uri?(@resultURI)
    end
  rescue => ex
    RestClientWrapper.raise_uri_error(ex.message, @uri)
  end
end

#completed(uri) ⇒ Object



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

def completed(uri)
  RestClientWrapper.put(File.join(@uri,'Completed'),{:resultURI => uri})
  reload
end

#completed?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/task.rb', line 91

def completed?
  @hasStatus.to_s == 'Completed'
end

#error(description) ⇒ Object



78
79
80
81
# File 'lib/task.rb', line 78

def error(description)
  RestClientWrapper.put(File.join(@uri,'Error'),{:description => description.to_s[0..2000]})
  reload
end

#error?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/task.rb', line 95

def error?
  @hasStatus.to_s == 'Error'
end

#pid=(pid) ⇒ Object



83
84
85
# File 'lib/task.rb', line 83

def pid=(pid)
  RestClientWrapper.put(File.join(@uri,'pid'), {:pid => pid})
end

#reload(accept_header = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/task.rb', line 37

def reload( accept_header=nil )
  unless accept_header 
    if (@@config[:yaml_hosts].include?(URI.parse(uri).host))
      accept_header = "application/x-yaml"
    else
      accept_header = 'application/rdf+xml'
    end
  end
  result = RestClientWrapper.get(uri, {:accept => accept_header}, false)#'application/x-yaml'})
  @http_code = result.code
  reload_from_data(result, result.content_type, uri)
end

#reload_from_data(data, content_type, base_uri) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/task.rb', line 50

def reload_from_data( data, content_type, base_uri )
  case content_type
  when /yaml/
    task =  YAML.load data
    TASK_ATTRIBS.each do |a|
      raise "task yaml data invalid, key missing: "+a.to_s unless task.has_key?(a)
      send("#{a.to_s}=".to_sym,task[a])
    end
  when /application\/rdf\+xml/
    owl = OpenTox::Owl.from_data(data,base_uri,"Task")
    self.uri = owl.uri
    (TASK_ATTRIBS-[:uri]).each{|a| self.send("#{a.to_s}=".to_sym, owl.get(a.to_s))}
  else
    raise "content type for tasks not supported: "+content_type.to_s
  end
  raise "uri is null after loading" unless @uri and @uri.to_s.strip.size>0
end

#running?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/task.rb', line 87

def running?
  @hasStatus.to_s == 'Running'
end

#wait_for_completion(dur = 0.3) ⇒ Object

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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/task.rb', line 100

def wait_for_completion(dur=0.3)
  
  if (@uri.match(@@config[:services]["opentox-task"]))
    due_to_time = (@due_to_time.is_a?(Time) ? @due_to_time : Time.parse(@due_to_time))
    running_time = due_to_time - (@date.is_a?(Time) ? @date : Time.parse(@date))
  else
    # the date of the external task cannot be trusted, offest to local time might be to big
    due_to_time = Time.new + EXTERNAL_TASK_MAX_DURATION
    running_time = EXTERNAL_TASK_MAX_DURATION
  end
  LOGGER.debug "start waiting for task "+@uri.to_s+" at: "+Time.new.to_s+", waiting at least until "+due_to_time.to_s
  
  while self.running?
    sleep dur
    reload
    check_state
    if (Time.new > due_to_time)
      raise "max wait time exceeded ("+running_time.to_s+"sec), task: '"+@uri.to_s+"'"
    end
  end
  
  LOGGER.debug "Task '"+@hasStatus+"': "+@uri.to_s+", Result: "+@resultURI.to_s
end