Module: WorkflowRESTHelpers

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

#clean_job(workflow, job) ⇒ Object



220
221
222
223
224
225
226
227
228
# File 'lib/rbbt/rest/workflow/jobs.rb', line 220

def clean_job(workflow, job)
  job.clean

  if ajax
    halt 200
  else
    redirect to(File.join("/", workflow.to_s, job.task_name.to_s))
  end
end

#complete_input_set(workflow, task, inputs) ⇒ Object



23
24
25
# File 'lib/rbbt/rest/workflow/jobs.rb', line 23

def complete_input_set(workflow, task, inputs)
  inputs.keys.sort === workflow.task_info(task.to_sym)[:inputs].sort
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
# 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)
    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



41
42
43
44
45
46
# File 'lib/rbbt/rest/workflow/jobs.rb', line 41

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

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



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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/rbbt/rest/workflow/jobs.rb', line 148

def issue_job(workflow, task, jobname = nil, params = {})
  inputs = prepare_job_inputs(workflow, task, params)
  job = workflow.job(task, jobname, inputs)

  job.clean if update == :clean
  job.recursive_clean if update == :recursive_clean

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

    begin
      job_url = to(File.join("/", workflow.to_s, task, job.name)) 
      job_url += "?_format=#{@format}" if @format

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

      #halt 200, job.name if format == :jobname 
      if format == :jobname
        job.name
      else
        redirect job_url
      end
    rescue Exception
      Log.exception $!
      halt 500, $!.message
    end
  when :asynchronous, :async, nil
    if update == :reload
      job.abort
      job.clean 
    end

    begin
      job.fork 
      job.grace

      job_url = to(File.join("/", workflow.to_s, task, job.name)) 
      job_url += "?_format=#{@format}" if @format
      #halt 200, job.name if format == :jobname
      if format == :jobname
        content_type :text
        job.name
      else
        redirect 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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rbbt/rest/workflow/locate.rb', line 27

def locate_workflow_template(template, workflow = nil, task = nil)

  if workflow
    path = locate_workflow_template_from_resource(workflow.libdir.www.views.find, template, workflow, task)
    return path if path and path.exists?
  end

  workflow_resources.each do |resource|
    path = locate_workflow_template_from_resource(resource, template, workflow, task)
    return path if path and path.exists?
  end

  raise "Template not found: [#{ template }, #{workflow}, #{ task }]"
end

#locate_workflow_template_from_resource(resource, template, workflow = nil, task = nil) ⇒ Object



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

def locate_workflow_template_from_resource(resource, template, workflow = nil, task = nil)
  template += '.haml' unless template =~ /.+\..+/

  paths = []
  paths << resource[workflow][task][template] if task and workflow
  paths << resource[workflow][template] if workflow
  paths << resource[template]

  paths.each do |path|
    return path.find if path.exists?
  end 

  nil
end

#prepare_job_inputs(workflow, task, params) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rbbt/rest/workflow/jobs.rb', line 48

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

  task_inputs = {}
  inputs.each do |input|
    value = consume_parameter(input, params)
    param_file = consume_parameter(input.to_s + '__param_file', params)
    next if value.nil? and param_file.nil?
    type = input_types[input]

    fixed_value = fix_input(type, value, param_file)
    task_inputs[input] = fixed_value
  end

  task_inputs
end

#recursive_clean_job(workflow, job) ⇒ Object



211
212
213
214
215
216
217
218
219
# File 'lib/rbbt/rest/workflow/jobs.rb', line 211

def recursive_clean_job(workflow, job)
  job.recursive_clean

  if 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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rbbt/rest/workflow/jobs.rb', line 72

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"
    halt 200, result.to_s
  when :literal, :raw
    content_type "text/plain"
    case workflow.task_info(task)[:result_type]
    when :array
      halt 200, result * "\n"
    else
      halt 200, result.to_s
    end
  when :binary
    content_type "application/octet-stream"
    halt 200, result.to_s
  when :jobname
    halt 200, nil
  else
    raise "Unsupported format: #{ format }"
  end
end

#show_result(job, workflow, task) ⇒ Object



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

def show_result(job, workflow, task)
  case format.to_sym
  when :html
    show_result_html job.load, workflow, task, job.name, job
  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.values.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
    content_type "text/plain"
    job.path ? send_file(job.path) : halt(200, job.load.to_s)
  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)
    send_file excel_file, :type => 'application/vnd.ms-excel', :filename => job.clean_name + '.xls'
  else
    raise "Unsupported format: #{ format }"
  end
end

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



66
67
68
69
70
# File 'lib/rbbt/rest/workflow/jobs.rb', line 66

def show_result_html(result, workflow, task, jobname = nil, job = nil)
  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)
end

#type_of_export(workflow, task) ⇒ Object



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

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)
    :asynchronous
  else
    raise "No known export type for #{ workflow } #{ task }. Accesses denied"
  end
end

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



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rbbt/rest/workflow/render.rb', line 21

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
# 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?

  template_file = locate_workflow_template(template, workflow, task)

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

  layout_file = layout ? locate_template("layout") : nil

  render(template_file, locals, layout_file)
end