Module: WorkflowRESTClient

Defined in:
lib/rbbt/workflow/remote/rest/get.rb,
lib/rbbt/workflow/remote/rest/adaptor.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.execute_job(base_url, task, task_params, cache_type) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/rbbt/workflow/remote/rest/adaptor.rb', line 85

def self.execute_job(base_url, task, task_params, cache_type)
  self.capture_exception do
    task_url = URI.encode(File.join(base_url, task.to_s))

    sout, sin = Misc.pipe

    post_thread = Thread.new(Thread.current) do |parent|
      bl = lambda do |rok|
        if Net::HTTPOK === rok
          _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
        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 = @adaptor.parse_exception text
          case ne
          when String
            parent.raise e.class, ne
          when Exception
            parent.raise ne
          else
            parent.raise "Error in RestClient: " << rok.message
          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}" }
      RestClient::Request.execute(:method => :post, :url => task_url, :payload => task_params, :block_response => bl)
    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

Instance Method Details

#_cleanObject



231
232
233
234
235
236
237
238
239
# File 'lib/rbbt/workflow/remote/rest/get.rb', line 231

def _clean
  begin
    _restart
    params = {:_update => :clean}
    @adaptor.clean_url(url, params) if @url
  rescue Exception
    Log.exception $!
  end
end

#_run_job(cache_type = :async) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/rbbt/workflow/remote/rest/get.rb', line 289

def _run_job(cache_type = :async)
  get_streams

  task_url = URI.encode(File.join(base_url, task.to_s))
  @adaptor.__prepare_inputs_for_restclient(inputs)
  task_params = inputs.merge(:_cache_type => cache_type, :jobname => base_name, :_format => [:string, :boolean, :tsv, :annotations].include?(result_type) ? :raw : :json)

  if cache_type == :stream or cache_type == :exec and stream_input and inputs[stream_input]
    io =  self.stream_job(task_url, task_params, stream_input, cache_type) 
    return io
  else
    @adaptor.execute_job(base_url, task, task_params, cache_type)
  end

end

#abortObject



192
193
194
195
196
# File 'lib/rbbt/workflow/remote/rest/get.rb', line 192

def abort
  return self if status == :done
  @adaptor.get_json(@url + '?_update=abort') if @url and @name
  self
end

#cleanObject



241
242
243
244
245
# File 'lib/rbbt/workflow/remote/rest/get.rb', line 241

def clean
  init_job
  _clean
  self
end

#exec_jobObject



187
188
189
190
# File 'lib/rbbt/workflow/remote/rest/get.rb', line 187

def exec_job
  res = _run_job(:exec)
  load_res res, result_type == :array ? :json : result_type
end

#getObject



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/rbbt/workflow/remote/rest/get.rb', line 166

def get
  params ||= {}
  params = params.merge(:_format => [:string, :boolean, :tsv, :annotations,:array].include?(result_type.to_sym) ? :raw : :json )
  Misc.insist 3, rand(2) + 1 do
    begin
      init_job if url.nil?
      @adaptor.get_raw(url, params)
    rescue
      Log.exception $!
      raise $!
    end
  end
end

#init_job(cache_type = nil, other_params = {}) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/rbbt/workflow/remote/rest/get.rb', line 198

def init_job(cache_type = nil, other_params = {})
  cache_type = :asynchronous if cache_type.nil? and not @is_exec
  cache_type = :exec if cache_type.nil?
  @last_info_time = nil
  @done = false
  get_streams
  @name ||= Persist.memory("RemoteSteps", :workflow => self, :task => task, :jobname => @name, :inputs => inputs, :cache_type => cache_type) do
    Misc.insist do
      @adaptor.post_jobname(File.join(base_url, task.to_s), inputs.merge(other_params).merge(:jobname => @name||@base_name, :_cache_type => cache_type))
    end
  end
  if Open.remote? @name
    @url = @name
    @name = File.basename(@name)
  else
    @url = File.join(base_url, task.to_s, @name)
  end
  self
end

#loadObject



180
181
182
183
184
185
# File 'lib/rbbt/workflow/remote/rest/get.rb', line 180

def load
  params = {}
  join unless done? or streaming?
  raise get_exception if error? or aborted?
  load_res get
end

#recursive_cleanObject



218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/rbbt/workflow/remote/rest/get.rb', line 218

def recursive_clean
  Log.warn "Not doing recursive cleans"
  return
  begin
    _restart
    params = {:_update => :recursive_clean}
    @adaptor.get_raw(url, params)
  rescue Exception
    Log.exception $!
  end
  self
end

#stream_job(task_url, task_params, stream_input, cache_type = :exec) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/rbbt/workflow/remote/rest/get.rb', line 247

def stream_job(task_url, task_params, stream_input, cache_type = :exec)
  require 'rbbt/util/misc/multipart_payload'
  WorkflowRESTClient.capture_exception do
    @streaming = true

    Log.debug{ "RestClient stream #{Process.pid}: #{ task_url } #{stream_input} #{cache_type} - #{Misc.fingerprint task_params}" }
    res = RbbtMutiplartPayload.issue task_url, task_params, stream_input, nil, nil, true
    type = res.gets

    out = case type.strip
          when "LOCATION"
            @url = res.gets
            @url.sub!(/\?.*/,'')
            join
            WorkflowRESTClient.get_raw(@url)
            @done = true
            @streaming = false
          when /STREAM: (.*)/
            @url = $1.strip
            res.callback = Proc.new do
              Log.medium "Done streaming result from #{@url}"
              @done = true
              @streaming = false
            end
            res
          when "BULK"
            begin
              res.read
            ensure
              @done = true
              @streaming = false
            end
          else
            raise "What? " + type
          end

    ConcurrentStream.setup(out, :filename => @url)

    out
  end
end