Module: WorkflowRESTHelpers

Included in:
StreamWorkflowTask
Defined in:
lib/rbbt/rest/workflow/jobs.rb,
lib/rbbt/rest/workflow/locate.rb,
lib/rbbt/rest/workflow/render.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#workflow_resourcesObject

Returns the value of attribute workflow_resources.



2
3
4
# File 'lib/rbbt/rest/workflow/locate.rb', line 2

def workflow_resources
  @workflow_resources
end

Class Method Details

.workflow_resourcesObject



4
5
6
# File 'lib/rbbt/rest/workflow/locate.rb', line 4

def self.workflow_resources
  @@workflow_resources ||= [Rbbt.share.views.find(:lib)]
end

Instance Method Details

#abort_job(workflow, job) ⇒ Object



303
304
305
306
# File 'lib/rbbt/rest/workflow/jobs.rb', line 303

def abort_job(workflow, job)
  job.abort
  halt 200, "Aborted #{ job.path }"
end

#clean_job(workflow, job) ⇒ Object



309
310
311
312
313
314
315
316
317
318
319
# File 'lib/rbbt/rest/workflow/jobs.rb', line 309

def clean_job(workflow, job)
  job.clean

  if format == :jobname
    halt 200, job.name
  elsif ajax or format == :json
    halt 200
  else
    redirect to(File.join("/", workflow.to_s, job.task_name.to_s))
  end
end

#complete_input_set(workflow, task, inputs) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rbbt/rest/workflow/jobs.rb', line 26

def complete_input_set(workflow, task, inputs)
  given = []
  inputs.each do |key,value|
    next if value.nil?
    given << key.to_s
  end
  given = given.sort

  taken = (workflow.task_info(task.to_sym)[:inputs].collect{|i| i.to_s} + ['jobname']).uniq.sort 

  given === taken 
end

#consume_task_parameters(workflow, task, params = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rbbt/rest/workflow/jobs.rb', line 6

def consume_task_parameters(workflow, task, params = nil)
  task_inputs = workflow.task_info(task.to_sym)[:inputs]

  task_parameters = {}
  task_inputs.each do |input|

    input_val = consume_parameter(input, params)
    input_val = input_val.strip if String === input_val 
    input_val = [] if input_val == "EMPTY_ARRAY"

    task_parameters[input] = input_val  unless input_val.nil?

    # Param files
    input_val = consume_parameter(input.to_s + '__param_file', params)
    task_parameters[input.to_s + '__param_file'] = input_val unless input_val.nil?
  end

  task_parameters
end

#execution_type(workflow, task) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/rbbt/rest/workflow/jobs.rb', line 53

def execution_type(workflow, task)
  export = type_of_export(workflow, task)
  return cache_type if cache_type
  return :sync if export == :exec and format == :html
  return export if export == :exec 
  return :asynchronous
end

#issue_job(workflow, task, jobname = nil, params = {}) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
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
288
289
# File 'lib/rbbt/rest/workflow/jobs.rb', line 216

def issue_job(workflow, task, jobname = nil, params = {})
  execution_type = execution_type(workflow, task)

  inputs = prepare_job_inputs(workflow, task, params)
  job = workflow.job(task, jobname, inputs)

  job.clean if job.aborted?

  clean_job(workflow, job) and clean = true if update.to_s == "clean"
  recursive_clean_job(workflow, job) and clean = true if update.to_s == "recursive_clean"

  case execution_type.to_sym
  when :exec
    show_exec_result job.exec(:stream), workflow, task
  when :stream
    if update == :reload
      job.abort
      job.clean 
    end

    job_url = File.join("/", workflow.to_s, task, job.name)

    stream_job(job, job_url)

  when :synchronous, :sync
    if update == :reload
      job.abort
      job.clean 
    end

    begin
      if not job.started?
        job.run
        job.join
      end

      if format == :jobname
        job.name
      else
        job_url = job.respond_to?(:url)? job.url : File.join("/", workflow.to_s, task, job.name)
        job_url += "?_format=#{@format}" if @format
        redirect to(job_url)
      end
    rescue Exception
      Log.exception $!
      halt 500, $!.message
    end
  when :asynchronous, :async, nil
    if update == :reload
      job.abort
      job.clean 
    end

    begin
      # $rest_cache_semaphore is defined in rbbt-util etc/app.d/semaphores.rb
      job.fork($rest_cache_semaphore) unless job.started?

      if format == :jobname
        job.soft_grace
        content_type :text
        job.name
      else
        job.soft_grace
        job_url = job.respond_to?(:url)? job.url : File.join("/", workflow.to_s, task, job.name)
        job_url += "?_format=#{@format}" if @format
        redirect to(job_url)
      end
    rescue Exception
      Log.exception $!
    end
  else
    raise "Unsupported execution_type: #{ execution_type }"
  end
end

