Module: RemoteStep::REST

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

Instance Method Summary collapse

Instance Method Details

#_cleanObject



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rbbt/workflow/remote_workflow/remote_step/rest.rb', line 71

def _clean
  begin
    _restart
    cache_files.each do |cache_file|
      Open.rm cache_file
    end
    params = {:_update => :clean}
    @adaptor.clean_url(url, params) if @url
  rescue Exception
    Log.exception $!
  end
end

#_run_job(cache_type = :asynchronous) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/rbbt/workflow/remote_workflow/remote_step/rest.rb', line 132

def _run_job(cache_type = :asynchronous)
  get_streams

  task_url = RemoteWorkflow::REST.escape_url(File.join(base_url, task.to_s))
  @adaptor.__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
    @adaptor.execute_job(base_url, task, task_params, cache_type)
  end
end

#abortObject



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

def abort
  return self if status == :done
  @adaptor.get_json(@url + '?_update=abort') if @url and @name
  self
end

#cleanObject



84
85
86
87
88
# File 'lib/rbbt/workflow/remote_workflow/remote_step/rest.rb', line 84

def clean
  init_job
  _clean
  self
end

#exec_jobObject



27
28
29
30
# File 'lib/rbbt/workflow/remote_workflow/remote_step/rest.rb', line 27

def exec_job
  res = _run_job(:exec)
  load_res res, result_type == :array ? :json : result_type
end

#getObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rbbt/workflow/remote_workflow/remote_step/rest.rb', line 4

def get
  params ||= {}
  params = params.merge(:_format => [:string, :boolean, :tsv, :annotations, :array].include?(result_type.to_sym) ? :raw : :json )
  @cache_result ||= Persist.persist("REST persist", :binary, :file => cache_file + "." + Misc.obj2digest(params)) do
    Misc.insist 3, rand(2) + 1 do
      begin
        init_job if url.nil?
        @adaptor.get_raw(url, params)
      rescue
        Log.exception $!
        raise $!
      end
    end
  end
end

#init_job(cache_type = nil, other_params = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rbbt/workflow/remote_workflow/remote_step/rest.rb', line 38

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?
  @last_info_time = nil
  @done = false
  get_streams
  @name ||= Persist.memory("RemoteSteps", :workflow => self, :task => task, :jobname => @name, :inputs => inputs, :cache_type => cache_type) do
    Misc.insist do
      @adaptor.post_jobname(File.join(base_url, task.to_s), inputs.merge(other_params).merge(:jobname => @name||@base_name, :_cache_type => cache_type))
    end
  end
  if Open.remote? @name
    @url = @name
    @name = File.basename(@name)
  else
    @url = File.join(base_url, task.to_s, @name)
  end
  self
end

#loadObject



20
21
22
23
24
25
# File 'lib/rbbt/workflow/remote_workflow/remote_step/rest.rb', line 20

def load
  params = {}
  join unless done? or streaming?
  raise get_exception if error? or aborted?
  load_res get
end

#produce(*args) ⇒ Object



148
149
150
151
152
# File 'lib/rbbt/workflow/remote_workflow/remote_step/rest.rb', line 148

def produce(*args)
  @started = true
  init_job
  _run_job
end

#recursive_cleanObject



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rbbt/workflow/remote_workflow/remote_step/rest.rb', line 58

def recursive_clean
  Log.warn "Not doing recursive cleans"
  return
  begin
    _restart
    params = {:_update => :recursive_clean}
    @adaptor.get_raw(url, params)
  rescue Exception
    Log.exception $!
  end
  self
end

#stream_job(task_url, task_params, stream_input, cache_type = :exec) ⇒ Object



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

def stream_job(task_url, task_params, stream_input, cache_type = :exec)
  require 'rbbt/util/misc/multipart_payload'
  RemoteWorkflow.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
            RemoteWorkflow.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