Module: Sinatra::RbbtRESTWorkflow

Defined in:
lib/rbbt/rest/workflow.rb

Constant Summary collapse

WORKFLOWS =
[]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.registered(base) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/rbbt/rest/workflow.rb', line 335

def self.registered(base)
  base.module_eval do
    helpers WorkflowRESTHelpers

    if ENV["RBBT_WORKFLOW_TASK_STREAM"] == 'true'
      require 'rbbt/rest/workflow/stream_task'
      Log.info "Preparing server for streaming workflow tasks"
      use StreamWorkflowTask if not @can_stream
      @can_stream = true
    else
      @can_stream = false
    end
  end
end

Instance Method Details

#add_workflow(workflow, add_resource = false) ⇒ Object



31
32
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
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
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
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
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
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
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
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/rbbt/rest/workflow.rb', line 31

def add_workflow(workflow, add_resource = false)
  raise "Provided workflow is not of type Workflow" unless  Workflow === workflow 
  RbbtRESTWorkflow::WORKFLOWS.push workflow unless RbbtRESTWorkflow::WORKFLOWS.include? workflow

  Log.debug "Adding #{ workflow } to REST server" 

  add_workflow_resource(workflow, add_resource == :priority) if add_resource
  workflow.documentation

  if ENV["RBBT_WORKFLOW_EXPORT_ALL"] == "true"
    workflow.export *workflow.tasks.keys
  end

  self.instance_eval workflow.libdir.lib['sinatra.rb'].read, workflow.libdir.lib['sinatra.rb'].find if workflow.respond_to?(:libdir) and  File.exists? workflow.libdir.lib['sinatra.rb']

  get "/#{workflow.to_s}" do
    case format
    when :json
      content_type "application/json"

      @can_stream = ENV["RBBT_WORKFLOW_TASK_STREAM"]  == 'true'
      {:stream => workflow.stream_exports, :exec => workflow.exec_exports, :synchronous => workflow.synchronous_exports, :asynchronous => workflow.asynchronous_exports, :can_stream => !!@can_stream}.to_json
    else
      workflow_render('tasks', workflow, nil, @clean_params)
    end
  end

  get "/#{workflow.to_s}/documentation" do
    case format
    when :html
      workflow_render('tasks', workflow)
    when :json
      content_type "application/json"
      workflow.documentation.to_json
    else
      raise "Unsupported format specified: #{ format }"
    end
  end

  get "/#{workflow.to_s}/:task/info" do
    task     = consume_parameter(:task)

    raise Workflow::TaskNotFoundException.new workflow, task unless workflow.tasks.include? task.to_sym

    case format
    when :html
      workflow_render('task_info', workflow, nil, :cache => false )
    when :json
      content_type "application/json"
      workflow.task_info(task.to_sym).to_json
    else
      raise "Unsupported format specified: #{ format }"
    end
  end

  get "/#{workflow.to_s}/:task/dependencies" do
    task     = consume_parameter(:task)

    raise Workflow::TaskNotFoundException.new workflow, task unless workflow.tasks.include? task.to_sym

    case format
    when :html
      workflow_render('task_dependencies', workflow)
    when :json
      content_type "application/json"
      workflow.task_dependencies[task.to_sym].to_json
    else
      raise "Unsupported format specified: #{ format }"
    end
  end

  get "/#{workflow.to_s}/description" do
    halt 200, workflow.documentation[:description] || ""
  end


  get "/#{workflow.to_s}/:task" do
    task     = consume_parameter(:task)
    jobname  = consume_parameter(:jobname)

    raise Workflow::TaskNotFoundException.new workflow, task unless workflow.tasks.include? task.to_sym

    execution_type = execution_type(workflow, task)

    task_parameters = consume_task_parameters(workflow, task, params)
    
    task_parameters[:jobname] = jobname

    if complete_input_set(workflow, task, task_parameters) || format != :html || jobname
      issue_job(workflow, task, jobname, task_parameters)
    else
      workflow_render('form', workflow, task, task_parameters)
    end
  end

  post "/#{workflow.to_s}/:task" do
    task = consume_parameter(:task)
    jobname  = consume_parameter(:jobname)

    raise Workflow::TaskNotFoundException.new workflow, task unless workflow.tasks.include? task.to_sym

    execution_type = execution_type(workflow, task)

    task_parameters = consume_task_parameters(workflow, task, params)

    if params.include?('__input_file_bundle')
      inputs = workflow.task_info(task)[:inputs]
      input_types = workflow.task_info(task)[:input_types]

      file = params['__input_file_bundle'][:tempfile].path
      task_parameters = task_parameters.merge(Workflow.load_inputs(file, inputs, input_types))
    end

    issue_job(workflow, task, jobname, task_parameters)
  end

  get "/#{workflow.to_s}/:task/:job" do
    task = consume_parameter(:task)
    job  = consume_parameter(:job)

    raise Workflow::TaskNotFoundException.new workflow, task unless workflow.tasks.include? task.to_sym

    execution_type = execution_type(workflow, task)

    job = workflow.fast_load_id(File.join(task, job))

    halt 404, "Job not found: #{job.path} (#{job.status})" if job.status == :noinfo and not job.done?

    abort_job(workflow, job) and halt 200 if update.to_s == "abort"
    clean_job(workflow, job) and halt 200 if update.to_s == "clean"
    recursive_clean_job(workflow, job) and halt 200 if update.to_s == "recursive_clean"

    begin
      status = job.status

      started = job.started?
      waiting = job.waiting?

      done = job.done? 
      error = job.error? || job.aborted? 

      dirty = (done || status == :done) && job.dirty?

      started = started || done || error

      done = false if dirty

      if done
        show_result job, workflow, task, params
      else
        if started || waiting
          exec_type = execution_type(workflow, task) 
          case
          when dirty
            error_for job
          when error
            error_for job
          when (exec_type == :asynchronous or exec_type == :async)
            case @format.to_s
            when 'json', 'raw', 'binary'
              halt 202
            else
              @title = [[job.workflow.to_s, job.task_name] * "#", job.clean_name] * " "
              wait_on job
            end
          else
            job.join
            raise RbbtRESTHelpers::Retry
          end
        else
          halt 404, "Job not found: #{status}"
        end
      end
    rescue RbbtRESTHelpers::Retry
      retry
    end
  end

  get "/#{workflow.to_s}/:task/:job/info" do
    task = consume_parameter(:task)
    job  = consume_parameter(:job)

    raise Workflow::TaskNotFoundException.new workflow, task unless workflow.tasks.include? task.to_sym

    execution_type = execution_type(workflow, task)

    job = workflow.load_id(File.join(task, job))

    not_started = true unless job.started? or job.waiting? or job.error? or job.aborted?
    dirty = true  if (job.done? || job.status == :done) && job.dirty?

    begin
      check_step job unless job.done? or not_started
    rescue Aborted
    end

    halt 404, "Job not found: #{job.path} (#{job.status})" if job.status == :noinfo and not job.done?

    case format
    when :html
      workflow_render('job_info', workflow, task, :job => job, :info => job.info)
    when :input_bundle
      task_info = workflow.task_info(task)
      input_types = task_info[:input_types]
      task_inputs = task_info[:inputs]
      TmpFile.with_file do |basedir|
        dir = File.join(basedir, 'inputs')
        Step.save_job_inputs(job, dir)
        filename = File.join(basedir, job.clean_name + '.input_bundle.tar.gz')
        content_type "application/tar+gzip"
        Misc.consume_stream(Misc.tarize(dir), false, filename)
        send_file filename, :filename => File.basename(filename)
      end
    when :json
      halt 200, {}.to_json if not_started
      content_type "application/json"
      info_json = {}
      job.info.each do |k,v|
        info_json[k] = case v.to_s
                       when "NaN"
                         "NaN"
                       when "Infinity"
                         "Infinity"
                       else
                         v
                       end
      end
      if dirty
        info_json[:status] = :error 
        info_json[:messages] ||= []
        info_json[:messages] << "Job dirty, please reissue."
      end
      halt 200, info_json.to_json
    else
      raise "Unsupported format specified: #{ format }"
    end
  end

  get "/#{workflow.to_s}/:task/:job/files" do
    task = consume_parameter(:task)
    job  = consume_parameter(:job)

    raise Workflow::TaskNotFoundException.new workflow, task unless workflow.tasks.include? task.to_sym

    execution_type = execution_type(workflow, task)

    job = workflow.fast_load_id(File.join(task, job))

    case format
    when :html
      workflow_render('job_files', workflow, task, :info => job.info, :job => job)
    when :json
      content_type "application/json"
      job.files.to_json
    else
      raise "Unsupported format specified: #{ format }"
    end
  end

  get "/#{workflow.to_s}/:task/:job/file/*" do
    task = consume_parameter(:task)
    job  = consume_parameter(:job)
    filename = params[:splat].first

    raise Workflow::TaskNotFoundException.new workflow, task unless workflow.tasks.include? task.to_sym

    execution_type = execution_type(workflow, task)

    job = workflow.fast_load_id(File.join(task, job))

    path = job.file(filename)
    #require 'mimemagic'
    #mime = nil
    #Open.open(path) do |io|
    #  begin
    #    mime = MimeMagic.by_path(io) 
    #    if mime.nil?
    #      io.rewind
    #      mime = MimeMagic.by_magic(io) 
    #    end
    #    if mime.nil?
    #      io.rewind
    #      mime = "text/tab-separated-values" if io.gets =~ /^#/ and io.gets.include? "\t"
    #    end
    #  rescue Exception
    #    Log.exception $!
    #  end
    #end
    mime = file_mimetype(path)
    content_type mime if mime
    send_file path
  end

  delete "/#{workflow.to_s}/:task/:job" do
    task = consume_parameter(:task)
    job  = consume_parameter(:job)
    job = workflow.fast_load_id(File.join(task, job))

    raise Workflow::TaskNotFoundException.new workflow, task unless workflow.tasks.include? task.to_sym

    clean_job(workflow, job)
  end
end

#add_workflow_resource(workflow, priority_templates = false) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/rbbt/rest/workflow.rb', line 23

def add_workflow_resource(workflow, priority_templates = false)
  views_dir = workflow.respond_to?(:libdir)? workflow.libdir.www.views.find(:lib) : nil
  if views_dir and views_dir.exists?
    Log.debug "Registering views for #{ workflow }: #{ views_dir.find } - priority #{priority_templates}"
    RbbtRESTMain.add_resource_path(views_dir, priority_templates)
  end
end