Class: WorkflowRESTClient::RemoteStep
- Inherits:
-
Step
- Object
- Step
- WorkflowRESTClient::RemoteStep
show all
- Defined in:
- lib/rbbt/rest/client/run.rb,
lib/rbbt/rest/client/step.rb
Constant Summary
Constants inherited
from Step
Step::INFO_SERIALIAZER, Step::STREAM_CACHE, Step::STREAM_CACHE_MUTEX
Instance Attribute Summary collapse
Attributes inherited from Step
#bindings, #dependencies, #dupped, #mutex, #pid, #result, #saved_stream, #seen, #stream, #workflow
Class Method Summary
collapse
Instance Method Summary
collapse
-
#_restart ⇒ Object
-
#_run_job(cache_type = :async) ⇒ Object
-
#abort ⇒ Object
-
#clean ⇒ Object
-
#clean_name ⇒ Object
-
#done? ⇒ Boolean
-
#dup_inputs ⇒ Object
-
#exec(noload = false) ⇒ Object
-
#exec_job ⇒ Object
-
#execute_job(task_url, task_params, cache_type) ⇒ Object
-
#file(file) ⇒ Object
-
#files ⇒ Object
-
#fork(noload = false, semaphore = nil) ⇒ Object
-
#get ⇒ Object
-
#get_stream ⇒ Object
-
#get_streams ⇒ Object
-
#grace ⇒ Object
-
#info(check_lock = false) ⇒ Object
-
#init_job(cache_type = nil, other_params = {}) ⇒ Object
-
#initialize(base_url, task = nil, base_name = nil, inputs = nil, result_type = nil, result_description = nil, is_exec = false, is_stream = false, stream_input = nil) ⇒ RemoteStep
constructor
A new instance of RemoteStep.
-
#join ⇒ Object
-
#load ⇒ Object
-
#load_res(res, result_type = nil) ⇒ Object
-
#name ⇒ Object
-
#name=(name) ⇒ Object
-
#nopid? ⇒ Boolean
-
#path ⇒ Object
-
#recursive_clean ⇒ Object
-
#run(no_load = false) ⇒ Object
-
#running? ⇒ Boolean
-
#started? ⇒ Boolean
-
#status ⇒ Object
-
#stream_job(task_url, task_params, stream_input, cache_type = :exec) ⇒ Object
-
#task_name ⇒ Object
Methods inherited from Step
#_abort, #_clean_finished, #_exec, #abort_pid, #abort_stream, #aborted?, #checks, #child, clean, #dependency_checks, #dirty?, dup_stream, #error?, #exception, #execute_and_dup, #execute_dependency, files_dir, #files_dir, #get_exception, info_file, #info_file, #info_lock, #init_info, #input_checks, job_name_for_info_file, #join_stream, #kill_children, #load_file, #load_inputs_from_info, #log, log, log_block, #log_dependency_exec, log_progress, #log_progress, log_string, #merge_info, #message, #messages, #missing?, #pid_file, pid_file, prepare_for_execution, #prepare_result, #produce, #progress_bar, #provenance, #provenance_paths, purge_stream_cache, #rec_dependencies, #recoverable_error?, #recursive_inputs, #relay_log, #resolve_input_steps, #rewind_inputs, #run_compute_dependencies, #run_dependencies, #save_file, #set_info, #short_path, #soft_grace, #stalled?, #status=, status_color, #step, step_info, #stop_dependencies, #streaming?, #update, #updated?, wait_for_jobs, #waiting?
Constructor Details
#initialize(base_url, task = nil, base_name = nil, inputs = nil, result_type = nil, result_description = nil, is_exec = false, is_stream = false, stream_input = nil) ⇒ RemoteStep
Returns a new instance of RemoteStep.
23
24
25
26
27
|
# File 'lib/rbbt/rest/client/step.rb', line 23
def initialize(base_url, task = nil, base_name = nil, inputs = nil, result_type = nil, result_description = nil, is_exec = false, is_stream = false, stream_input = nil)
@base_url, @task, @base_name, @inputs, @result_type, @result_description, @is_exec, @is_stream, @stream_input = base_url, task, base_name, inputs, result_type, result_description, is_exec, is_stream, stream_input
@base_url = "http://" << @base_url unless @base_url =~ /^https?:\/\//
@mutex = Mutex.new
end
|
Instance Attribute Details
#base_name ⇒ Object
Returns the value of attribute base_name.
21
22
23
|
# File 'lib/rbbt/rest/client/step.rb', line 21
def base_name
@base_name
end
|
#base_url ⇒ Object
Returns the value of attribute base_url.
21
22
23
|
# File 'lib/rbbt/rest/client/step.rb', line 21
def base_url
@base_url
end
|
Returns the value of attribute inputs.
21
22
23
|
# File 'lib/rbbt/rest/client/step.rb', line 21
def inputs
@inputs
end
|
#is_exec ⇒ Object
Returns the value of attribute is_exec.
21
22
23
|
# File 'lib/rbbt/rest/client/step.rb', line 21
def is_exec
@is_exec
end
|
#is_stream ⇒ Object
Returns the value of attribute is_stream.
21
22
23
|
# File 'lib/rbbt/rest/client/step.rb', line 21
def is_stream
@is_stream
end
|
#result_description ⇒ Object
Returns the value of attribute result_description.
21
22
23
|
# File 'lib/rbbt/rest/client/step.rb', line 21
def result_description
@result_description
end
|
#result_type ⇒ Object
Returns the value of attribute result_type.
21
22
23
|
# File 'lib/rbbt/rest/client/step.rb', line 21
def result_type
@result_type
end
|
Returns the value of attribute stream_input.
21
22
23
|
# File 'lib/rbbt/rest/client/step.rb', line 21
def stream_input
@stream_input
end
|
#task ⇒ Object
Returns the value of attribute task.
21
22
23
|
# File 'lib/rbbt/rest/client/step.rb', line 21
def task
@task
end
|
#url ⇒ Object
Returns the value of attribute url.
21
22
23
|
# File 'lib/rbbt/rest/client/step.rb', line 21
def url
@url
end
|
Class Method Details
.get_streams(inputs, stream_input = nil) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/rbbt/rest/client/step.rb', line 60
def self.get_streams(inputs, stream_input = nil)
new_inputs = {}
inputs.each do |k,v|
stream = stream_input.to_s == k.to_s
if Step === v
unless (v.done? or v.streaming?) v.run(true) and v.grace
end
begin
if stream
new_inputs[k] = TSV.get_stream(v)
else
new_inputs[k] = v.load
end
rescue Exception
raise $!
end
else
new_inputs[k] = v
end
end
new_inputs
end
|
Instance Method Details
#_restart ⇒ Object
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
# File 'lib/rbbt/rest/client/step.rb', line 335
def _restart
@done = nil
@name = nil
@started = nil
@aborted = nil
new_inputs = {}
inputs.each do |k,i|
if File === i
new_inputs[k] = File.open(i.path)
else
new_inputs[k] = i
end
end
@inputs = new_inputs
end
|
#_run_job(cache_type = :async) ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/rbbt/rest/client/run.rb', line 85
def _run_job(cache_type = :async)
get_streams
task_url = URI.encode(File.join(base_url, task.to_s))
WorkflowRESTClient.__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
execute_job(task_url, task_params, cache_type)
end
end
|
#abort ⇒ Object
92
93
94
95
96
|
# File 'lib/rbbt/rest/client/step.rb', line 92
def abort
return self if status == :done
WorkflowRESTClient.get_json(@url + '?_update=abort') if @url and @name
self
end
|
#clean ⇒ Object
364
365
366
367
368
369
370
371
372
373
374
|
# File 'lib/rbbt/rest/client/step.rb', line 364
def clean
init_job
begin
params = {:_update => :clean}
WorkflowRESTClient.clean_url(url, params) if @url
_restart
rescue Exception
Log.exception $!
end
self
end
|
#clean_name ⇒ Object
29
30
31
|
# File 'lib/rbbt/rest/client/step.rb', line 29
def clean_name
@base_name
end
|
#done? ⇒ Boolean
162
163
164
|
# File 'lib/rbbt/rest/client/step.rb', line 162
def done?
@done || status.to_s == 'done'
end
|
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/rbbt/rest/client/step.rb', line 98
def dup_inputs
return if @dupped or ENV["RBBT_NO_STREAM"] == 'true'
Log.low "Dupping inputs for #{path}"
dupped_inputs = {}
@inputs.collect do |k,input|
dupped_inputs[k] = Step.dup_stream input
end
@inputs = dupped_inputs
@dupped = true
end
|
#exec(noload = false) ⇒ Object
230
231
232
233
234
235
236
237
238
239
240
|
# File 'lib/rbbt/rest/client/step.rb', line 230
def exec(noload = false)
@result ||= begin
if noload == :stream
_run_job(:exec)
else
exec_job
end
ensure
@started = true
end
end
|
#exec_job ⇒ Object
330
331
332
333
|
# File 'lib/rbbt/rest/client/step.rb', line 330
def exec_job
res = _run_job(:exec)
load_res res, result_type == :array ? :json : result_type
end
|
#execute_job(task_url, task_params, cache_type) ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/rbbt/rest/client/run.rb', line 45
def execute_job(task_url, task_params, cache_type)
WorkflowRESTClient.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
parent.raise "Error in RestClient: " << rok.message
end
end
Log.debug{ "RestClient execute: #{ url } - #{Misc.fingerprint task_params}" }
RestClient::Request.execute(:method => :post, :url => task_url, :payload => task_params, :block_response => bl)
end
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
end
end
|
#file(file) ⇒ Object
170
171
172
|
# File 'lib/rbbt/rest/client/step.rb', line 170
def file(file)
WorkflowRESTClient.get_raw(File.join(url, 'file', file))
end
|
#fork(noload = false, semaphore = nil) ⇒ Object
214
215
216
|
# File 'lib/rbbt/rest/client/step.rb', line 214
def fork(noload=false, semaphore=nil)
init_job(:asynchronous)
end
|
#get ⇒ Object
264
265
266
267
268
269
270
271
272
273
274
275
276
|
# File 'lib/rbbt/rest/client/step.rb', line 264
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?
WorkflowRESTClient.get_raw(url, params)
rescue
Log.exception $!
raise $!
end
end
end
|
#get_stream ⇒ Object
174
175
176
177
178
179
180
181
182
183
|
# File 'lib/rbbt/rest/client/step.rb', line 174
def get_stream
case @result
when IO
@result
when String
StringIO.new @result
else
nil
end
end
|
#get_streams ⇒ Object
85
86
87
88
89
90
|
# File 'lib/rbbt/rest/client/step.rb', line 85
def get_streams
return if @inputs_done
@inputs = WorkflowRESTClient::RemoteStep.get_streams @inputs, @stream_input
@inputs_done = true
@inputs
end
|
#grace ⇒ Object
185
186
187
188
189
190
191
192
193
|
# File 'lib/rbbt/rest/client/step.rb', line 185
def grace
produce unless @started
sleep 0.1 unless started?
sleep 0.5 unless started?
sleep 1 unless started?
while not (done? or started?)
sleep 1
end
end
|
#info(check_lock = false) ⇒ Object
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/rbbt/rest/client/step.rb', line 129
def info(check_lock=false)
@done = @info && @info[:status] && @info[:status].to_sym == :done
@info = Persist.memory("RemoteSteps Info", :url => @url, :persist => true, :update => !@done) do
init_job unless @url
info = begin
WorkflowRESTClient.get_json(File.join(@url, 'info'))
rescue
clean
raise $!
end
info = WorkflowRESTClient.fix_hash(info)
info[:status] = info[:status].to_sym if String === info[:status]
info
end
end
|
#init_job(cache_type = nil, other_params = {}) ⇒ Object
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
# File 'lib/rbbt/rest/client/step.rb', line 197
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?
get_streams
@name ||= Persist.memory("RemoteSteps", :workflow => self, :task => task, :jobname => @name, :inputs => inputs, :cache_type => cache_type) do
WorkflowRESTClient.post_jobname(File.join(base_url, task.to_s), inputs.merge(other_params).merge(:jobname => @name||@base_name, :_cache_type => cache_type))
end
if Open.remote? @name
@url = @name
@name = File.basename(@name)
else
@url = File.join(base_url, task.to_s, @name)
end
self
end
|
#join ⇒ Object
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
# File 'lib/rbbt/rest/client/step.rb', line 242
def join
init_job unless @url
Log.debug{ "Joining RestClient: #{path}" }
if IO === @result
res = @result
@result = nil
Misc.consume_stream(res, true)
end
if not (self.done? || self.aborted? || self.error?)
self.info
return self if self.done? || self.aborted? || self.error?
sleep 0.2 unless self.done? || self.aborted? || self.error?
sleep 1 unless self.done? || self.aborted? || self.error?
while not (self.done? || self.aborted? || self.error?)
sleep 3
end
end
self
end
|
#load ⇒ Object
323
324
325
326
327
328
|
# File 'lib/rbbt/rest/client/step.rb', line 323
def load
params = {}
join unless done? or streaming?
raise get_exception if error? or aborted?
load_res get
end
|
#load_res(res, result_type = nil) ⇒ Object
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
# File 'lib/rbbt/rest/client/step.rb', line 278
def load_res(res, result_type = nil)
stream = true if res.respond_to? :read
join unless stream
result_type ||= self.result_type
case result_type
when :string
stream ? res.read : res
when :boolean
(stream ? res.read : res) == 'true'
when :tsv
if stream
TSV.open(res, :monitor => true)
else
TSV.open(StringIO.new(res))
end
when :annotations
if stream
Annotated.load_tsv(TSV.open(res))
else
Annotated.load_tsv(TSV.open(StringIO.new(res)))
end
when :array
(stream ? res.read : res).split("\n")
res.split("\n")
else
json_text = if IO === res
res.read
else
res
end
begin
JSON.parse json_text
rescue
case
when json_text =~ /^\d+$/
json_text.to_i
when json_text =~ /^\d+\.\d/
json_text.to_f
else
raise $!
end
end
end
end
|
#name ⇒ Object
109
110
111
112
113
|
# File 'lib/rbbt/rest/client/step.rb', line 109
def name
return nil if @is_exec
return @path if @url.nil?
(Array === @url ? @url.first : @url).split("/").last
end
|
#name=(name) ⇒ Object
115
116
117
|
# File 'lib/rbbt/rest/client/step.rb', line 115
def name=(name)
@url = [base_url,task, name] * "/"
end
|
#nopid? ⇒ Boolean
125
126
127
|
# File 'lib/rbbt/rest/client/step.rb', line 125
def nopid?
false
end
|
#path ⇒ Object
222
223
224
225
226
227
228
|
# File 'lib/rbbt/rest/client/step.rb', line 222
def path
if @url
Misc.add_GET_param(@url, "_format", "raw")
else
[base_url, task, @base_name + '-' + Misc.fingerprint(inputs)] * "/"
end
end
|
#recursive_clean ⇒ Object
351
352
353
354
355
356
357
358
359
360
361
362
|
# File 'lib/rbbt/rest/client/step.rb', line 351
def recursive_clean
return
begin
params = {:_update => :recursive_clean}
init_job(nil, params)
WorkflowRESTClient.get_raw(url, params)
_restart
rescue Exception
Log.exception $!
end
self
end
|
#run(no_load = false) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/rbbt/rest/client/step.rb', line 33
def run(no_load = false)
no_load = @is_stream ? :stream : true if no_load
@mutex.synchronize do
@result ||= begin
if @is_exec
exec(no_load)
elsif no_load == :stream
_run_job(:stream)
elsif no_load
init_job
nil
else
init_job
join
self.load
end
ensure
@started = true
end
end
return @result if no_load == :stream
no_load ? Misc.add_GET_param(path, "_format", "raw") : @result
end
|
#running? ⇒ Boolean
218
219
220
|
# File 'lib/rbbt/rest/client/step.rb', line 218
def running?
! %w(done error aborted).include? status.to_s
end
|
#started? ⇒ Boolean
158
159
160
|
# File 'lib/rbbt/rest/client/step.rb', line 158
def started?
@result != nil or @started or @streaming or @url
end
|
#status ⇒ Object
145
146
147
148
149
150
151
152
153
154
155
156
|
# File 'lib/rbbt/rest/client/step.rb', line 145
def status
return :done if @done
return nil unless url or started?
return :streaming if @streaming
begin
status = info[:status]
@done = true if status and status.to_sym == :done
status
ensure
@info = nil
end
end
|
#stream_job(task_url, task_params, stream_input, cache_type = :exec) ⇒ Object
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/rbbt/rest/client/run.rb', line 3
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
|
#task_name ⇒ Object
119
120
121
122
123
|
# File 'lib/rbbt/rest/client/step.rb', line 119
def task_name
return task if task
init_job
(Array === @url ? @url.first : @url).split("/")[-2]
end
|