Class: UU::OS::REST::Future
- Inherits:
-
Object
- Object
- UU::OS::REST::Future
- Defined in:
- uu_os_framework-0.29.16/lib/uu/os/rest/future.rb
Overview
Object for accessing result of asynchronous processes.
Constant Summary
Instance Method Summary (collapse)
-
- (UU::OS::UESURI) get(result_code = nil, timeout = 60)
Returns result identified by the given
result_code
. -
- (UU::OS::UESURI) get_process_uri
Returns underlining process uri.
-
- (Future) initialize(init_data, result_code = nil)
constructor
Creates new instance of Future.
-
- (String) to_json(options = {})
Returns json representation of underlining process uri.
-
- (String) to_s
Returns string representation of underlining process uri.
Constructor Details
- (Future) initialize(init_data, result_code = nil)
Creates new instance of Future.
40 41 42 43 44 45 46 47 48 49 |
# File 'uu_os_framework-0.29.16/lib/uu/os/rest/future.rb', line 40 def initialize(init_data, result_code = nil) if init_data.kind_of?UU::OS::REST::Future @process_uri = init_data.get_process_uri else @process_uri = UU::OS::UESURI.new(init_data) end @default_result_code = result_code end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
- (Object) method_missing(method, *args, &block) (private)
192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'uu_os_framework-0.29.16/lib/uu/os/rest/future.rb', line 192 def method_missing(method, *args, &block) #:nodoc: # Delegate unknown method calling to the inner UESURI object because of backward compatibility if @process_uri.respond_to? method # log warning logger.warning("Script tries to use method from the UESURI object on Future object. Method name: #{method}, Back trace: #{caller.join("\n")}") # send event to inner UESURI return @process_uri.send(method, *args, &block) else # if the method is not defined either on the UESURI object super end end |
Instance Method Details
- (UU::OS::UESURI) get(result_code = nil, timeout = 60)
Returns result identified by the given result_code
.
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 |
# File 'uu_os_framework-0.29.16/lib/uu/os/rest/future.rb', line 85 def get(result_code = nil, timeout = 60) if result_code.kind_of?Numeric timeout = result_code result_code = nil end if (timeout < 0) || (timeout > @@MAXIMUM_WAIT_TIMEOUT) remaining_time = @@MAXIMUM_WAIT_TIMEOUT else remaining_time = timeout end wait = 0.5 # First wait will be one second (in each loop value is multiplied by 2) result = nil svc = RemoteClient.new(UU::OS::REST, @@PROCEXP_PATH) # if result_code passed to method is null, try default result code from constructor result_code = @default_result_code if result_code.nil? if !result_code.nil? data = "{\"eventCodes\":[\"#{result_code}\"]}" else data = '{}' end while true result = svc.post(:getProcessResult, @process_uri, data) if (result == 'null') || (result == '') # Nil result is wrongly returned as "null" string or empty string # (based on process entity existence) result = nil end if (result) # Result object was provided (event matching given code or event # representing process final state if no explicit code is given) result = JSON.parse(result, :symbolize_names => true) if result_code.nil? && (result[:state] == 'FAILED' || result[:state] == 'ERROR') init_data = "{\"code\":\"UU.OS/E05P02.M16\",\"errorMessages\":[{\"code\":\"UU.OS/E05P02.M16\",\"localizedMessage\":\"Asynchronous process was not successfully finished:\\n\\t#{(result)}\"}]}" raise UU::OS::REST::ServerException.new(init_data) elsif !result[:errorCode].nil? init_data = "{\"code\":\"UU.OS/E05P02.M16\",\"errorMessages\":[{\"code\":\"UU.OS/E05P02.M17\",\"localizedMessage\":\"Expected result cannot be provided:\\n\\t#{(result)}\"}]}" raise UU::OS::REST::ServerException.new(init_data) elsif !result[:result].nil? # Get result UESURI result = UU::OS::UESURI.new(result[:result]) else # No UESURI -> no result result = nil end break end status = JSON.parse(svc.get(:getProcessProgress, @process_uri), :symbolize_names => true) if status[:finished] >= 0 # We have no result, but the process is finished break end if remaining_time <= 0 # We have no result, process is not finished, but we are out of time raise 'Result has not been provided by process in the given time.' end # Wait longer in each cycle (in order not to flood remote server) wait *= 2 if (remaining_time > wait) remaining_time -= wait else # If next wait is greater than remaining wait time, wait for the remaining time only wait = remaining_time remaining_time = 0 end sleep(wait.to_i) end result end |
- (UU::OS::UESURI) get_process_uri
Returns underlining process uri.
163 164 165 |
# File 'uu_os_framework-0.29.16/lib/uu/os/rest/future.rb', line 163 def get_process_uri @process_uri end |
- (String) to_json(options = {})
Returns json representation of underlining process uri.
185 186 187 |
# File 'uu_os_framework-0.29.16/lib/uu/os/rest/future.rb', line 185 def to_json( = {}) @process_uri.to_json() end |
- (String) to_s
Returns string representation of underlining process uri.
174 175 176 |
# File 'uu_os_framework-0.29.16/lib/uu/os/rest/future.rb', line 174 def to_s @process_uri.to_s end |