Module: RemoteWorkflow::REST
- Defined in:
- lib/rbbt/workflow/remote_workflow/driver/rest.rb
Class Method Summary collapse
- .__prepare_inputs_for_restclient(inputs) ⇒ Object
- .clean_url(url, params = {}) ⇒ Object
- .encode(url) ⇒ Object
- .escape_url(url) ⇒ Object
- .execute_job(base_url, task, task_params, cache_type) ⇒ Object
- .get_json(url, params = {}) ⇒ Object
- .get_raw(url, params = {}) ⇒ Object
- .post_jobname(url, params = {}) ⇒ Object
- .post_json(url, params = {}) ⇒ Object
- .task_info(url, task) ⇒ Object
Instance Method Summary collapse
Class Method Details
.__prepare_inputs_for_restclient(inputs) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/rbbt/workflow/remote_workflow/driver/rest.rb', line 5 def self.__prepare_inputs_for_restclient(inputs) inputs.each do |k,v| if v.respond_to? :path and not v.respond_to? :original_filename class << v def original_filename File.(path) end end end if Array === v and v.empty? inputs[k] = "EMPTY_ARRAY" end end end |
.clean_url(url, params = {}) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rbbt/workflow/remote_workflow/driver/rest.rb', line 47 def self.clean_url(url, params = {}) params = params.merge({ :_format => 'json', :update => 'clean' }) params = RemoteWorkflow.fix_params params res = RemoteWorkflow.capture_exception do Misc.insist(2, 0.5) do Log.debug{ "RestClient clean: #{ url } - #{Misc.fingerprint params}" } res = begin RestClient.get(self.encode(url), :params => params) rescue RestClient::NotFound return nil end raise TryAgain if res.code == 202 res end end res end |
.encode(url) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rbbt/workflow/remote_workflow/driver/rest.rb', line 36 def self.encode(url) # ToDo: Check this return escape_url(url) begin URI::DEFAULT_PARSER.escape(url) rescue Log.warn $!. url end end |
.escape_url(url) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rbbt/workflow/remote_workflow/driver/rest.rb', line 21 def self.escape_url(url) base, _sep, query = url.partition("?") protocol, path = base.split("://") path = protocol if path.nil? path = path.split("/").collect{|p| CGI.escape(p) }* "/" base = protocol ? [protocol, path] * "://" : path if query && ! query.empty? query = query.split("&").collect{|e| e.split("=").collect{|pe| CGI.escape(pe) } * "=" } * "&" [base, query] * "?" else base end end |
.execute_job(base_url, task, task_params, cache_type) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/rbbt/workflow/remote_workflow/driver/rest.rb', line 153 def self.execute_job(base_url, task, task_params, cache_type) RemoteWorkflow.capture_exception do task_url = self.escape_url(File.join(base_url, task.to_s)) sout, sin = Misc.pipe post_thread = Thread.new(Thread.current) do |parent| bl = lambda do |rok| case rok when Net::HTTPOK _url = rok["RBBT-STREAMING-JOB-URL"] @url = File.join(task_url, File.basename(_url)) if _url rok.read_body do |c,_a, _b| sin.write c end sin.close when Net::HTTPRedirection, Net::HTTPAccepted Thread.current.report_on_exception = false raise TryThis.new(rok) else err = StringIO.new rok.read_body do |c,_a, _b| err.write c end text = begin reader = Zlib::GzipReader.new(err) reader.read rescue err.rewind err.read end ne = RemoteWorkflow.parse_exception text case ne when String parent.raise e.class, ne when Exception parent.raise ne else parent.raise "Error in RestClient: " << rok. end end end task_params.each do |k,v| task_params[k] = v.read if IO === v end Log.debug{ "RestClient execute: #{ task_url } - #{Misc.fingerprint task_params}" } begin RestClient::Request.execute(:method => :post, :url => task_url, :payload => task_params, :block_response => bl) rescue TryThis url = $!.payload["location"] RestClient::Request.execute(:method => :get, :url => url, :block_response => bl) end end # It seems like now response body are now decoded by Net::HTTP after 2.1 # https://github.com/rest-client/rest-client/blob/cf3e5a115bcdb8f3344aeac0e45b44d67fac1a42/history.md decode = Gem.loaded_specs["rest-client"].version < Gem::Version.create('2.1') if decode reader = Zlib::GzipReader.new(sout) res_io = Misc.open_pipe do |sin| while c = reader.read(Misc::BLOCK_SIZE) sin.write c end sin.close @done = true end ConcurrentStream.setup(res_io, :threads => [post_thread]) do @done = true @streaming = false end else ConcurrentStream.setup(sout, :threads => [post_thread]) do @done = true @streaming = false end end end end |
.get_json(url, params = {}) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/rbbt/workflow/remote_workflow/driver/rest.rb', line 80 def self.get_json(url, params = {}) Log.debug{ "RestClient get_json: #{ url } - #{Misc.fingerprint params }" } params = params.merge({ :_format => 'json' }) params = RemoteWorkflow.fix_params params res = RemoteWorkflow.capture_exception do Misc.insist(2, 0.5) do RestClient.get(self.encode(url), :params => params) end end begin JSON.parse(res) rescue res end end |
.get_raw(url, params = {}) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/rbbt/workflow/remote_workflow/driver/rest.rb', line 65 def self.get_raw(url, params = {}) params = params.merge({ :_format => 'raw' }) params = RemoteWorkflow.fix_params params res = RemoteWorkflow.capture_exception do Misc.insist(2, 0.5) do raise "No url" if url.nil? Log.debug{ "RestClient get_raw: #{ url } - #{Misc.fingerprint params}" } res = RestClient.get(self.encode(url), :params => params) raise TryAgain if res.code == 202 res.to_s end end res end |
.post_jobname(url, params = {}) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/rbbt/workflow/remote_workflow/driver/rest.rb', line 98 def self.post_jobname(url, params = {}) Log.debug{ "RestClient post_jobname: #{ url } - #{Misc.fingerprint params}" } params = params.merge({ :_format => 'jobname' }) params = RemoteWorkflow.fix_params params RemoteWorkflow::REST.__prepare_inputs_for_restclient(params) name = RemoteWorkflow.capture_exception do begin RestClient.post(self.encode(url), params) rescue RestClient::MovedPermanently, RestClient::Found, RestClient::TemporaryRedirect raise RbbtException, "REST end-point moved to: #{$!.response.headers[:location]}" end end Log.debug{ "RestClient jobname returned for #{ url } - #{Misc.fingerprint params}: #{name}" } name end |
.post_json(url, params = {}) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/rbbt/workflow/remote_workflow/driver/rest.rb', line 117 def self.post_json(url, params = {}) if url =~ /_cache_type=:exec/ JSON.parse(Open.open(url, :nocache => true)) else params = params.merge({ :_format => 'json' }) params = fix_params params res = RemoteWorkflow.capture_exception do RestClient.post(self.encode(url), params) end begin JSON.parse(res) rescue res end end end |
.task_info(url, task) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/rbbt/workflow/remote_workflow/driver/rest.rb', line 136 def self.task_info(url, task) @@task_info ||= {} key = [url, task] * "#" @@task_info[key] ||= begin task_info = RemoteWorkflow::REST.get_json(File.join(url, task.to_s, 'info')) task_info = RemoteWorkflow.fix_hash(task_info) task_info[:result_type] = task_info[:result_type].to_sym task_info[:export] = task_info[:export].to_sym task_info[:input_types] = RemoteWorkflow.fix_hash(task_info[:input_types], true) task_info[:inputs] = task_info[:inputs].collect{|input| input.to_sym } task_info end end |
Instance Method Details
#init_remote_tasks ⇒ Object
251 252 253 254 255 256 257 258 |
# File 'lib/rbbt/workflow/remote_workflow/driver/rest.rb', line 251 def init_remote_tasks task_exports = IndiferentHash.setup(RemoteWorkflow::REST.get_json(url)) @asynchronous_exports = (task_exports["asynchronous"] || []).collect{|task| task.to_sym } @synchronous_exports = (task_exports["synchronous"] || []).collect{|task| task.to_sym } @exec_exports = (task_exports["exec"] || []).collect{|task| task.to_sym } @stream_exports = (task_exports["stream"] || []).collect{|task| task.to_sym } @can_stream = task_exports["can_stream"] end |
#task_info(task) ⇒ Object
247 248 249 |
# File 'lib/rbbt/workflow/remote_workflow/driver/rest.rb', line 247 def task_info(task) RemoteWorkflow::REST.task_info(url, task) end |
#tasks ⇒ Object
235 236 237 238 239 240 241 242 243 244 |
# File 'lib/rbbt/workflow/remote_workflow/driver/rest.rb', line 235 def tasks @tasks ||= Hash.new do |hash,task_name| info = task_info(task_name) task = Task.setup info do |*args| raise "This is a remote task" end task.name = task_name.to_sym hash[task_name] = task end end |