Class: WorkflowRESTClient

Inherits:
Object
  • Object
show all
Defined in:
lib/rbbt/rest/client.rb

Defined Under Namespace

Classes: RemoteStep

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, name) ⇒ WorkflowRESTClient

Returns a new instance of WorkflowRESTClient.



178
179
180
181
182
# File 'lib/rbbt/rest/client.rb', line 178

def initialize(url, name)
  Log.debug{ "Loading remote workflow #{ name }: #{ url }" }
  @url, @name = url, name
  init_remote_tasks
end

Instance Attribute Details

#asynchronous_exportsObject

Returns the value of attribute asynchronous_exports.



176
177
178
# File 'lib/rbbt/rest/client.rb', line 176

def asynchronous_exports
  @asynchronous_exports
end

#exec_exportsObject

Returns the value of attribute exec_exports.



176
177
178
# File 'lib/rbbt/rest/client.rb', line 176

def exec_exports
  @exec_exports
end

#nameObject

Returns the value of attribute name.



176
177
178
# File 'lib/rbbt/rest/client.rb', line 176

def name
  @name
end

#synchronous_exportsObject

Returns the value of attribute synchronous_exports.



176
177
178
# File 'lib/rbbt/rest/client.rb', line 176

def synchronous_exports
  @synchronous_exports
end

#urlObject

Returns the value of attribute url.



176
177
178
# File 'lib/rbbt/rest/client.rb', line 176

def url
  @url
end

Class Method Details