#locate_workflow_template(template, workflow = nil, task = nil) ⇒ Object

Raises:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rbbt/rest/workflow/locate.rb', line 12

def locate_workflow_template(template, workflow = nil, task = nil)
  resources = workflow_resources
  resources.unshift workflow.libdir.www.views if workflow

  paths = [template]
  paths.unshift [workflow.to_s, template.to_s]*"/" if workflow 
  paths.unshift [workflow.to_s, task.to_s, template.to_s]*"/" if workflow and task

  path = nil
  paths.each do |location|
    path ||= locate_server_file(location, resources, 'haml')
  end

  raise TemplateMissing, "Template not found: [#{ template }, #{workflow}, #{ task }]" if path.nil?

  path
end

#prepare_job_inputs(workflow, task, params) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rbbt/rest/workflow/jobs.rb', line 61

def prepare_job_inputs(workflow, task, params)
  inputs = workflow.task_info(task)[:inputs]
  input_types = workflow.task_info(task)[:input_types]
  input_options = workflow.task_info(task)[:input_options]

  task_inputs = {}
  inputs.each do |input|
    stream = input_options.include?(input) && input_options[input][:stream]
    value = prepare_input(params, input, input_types[input], stream)
    next if value.nil?
    task_inputs[input] = value
  end
 
  task_inputs
end

#recursive_clean_job(workflow, job) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
# File 'lib/rbbt/rest/workflow/jobs.rb', line 291

def recursive_clean_job(workflow, job)
  job.recursive_clean

  if format == :jobname
    halt 200, job.name
  elsif ajax
    halt 200
  else
    redirect to(File.join("/", workflow.to_s, job.task_name.to_s))
  end
end

#show_exec_result(result, workflow, task) ⇒ Object



84
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
# File 'lib/rbbt/rest/workflow/jobs.rb', line 84

def show_exec_result(result, workflow, task)
  case format.to_sym
  when :html
    show_result_html result, workflow, task, nil
  when :json
    content_type "application/json"
    halt 200, result.to_json
  when :tsv
    content_type "text/tab-separated-values"
    result = result.to_s unless String === result or result.respond_to? :gets
    halt 200, result
  when :literal, :raw
    content_type "text/plain"
    case workflow.task_info(task)[:result_type]
    when :array
      halt 200, result * "\n"
    else
      result = result.to_s unless String === result or result.respond_to? :gets
      halt 200, result
    end
  when :binary
    content_type "application/octet-stream"
    result = result.to_s unless String === result or result.respond_to? :gets
    halt 200, result.to_s
  when :jobname
    halt 200, nil
  else
    raise "Unsupported format: #{ format }"
  end
end

#show_result(job, workflow, task, params = nil) ⇒ Object



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
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
# File 'lib/rbbt/rest/workflow/jobs.rb', line 115

