diff options
author | Martin Gütlein <martin.guetlein@gmail.com> | 2010-04-23 14:20:40 +0200 |
---|---|---|
committer | Martin Gütlein <martin.guetlein@gmail.com> | 2010-04-23 14:20:40 +0200 |
commit | ad974c419a2efe4c0ceaa1f3e2346906b5783e52 (patch) | |
tree | 5c98d81de01a79f39ff9b01d991e9f6041802004 /lib/task.rb | |
parent | 1926e43b9e2716e7f7248fe183a4d43da1209a1b (diff) |
adjusted task to api, new owl
Diffstat (limited to 'lib/task.rb')
-rw-r--r-- | lib/task.rb | 144 |
1 files changed, 76 insertions, 68 deletions
diff --git a/lib/task.rb b/lib/task.rb index 29fb4fe..dee8a86 100644 --- a/lib/task.rb +++ b/lib/task.rb @@ -4,96 +4,105 @@ module OpenTox class Task - attr_accessor :uri + TASK_ATTRIBS = [ :uri, :date, :title, :creator, :title, :description, :hasStatus, :percentageCompleted, :resultURI ] + TASK_ATTRIBS.each{ |a| attr_accessor(a) } - def initialize(uri) - @uri = uri.chomp - end - - def self.create - resource = RestClient::Resource.new(@@config[:services]["opentox-task"], :user => @@users[:users].keys[0], :password => @@users[:users].values[0]) - uri = resource.post({}).chomp - Task.new(uri.chomp) + private + def initialize(uri) + @uri = uri + end + + public + def self.create + task_uri = RestClientWrapper.post(@@config[:services]["opentox-task"], nil, nil, false).to_s + Task.find(task_uri.chomp) end def self.find(uri) - Task.new(uri) - end - - def self.base_uri - @@config[:services]["opentox-task"] - end - - def self.all - task_uris = RestClient.get(@@config[:services]["opentox-task"]).chomp.split(/\n/) - task_uris.collect{|uri| Task.new(uri)} - end - - def created_at - RestClient.get File.join(@uri, 'created_at') - end - - def finished_at - RestClient.get File.join(@uri, 'finished_at') - end - - def description - RestClient.get File.join(@uri, 'description') + task = Task.new(uri) + task.reload + return task end - def status - RestClient.get File.join(@uri, 'status') - end - - def resource - RestClient.get File.join(@uri, 'resource') - end - - def started - LOGGER.info File.join(@uri,'started') - resource = RestClient::Resource.new(File.join(@uri,'started'), :user => @@users[:users].keys[0], :password => @@users[:users].values[0]) - resource.put({}) - end + def self.from_data(data, content_type, base_uri) + begin + task = Task.new(nil) + task.reload_from_data(data, content_type, base_uri) + return task + rescue + return nil + end + end + + def reload + result = RestClientWrapper.get(uri) + reload_from_data(result, result.content_type) + end + + def reload_from_data( data=nil, content_type=nil, base_uri=nil ) + case content_type + when /text\/x-yaml/ + task = YAML.load data + raise "yaml data is no task" if task.is_a?(Task) + TASK_ATTRIBS.each{ |a| send("#{a.to_s}=".to_sym,task[a]) } + when /application\/rdf\+xml/ + base_uri = uri unless base_uri + owl = OpenTox::Owl.from_data(data,base_uri) + raise "not a task" if owl.ot_class=="Task" + TASK_ATTRIBS.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 + end + + + # invalid: getters in task.rb should work for non-internal tasks as well + # + #def self.base_uri + # @@config[:services]["opentox-task"] + #end + #def self.all + # task_uris = RestClientWrapper.get(@@config[:services]["opentox-task"]).chomp.split(/\n/) + # task_uris.collect{|uri| Task.new(uri)} + #end def cancel - resource = RestClient::Resource.new(File.join(@uri,'cancelled'), :user => @@users[:users].keys[0], :password => @@users[:users].values[0]) - resource.put({}) + RestClientWrapper.put(File.join(@uri,'Cancelled')) + reload end def completed(uri) - resource = RestClient::Resource.new(File.join(@uri,'completed'), :user => @@users[:users].keys[0], :password => @@users[:users].values[0]) - resource.put :resource => uri + RestClientWrapper.put(File.join(@uri,'Completed'),{:resultURI => uri}) + reload end - def failed(description) - resource = RestClient::Resource.new(File.join(@uri,'failed'), :user => @@users[:users].keys[0], :password => @@users[:users].values[0]) - resource.put :description => description - #resource.put({}) + def error(description) + RestClientWrapper.put(File.join(@uri,'Error'),{:description => description}) + reload end def parent=(task) - #RestClient.put File.join(@uri,'parent'), {:uri => task.uri} - resource = RestClient::Resource.new(File.join(@uri,'parent'), :user => @@users[:users].keys[0], :password => @@users[:users].values[0]) - LOGGER.debug "task.rb: #{resource}" - resource.put :uri => task.uri + RestClientWrapper.put(File.join(@uri,'parent'), {:uri => task.uri}) + reload end def pid=(pid) - resource = RestClient::Resource.new(File.join(@uri,'pid'), :user => @@users[:users].keys[0], :password => @@users[:users].values[0]) - resource.put :pid => pid + RestClientWrapper.put(File.join(@uri,'pid'), {:pid => pid}) + reload end def completed? - self.status.to_s == 'completed' + @hasStatus.to_s == 'Completed' end - def failed? - self.status.to_s == 'failed' + def error? + @hasStatus.to_s == 'Error' end def wait_for_completion(dur=0.1) - until self.completed? or self.failed? + until self.completed? or self.error? sleep dur + reload end end @@ -102,29 +111,28 @@ module OpenTox task = OpenTox::Task.create task.parent = parent_task if parent_task - LOGGER.debug "Starting task" pid = Spork.spork(:logger => LOGGER) do - task.started LOGGER.debug "Task #{task.uri} started #{Time.now}" begin result = catch(:halt) do yield task + LOGGER.debug "Task #{task.uri} done #{Time.now}" end if result && result.is_a?(Array) && result.size==2 && result[0]>202 # halted while executing task LOGGER.error "task was halted: "+result.inspect - task.failed(result[1]) + task.error(result[1]) throw :halt,result end task.completed(result) rescue => ex #raise ex LOGGER.error "task failed: "+ex.message - task.failed(ex.message) + task.error(ex.message) end - raise "Invalid task state" unless task.completed? || task.failed? + raise "Invalid task state" unless task.completed? || task.error? end - LOGGER.debug "task PID: " + pid.to_s + LOGGER.debug "Started task with PID: " + pid.to_s task.pid = pid task.uri end |