.fix_hash(hash, fix_values = false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rbbt/rest/client.rb', line 9

def self.fix_hash(hash, fix_values = false)
  fixed = {}
  hash.each do |key, value|
    fixed[key.to_sym] = case
                        when Hash === value 
                          fix_hash(value)  
                        when (fix_values and String === value)
                          value.to_sym
                        else
                          value
                        end
  end
  fixed
end

.get_json(url, params = {}) ⇒ Object



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

def self.get_json(url, params = {})
  Log.debug{ "RestClient get_json: #{ url } - #{Misc.fingerprint params }" }
  params = params.merge({ :_format => 'json' })
  begin
    res = RestClient.get(URI.encode(url), :params => params)
  rescue => e
    raise JSON.parse(e.response)["message"]
  end
  begin
    JSON.parse(res)
  rescue
    res
  end
end

.get_raw(url, params = {}) ⇒ Object



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

def self.get_raw(url, params = {})
  Log.debug{ "RestClient get_raw: #{ url } - #{Misc.fingerprint params}" }
  params = params.merge({ :_format => 'raw' })
  Misc.insist(2, 0.5) do
    RestClient.get(URI.encode(url), :params => params)
  end
end

.post_jobname(url, params = {}) ⇒ Object



32
33
34
35
36
# File 'lib/rbbt/rest/client.rb', line 32

def self.post_jobname(url, params = {})
  Log.debug{ "RestClient post_jobname: #{ url } - #{Misc.fingerprint params}" }
  params = params.merge({ :_format => 'jobname' })
  RestClient.post(URI.encode(url), params)
end

.post_json(url, params = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rbbt/rest/client.rb', line 53

def self.post_json(url, params = {})
  if url =~ /_cache_type=:exec/
    JSON.parse(Open.open(url, :nocache => true))
  else
    params = params.merge({ :_format => 'json' })
    res = RestClient.post(URI.encode(url), params)
    begin
      JSON.parse(res)
    rescue
      res
    end
  end
end

Instance Method Details

#doc(task = nil) ⇒ Object



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
# File 'lib/rbbt/rest/client.rb', line 290

def doc(task = nil)

  if task.nil?
    puts self.to_s 
    puts "=" * self.to_s.length
    puts

    puts "## TASKS"
    puts
    tasks.each do |name,task|
      puts "  * #{ name }:"
      puts "    " << task.description if task.description and not task.description.empty?
      puts
    end
  else

    if Task === task
      task_name = task.name
    else
      task_name = task
      task = self.tasks[task_name]
    end
    dependencies = self.rec_dependencies(task_name).collect{|dep_name| self.tasks[dep_name.to_sym]}

    task.doc(dependencies)
  end
end

#exported_tasksObject



210
211
212
# File 'lib/rbbt/rest/client.rb', line 210

def exported_tasks
  (@asynchronous_exports  + @synchronous_exports + @exec_exports).compact.flatten
end

#init_remote_tasksObject



270
271
272
273
274
275
# File 'lib/rbbt/rest/client.rb', line 270

def init_remote_tasks
  task_exports = WorkflowRESTClient.get_json(url)
  @asynchronous_exports = task_exports["asynchronous"].collect{|task| task.to_sym }
  @synchronous_exports = task_exports["synchronous"].collect{|task| task.to_sym }
  @exec_exports = task_exports["exec"].collect{|task| task.to_sym }
end

#job(task, name, inputs) ⇒ Object



277
278
279
280
# File 'lib/rbbt/rest/client.rb', line 277

def job(task, name, inputs)
  task_info = task_info(task)
  RemoteStep.new(url, task, name, inputs, task_info[:result_type], task_info[:result_description], @exec_exports.include?(task))
end

#load_id(id) ⇒ Object



282
283
284
285
286
287
288
# File 'lib/rbbt/rest/client.rb', line 282

def load_id(id)
  task, name = id.split("/")
  step = RemoteStep.new File.join(url, id)
  step.result_type = task_info(task)[:result_type]
  step.result_description = task_info(task)[:result_description]
  step
end

#load_tasksObject



225
226
227
228
# File 'lib/rbbt/rest/client.rb', line 225

def load_tasks
  exported_tasks.each{|name| tasks[name]}
  nil
end

#rec_dependencies(taskname) ⇒ Object



240
241
242
243
244
245
246
247
248
# File 'lib/rbbt/rest/client.rb', line 240

def rec_dependencies(taskname)
  if task_dependencies.include? taskname
    deps = task_dependencies[taskname].select{|dep| String === dep or Symbol === dep}
    deps.concat deps.collect{|dep| rec_dependencies(dep)}.compact.flatten
    deps.uniq
  else
    []
  end
end

#rec_input_defaults(taskname) ⇒ Object



254
255
256
# File 'lib/rbbt/rest/client.rb', line 254

def rec_input_defaults(taskname)
  [taskname].concat(rec_dependencies(taskname)).inject({}){|acc, tn| acc.merge tasks[tn.to_sym].input_defaults}
end

#rec_input_descriptions(taskname) ⇒ Object



262
263
264
# File 'lib/rbbt/rest/client.rb', line 262

def rec_input_descriptions(taskname)
  [taskname].concat(rec_dependencies(taskname)).inject({}){|acc, tn| acc.merge tasks[tn.to_sym].input_descriptions}
end

#rec_input_options(taskname) ⇒ Object



266
267
268
# File 'lib/rbbt/rest/client.rb', line 266

def rec_input_options(taskname)
  [taskname].concat(rec_dependencies(taskname)).inject({}){|acc, tn| acc.merge tasks[tn.to_sym].input_options}
end

#rec_input_types(taskname) ⇒ Object



258
259
260
# File 'lib/rbbt/rest/client.rb', line 258

def rec_input_types(taskname)
  [taskname].concat(rec_dependencies(taskname)).inject({}){|acc, tn| acc.merge tasks[tn.to_sym].input_types}
end

#rec_inputs(taskname) ⇒ Object



250
251
252
# File 'lib/rbbt/rest/client.rb', line 250

def rec_inputs(taskname)
  [taskname].concat(rec_dependencies(taskname)).inject([]){|acc, tn| acc.concat tasks[tn.to_sym].inputs}
end

#task_dependenciesObject



230
231
232
233
234
235
236
237
238
# File 'lib/rbbt/rest/client.rb', line 230

def task_dependencies
  @task_dependencies ||= Hash.new do |hash,task| 
    hash[task] = if exported_tasks.include? task
      WorkflowRESTClient.get_json(File.join(url, task.to_s, 'dependencies'))
    else
      []
    end
  end
end

#task_info(task) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/rbbt/rest/client.rb', line 192

def task_info(task)
  @task_info ||= {}
  @task_info[task]
  
  if @task_info[task].nil?
    task_info = WorkflowRESTClient.get_json(File.join(url, task.to_s, 'info'))
    task_info = WorkflowRESTClient.fix_hash(task_info)

    task_info[:result_type] = task_info[:result_type].to_sym
    task_info[:export] = task_info[:export].to_sym
    task_info[:input_types] = WorkflowRESTClient.fix_hash(task_info[:input_types], true)
    task_info[:inputs] = task_info[:inputs].collect{|input| input.to_sym }

    @task_info[task] = task_info
  end
  @task_info[task]
end

#tasksObject



214
215
216
217
218
219
220
221
222
223
# File 'lib/rbbt/rest/client.rb', line 214

def tasks
  @tasks ||= Hash.new do |hash,task_name| 
    info = task_info(task_name)
    task = Task.setup info do |*args|
      raise "This is a remote task" 
    end
    task.name = task_name.to_sym
    hash[task_name] = task
  end
end

#to_sObject



184
185
186
# File 'lib/rbbt/rest/client.rb', line 184

def to_s
  name
end

#workflow_descriptionObject



188
189
190
# File 'lib/rbbt/rest/client.rb', line 188

def workflow_description
  WorkflowRESTClient.get_raw(File.join(url, 'description'))
end