diff options
author | Martin Gütlein <martin.guetlein@gmail.com> | 2010-04-21 16:25:57 +0200 |
---|---|---|
committer | Martin Gütlein <martin.guetlein@gmail.com> | 2010-04-21 16:25:57 +0200 |
commit | bc8c59c41bee5283bc67fc56809c865babbed08f (patch) | |
tree | 1c3afec36e592fda19281d2d267e311749dfd367 /lib | |
parent | 7b68e9c00542c383871fc78ec3bb2b0297bba4cd (diff) |
rest client wrapper: changed method signature, added timeout
Diffstat (limited to 'lib')
-rw-r--r-- | lib/environment.rb | 10 | ||||
-rw-r--r-- | lib/model.rb | 8 | ||||
-rw-r--r-- | lib/rest_client_wrapper.rb | 86 | ||||
-rw-r--r-- | lib/task.rb | 2 | ||||
-rw-r--r-- | lib/utils.rb | 5 |
5 files changed, 61 insertions, 50 deletions
diff --git a/lib/environment.rb b/lib/environment.rb index dc3bad9..27235c3 100644 --- a/lib/environment.rb +++ b/lib/environment.rb @@ -60,29 +60,21 @@ class MyLogger < Logger def trace() lines = caller(0) -# puts lines.join("\n") -# puts "-" n = 2 line = lines[n] while (line =~ /spork.rb/ or line =~ /as_task/ or line =~ /environment.rb/) - #puts "skip line "+line.to_s n += 1 line = lines[n] end -# puts line -# puts "-" index = line.rindex(/\/.*\.rb/) -# raise "index = nil" if index==nil return line if index==nil -# puts "<<< "+line[index..-1].size.to_s+" <<< "+line[index..-1] -# raise "stop" line[index..-1] end def format(msg) - pwd.ljust(18)+" :: "+msg.to_s+" :: "+trace + pwd.ljust(18)+" :: "+msg.to_s+" :: "+trace+" :: "+ENV['REMOTE_ADDR'].to_s end def debug(msg) diff --git a/lib/model.rb b/lib/model.rb index 8674311..3aae652 100644 --- a/lib/model.rb +++ b/lib/model.rb @@ -40,16 +40,16 @@ module OpenTox end LOGGER.debug "Build model, algorithm_uri:"+algorithm_uri.to_s+", algorithm_parms: "+algorithm_params.inspect.to_s - uri = OpenTox::RestClientWrapper.post(algorithm_uri,algorithm_params, nil, true).to_s - raise "Invalid build model result: "+uri.to_s unless uri =~ /model/ + uri = OpenTox::RestClientWrapper.post(algorithm_uri,algorithm_params).to_s + RestClientWrapper.illegal_result("Invalid build model result: "+uri.to_s, algorithm_uri, algorithm_params ) unless Utils.model_uri?(uri) return PredictionModel.find(uri) end def predict_dataset( dataset_uri ) LOGGER.debug "Predict dataset: "+dataset_uri.to_s+" with model "+@uri.to_s - uri = RestClientWrapper.post(@uri, {:dataset_uri=>dataset_uri}, nil, true) - raise "Prediciton result no dataset uri: "+uri.to_s unless Utils.dataset_uri?(uri) + uri = RestClientWrapper.post(@uri, {:dataset_uri=>dataset_uri}) + RestClientWrapper.illegal_result("Prediciton result no dataset uri: "+uri.to_s, @uri, {:dataset_uri=>dataset_uri} ) unless Utils.dataset_uri?(uri) uri end diff --git a/lib/rest_client_wrapper.rb b/lib/rest_client_wrapper.rb index 725ac55..b0af8ca 100644 --- a/lib/rest_client_wrapper.rb +++ b/lib/rest_client_wrapper.rb @@ -16,7 +16,7 @@ module OpenTox def self.parse(error_array_string) begin err = YAML.load(error_array_string) - if err.is_a?(Array) and err.size>0 and err[0].is_a?(Error) + if err and err.is_a?(Array) and err.size>0 and err[0].is_a?(Error) return err else return nil @@ -27,7 +27,7 @@ module OpenTox end end - + module RestClientWrapper # PENDING: remove as soon as redirect tasks are remove from partner webservices @@ -53,30 +53,45 @@ module OpenTox return uri end - def self.get(uri, headers=nil, wait_for_task=false, curl_hack=false) - execute( "get", uri, nil, headers, wait_for_task, curl_hack ) + def self.get(uri, headers=nil, curl_hack=false) + execute( "get", uri, headers, nil, curl_hack ) end - def self.post(uri, payload=nil, headers=nil, wait_for_task=false, curl_hack=false) - execute( "post", uri, payload, headers, wait_for_task, curl_hack ) + def self.post(uri, headers, payload=nil, curl_hack=false) + raise "payload and headers switched" if payload.is_a?(Hash) and headers==nil + raise "illegal headers" unless headers==nil || headers.is_a?(Hash) + execute( "post", uri, headers, payload, curl_hack ) end - def self.delete(uri, headers=nil, wait_for_task=false, curl_hack=false) - execute( "delete", uri, nil, headers, wait_for_task, curl_hack ) + def self.delete(uri, headers=nil, curl_hack=false) + execute( "delete", uri, headers, nil, curl_hack ) end + def self.illegal_result(error_msg, uri, headers, payload=nil) + do_halt( "-", error_msg, uri, headers, payload ) + end + private - def self.execute( rest_call, uri, payload, headers, wait_for_task=false, curl_hack=false ) + def self.execute( rest_call, uri, headers, payload=nil, curl_hack=false ) + + do_halt 400,"uri is null",uri,headers,payload unless uri + do_halt 400,"not an uri",uri,headers,payload unless Utils.is_uri?(uri) + do_halt 400,"headers are no hash",uri,headers,payload unless headers==nil or headers.is_a?(Hash) + headers.each{ |k,v| headers.delete(k) if v==nil } if headers #remove keys with empty values, as this can cause problems - do_halt 400,"uri is null",uri,payload,headers unless uri begin - - payload.each{ |k,v| payload.delete(k) if v==nil } if payload #remove keys with empty values, as this can cause problems unless curl_hack + + LOGGER.debug "RestCall: "+rest_call.to_s+" "+uri.to_s+" "+headers.inspect + resource = RestClient::Resource.new(uri, :timeout => 60) if payload - result = RestClient.send(rest_call, uri, payload, headers).to_s - else - result = RestClient.send(rest_call, uri, headers).to_s + result = resource.send(rest_call, payload, headers).to_s + #result = RestClient.send(rest_call, uri, payload, headers).to_s + elsif headers + #result = RestClient.send(rest_call, uri, headers).to_s + result = resource.send(rest_call, headers).to_s + else + result = resource.send(rest_call).to_s end else result = "" @@ -93,23 +108,22 @@ module OpenTox #raise "STOP "+result end - if wait_for_task - if result.to_s =~ /ambit.*task|tu-muenchen.*task/ - result = redirect_task(result) - elsif Utils.task_uri?(result) - task = OpenTox::Task.find(result) - task.wait_for_completion - raise task.description if task.failed? - result = task.resource - end + if result.to_s =~ /ambit.*task|tu-muenchen.*task/ + result = redirect_task(result) + elsif Utils.task_uri?(result) + task = OpenTox::Task.find(result) + task.wait_for_completion + raise task.description if task.failed? + result = task.resource end return result rescue RestClient::RequestFailed => ex - do_halt ex.http_code,ex.http_body,uri,payload,headers + do_halt ex.http_code,ex.http_body,uri,headers,payload rescue RestClient::RequestTimeout => ex - do_halt 408,ex.message,uri,payload,headers + do_halt 408,ex.message,uri,headers,payload rescue => ex + #raise ex begin code = ex.http_code msg = ex.http_body @@ -117,11 +131,11 @@ module OpenTox code = 500 msg = ex.to_s end - do_halt code,msg,uri,payload,headers + do_halt code,msg,uri,headers,payload end end - def self.do_halt( code, body, uri, payload, headers ) + def self.do_halt( code, body, uri, headers, payload=nil ) #build error causing_errors = Error.parse(body) @@ -132,14 +146,14 @@ module OpenTox end # debug utility: write error to file -# error_dir = "/tmp/ot_errors" -# FileUtils.mkdir(error_dir) unless File.exist?(error_dir) -# raise "could not create error dir" unless File.exist?(error_dir) and File.directory?(error_dir) -# file_name = "error" -# time=Time.now.strftime("%m.%d.%Y-%H:%M:%S") -# count = 1 -# count+=1 while File.exist?(File.join(error_dir,file_name+"_"+time+"_"+count.to_s)) -# File.new(File.join(error_dir,file_name+"_"+time+"_"+count.to_s),"w").puts(body) + #error_dir = "/tmp/ot_errors" + #FileUtils.mkdir(error_dir) unless File.exist?(error_dir) + #raise "could not create error dir" unless File.exist?(error_dir) and File.directory?(error_dir) + #file_name = "error" + #time=Time.now.strftime("%m.%d.%Y-%H:%M:%S") + #count = 1 + #count+=1 while File.exist?(File.join(error_dir,file_name+"_"+time+"_"+count.to_s)) + #File.new(File.join(error_dir,file_name+"_"+time+"_"+count.to_s),"w").puts(body) # return error (by halting, halts should be logged) # PENDING always return yaml for now diff --git a/lib/task.rb b/lib/task.rb index 8b1ab5d..29fb4fe 100644 --- a/lib/task.rb +++ b/lib/task.rb @@ -98,7 +98,7 @@ module OpenTox end def self.as_task(parent_task=nil) - #return yield + #return yield nil task = OpenTox::Task.create task.parent = parent_task if parent_task diff --git a/lib/utils.rb b/lib/utils.rb index fa61a28..629404c 100644 --- a/lib/utils.rb +++ b/lib/utils.rb @@ -13,6 +13,11 @@ module OpenTox def self.dataset_uri?(uri) is_uri?(uri) && uri.to_s =~ /dataset/ end + + def self.model_uri?(uri) + is_uri?(uri) && uri.to_s =~ /model/ + end + def self.is_uri?(uri) begin |