Class: Tribe::Future
- Inherits:
-
Object
- Object
- Tribe::Future
- Defined in:
- lib/tribe/future.rb
Instance Method Summary collapse
- #failure(&block) ⇒ Object
- #failure? ⇒ Boolean
- #finished? ⇒ Boolean
-
#initialize(actor = nil) ⇒ Future
constructor
A new instance of Future.
- #result ⇒ Object
- #result=(val) ⇒ Object
- #success(&block) ⇒ Object
- #success? ⇒ Boolean
- #timeout ⇒ Object
- #timeout=(val) ⇒ Object
- #timeout? ⇒ Boolean
- #wait ⇒ Object
Constructor Details
#initialize(actor = nil) ⇒ Future
Returns a new instance of Future.
3 4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/tribe/future.rb', line 3 def initialize(actor = nil) @state = :initialized @lock = Mutex.new @condition = ConditionVariable.new @result = nil @success_callback = nil @failure_callback = nil @actor = actor @timer = nil @timeout = nil return nil end |
Instance Method Details
#failure(&block) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/tribe/future.rb', line 124 def failure(&block) @lock.synchronize do case @state when :initialized @failure_callback = block when :finished if @result.is_a?(Exception) if @actor @actor.perform! do block.call(@result) end else block.call(@result) end end else raise Tribe::FutureError.new('Invalid state.') end return nil end end |
#failure? ⇒ Boolean
97 98 99 |
# File 'lib/tribe/future.rb', line 97 def failure? return !success? end |
#finished? ⇒ Boolean
17 18 19 20 21 |
# File 'lib/tribe/future.rb', line 17 def finished? @lock.synchronize do return @state == :finished end end |
#result ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/tribe/future.rb', line 65 def result @lock.synchronize do raise Tribe::FutureNoResult.new('Result must be set first.') unless @state == :finished return @result end end |
#result=(val) ⇒ Object
29 30 31 32 33 34 35 36 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 |
# File 'lib/tribe/future.rb', line 29 def result=(val) @lock.synchronize do return unless @state == :initialized @timer.cancel if @timer @result = val @state = :finished @condition.signal if val.is_a?(Exception) if @failure_callback if @actor @actor.perform! do @failure_callback.call(val) end else @failure_callback.call(val) end end else if @success_callback if @actor @actor.perform! do @success_callback.call(val) end else @success_callback.call(val) end end end return nil end end |
#success(&block) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/tribe/future.rb', line 101 def success(&block) @lock.synchronize do case @state when :initialized @success_callback = block when :finished unless @result.is_a?(Exception) if @actor @actor.perform! do block.call(@result) end else block.call(@result) end end else raise Tribe::FutureError.new('Invalid state.') end return nil end end |
#success? ⇒ Boolean
89 90 91 92 93 94 95 |
# File 'lib/tribe/future.rb', line 89 def success? @lock.synchronize do raise Tribe::FutureNoResult.new('Result must be set first.') unless @state == :finished return !@result.is_a?(Exception) end end |
#timeout ⇒ Object
147 148 149 150 151 |
# File 'lib/tribe/future.rb', line 147 def timeout @lock.synchronize do @timeout end end |
#timeout=(val) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/tribe/future.rb', line 153 def timeout=(val) raise Tribe::FutureError.new('Timeout may only be set once.') if @timeout @timeout = val @timer = Workers::Timer.new(val) do begin raise Tribe::FutureTimeout.new("Timeout after #{@timeout} seconds.") rescue Tribe::FutureTimeout => e self.result = e end end end |
#timeout? ⇒ Boolean
23 24 25 26 27 |
# File 'lib/tribe/future.rb', line 23 def timeout? @lock.synchronnize do return @state == :finished && @result.is_a?(Tribe::FutureTimeout) end end |
#wait ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/tribe/future.rb', line 73 def wait @lock.synchronize do return if @state == :finished # The wait can return even if nothing called @conditional.signal, # so we need to check to see if the condition actually changed. # See https://github.com/chadrem/workers/issues/7 loop do @condition.wait(@lock) break if @state == :finished end return nil end end |