Class: UU::OS::REST::Future

Inherits:
Object
  • Object
show all
Defined in:
uu_os_framework-0.10.6/lib/uu/os/rest/future.rb

Overview

Object for accessing result of asynchronous processes.

Instance Method Summary (collapse)

Constructor Details

- (Future) initialize(process_uri)

Creates new instance of Future.

Parameters:

  • process_uri (String, UU::OS::UESURI)

    URI of asynchronous process where to look for result.



23
24
25
# File 'uu_os_framework-0.10.6/lib/uu/os/rest/future.rb', line 23

def initialize(process_uri)
  @process_uri = UU::OS::UESURI.new(process_uri)
end

Instance Method Details

- (UU::OS::UESURI) get(result_code = nil, timeout = 60)

Returns result identified by the given result_code.

Parameters:

  • result_code (String) (defaults to: nil)

    Code (regular expressions may be used) of the required result. Default is nil which means we are waiting for result of parent process final state. In case of explicitly given result_code result is being searched also in all nested (asynchronous) subprocesses. In case of nil value result is returned as soon as parent process is finished (does not wait and search for results of nested asynchronous subprocesses).

  • timeout (Numeric) (defaults to: 60)

    Maximum wait time in seconds (default is one minute, maximum is 30 minutes)

Returns:

  • (UU::OS::UESURI)

    Result in form of UESURI or nil if result was not provided. Throws exception in case the result has not been provided in the given timeout.



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
# File 'uu_os_framework-0.10.6/lib/uu/os/rest/future.rb', line 37

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.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)
      # We have a result
      result = JSON.parse(result, :symbolize_names => true)
      if result[:result]
        # 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