Module: RemoteStep::REST

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

Constant Summary collapse

DEFAULT_REFRESH_TIME =
2

Instance Method Summary collapse

Instance Method Details

#_cleanObject



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

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



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

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



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

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

#cleanObject



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

def clean
  init_job
  _clean
  self
end

#exec_jobObject



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

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

#getObject



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

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



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

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



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

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

#produce(*args) ⇒ Object



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

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

#recursive_cleanObject



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

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



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

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