def show_result(job, workflow, task, params = nil)
  return show_result_html nil, workflow, task, job.name, job, params if @fragment

  case format.to_sym
  when :html
    show_result_html :load, workflow, task, job.name, job, params
  when :table
    halt 200, tsv2html(job.path, :url => "/" << [workflow.to_s, task, job.name] * "/")
  when :entities
    tsv = tsv_process(load_tsv(job.path).first)
    list = tsv.column_values(tsv.fields.first).flatten
    if not AnnotatedArray === list and Annotated === list.first
      list.first.annotate list 
      list.extend AnnotatedArray
    end
    type = list.annotation_types.last
    list_id = "TMP #{type} in #{ [workflow.to_s, task, job.name] * " - " }"
    Entity::List.save_list(type.to_s, list_id, list, user)
    redirect to(Entity::REST.entity_list_url(list_id, type))
  when :map
    tsv = tsv_process(load_tsv(job.path).first)
    type = tsv.keys.annotation_types.last
    column = tsv.fields.first
    map_id = "MAP #{type}-#{column} in #{ [workflow.to_s, task, job.name] * " - " }"
    Entity::Map.save_map(type.to_s, column, map_id, tsv, user)
    redirect to(Entity::REST.entity_map_url(map_id, type, column))
  when :json
    content_type "application/json"
    halt 200, job.load.to_json
  when :tsv
    content_type "text/tab-separated-values"
    job.path ? send_file(job.path) : halt(200, job.load.to_s)
  when :literal, :raw
    path = job.path
    mime = file_mimetype path
    content_type mime || "text/plain"
    if job.path
      if Open.remote? job.path
        Open.open(job.path, :nocache => true)
      elsif File.exists? job.path
        send_file(job.path)
      else
        halt 200, job.load
      end
    else
      halt(200, job.load.to_s)
    end
  when :binary
    content_type "application/octet-stream"
    job.path ? send_file(job.path) : halt(200, job.load.to_s)
  when :excel
    require 'rbbt/tsv/excel'
    data = nil
    excel_file = TmpFile.tmp_file
    result = job.load
    result.excel(excel_file, :name => @excel_use_name,:sort_by => @excel_sort_by, :sort_by_cast => @excel_sort_by_cast, :remove_links => true)
    send_file excel_file, :type => 'application/vnd.ms-excel', :filename => job.clean_name + '.xls'
  when :heatmap 
    tsv = job.load
    content_type "text/html"
    data = nil
    png_file = TmpFile.tmp_file
    width = tsv.fields.length * 10 + 500
    height = tsv.size * 10 + 500
    width = 10000 if width > 10000
    height = 10000 if height > 10000
    tsv.R <<-EOF
      rbbt.pheatmap(file='#{png_file}', data, width=#{width}, height=#{height})
    EOF
    send_file png_file, :type => 'image/png', :filename => job.name + ".heatmap.png"
  else
    raise "Unsupported format: #{ format }"
  end
end

#show_result_html(result, workflow, task, jobname = nil, job = nil, params = nil) ⇒ Object



77
78
79
80
81
82
# File 'lib/rbbt/rest/workflow/jobs.rb', line 77

def show_result_html(result, workflow, task, jobname = nil, job = nil, params = nil)
  params ||= {}
  result_type = workflow.task_info(task)[:result_type]
  result_description = workflow.task_info(task)[:result_description]
  workflow_render('job_result', workflow, task, {:result => result, :type => result_type, :description => result_description, :jobname => jobname, :job => job}.merge(params))
end

#stream_job(job, job_url = nil) ⇒ Object



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
# File 'lib/rbbt/rest/workflow/jobs.rb', line 190

def stream_job(job, job_url = nil)
  job.clean if job.recoverable_error?

  unless job.started? or job.done?
    job.fork(:stream) 
    job.soft_grace
  end

  raise "Error in #{job.path}: " + job.messages.last if job.error?

  s = TSV.get_stream job

  sout, sin = Misc.pipe

  Misc.consume_stream(s, true, sin)

  headers "RBBT-STREAMING-JOB-URL" => to(job_url) if job_url

  ConcurrentStream.setup(sout, :pair => s, :autojoin => true) do
    job.abort unless job.done?
  end


  halt 200, sout
end

#type_of_export(workflow, task) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rbbt/rest/workflow/jobs.rb', line 39

def type_of_export(workflow, task)
  task = task.to_sym
  case
  when workflow.exec_exports.include?(task)
    :exec
  when workflow.synchronous_exports.include?(task)
    :synchronous
  when (workflow.asynchronous_exports.include?(task) or workflow.stream_exports.include?(task))
    :asynchronous
  else
    raise "Access denied: no known export type for #{ workflow }##{ task }."
  end
end

#workflow_partial(template, workflow = nil, task = nil, params = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rbbt/rest/workflow/render.rb', line 45

def workflow_partial(template, workflow = nil, task = nil, params = {})
  workflow = consume_parameter(:workflow, params) if workflow.nil?
  task     = consume_parameter(:task, params) if workflow.nil?

  template_file = locate_workflow_template(template, workflow, task)

  locals = params.dup
  locals[:workflow] = workflow if workflow
  locals[:task]     = task if task

  render_partial(template_file, locals)
end

#workflow_render(template, workflow = nil, task = nil, params = {}) ⇒ Object



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/workflow/render.rb', line 6

def workflow_render(template, workflow = nil, task = nil, params = {})
  workflow = consume_parameter(:workflow, params) if workflow.nil?
  task     = consume_parameter(:task, params) if workflow.nil?
  job      = consume_parameter(:job, params) if job.nil?

  template_file = locate_workflow_template(template, workflow, task)

  locals = params.dup
  locals[:workflow] = workflow if workflow
  locals[:task]     = task if task

  if layout
    layout_file = workflow.libdir.www.views[workflow.to_s]["layout.haml"] if workflow.libdir
    layout_file = locate_template("layout") unless layout_file and layout_file.exists?
  else
    layout_file = nil
  end

  if job 
    locals[:job] = job 
    @step = job
    @title = [[workflow.to_s,task] * "#", job.clean_name] * " "
    cache_type = execution_type(workflow, task)
    server_key = $app_name
    html_dir = job.file('.html')
    other_params = params.dup
    other_params.delete_if{|k,v| k[0] == "_"}
    other_params.delete :result
    other_params[:template_file] = template_file
    cache_file = html_dir[server_key + "_" << Misc.obj2digest(other_params)]
    cache_type = false if params[:cache] == FalseClass
    render(template_file, locals, layout_file, [task,workflow,job.name] * "-", :cache_type => cache_type, :cache_file => cache_file)
  else
    cache_type = :async
    cache_type = false if params[:cache] == FalseClass
    render(template_file, locals, layout_file, [workflow.to_s, task, template_file.to_s].compact * "-", :cache_type => :async )
  end
end