Module: Garcon::Obligation

Includes:
Dereferenceable
Included in:
Delay, IVar
Defined in:
lib/garcon/task/obligation.rb

Instance Method Summary collapse

Instance Method Details

#complete?Boolean

Has the obligation completed processing?

Returns:



67
68
69
# File 'lib/garcon/task/obligation.rb', line 67

def complete?
  [:fulfilled, :rejected].include? state
end

#exception(*args) ⇒ Object

Examples:

allows Obligation to be risen

rejected_ivar = Ivar.new.fail
raise rejected_ivar


173
174
175
176
# File 'lib/garcon/task/obligation.rb', line 173

def exception(*args)
  raise 'obligation is not rejected' unless rejected?
  reason.exception(*args)
end

#fulfilled?Boolean Also known as: realized?

Has the obligation been fulfilled?

Returns:



34
35
36
# File 'lib/garcon/task/obligation.rb', line 34

def fulfilled?
  state == :fulfilled
end

#incomplete?Boolean

Is the obligation still awaiting completion of processing?

Returns:



75
76
77
# File 'lib/garcon/task/obligation.rb', line 75

def incomplete?
  [:unscheduled, :pending].include? state
end

#pending?Boolean

Is obligation completion still pending?

Returns:



51
52
53
# File 'lib/garcon/task/obligation.rb', line 51

def pending?
  state == :pending
end

#reasonException

If an exception was raised during processing this will return the exception object. Will return ‘nil` when the state is pending or if the obligation has been successfully fulfilled.

Returns:

  • (Exception)

    The exception raised during processing or ‘nil`



163
164
165
166
167
168
# File 'lib/garcon/task/obligation.rb', line 163

def reason
  mutex.lock
  @reason
ensure
  mutex.unlock
end

#rejected?Boolean

Has the obligation been rejected?

Returns:



43
44
45
# File 'lib/garcon/task/obligation.rb', line 43

def rejected?
  state == :rejected
end

#stateSymbol

The current state of the obligation.

Returns:

  • (Symbol)

    The current state.



149
150
151
152
153
154
# File 'lib/garcon/task/obligation.rb', line 149

def state
  mutex.lock
  @state
ensure
  mutex.unlock
end

#unscheduled?Boolean

Is the obligation still unscheduled?

Returns:



59
60
61
# File 'lib/garcon/task/obligation.rb', line 59

def unscheduled?
  state == :unscheduled
end

#value(timeout = nil) ⇒ Object

The current value of the obligation. Will be ‘nil` while the state is pending or the operation has been rejected.

Parameters:

  • Timeout (Numeric)

    the maximum time in seconds to wait.

Returns:

  • (Object)

    see Dereferenceable#deref



88
89
90
91
# File 'lib/garcon/task/obligation.rb', line 88

def value(timeout = nil)
  wait timeout
  deref
end

#value!(timeout = nil) ⇒ Object

The current value of the obligation. Will be ‘nil` while the state is pending or the operation has been rejected. Will re-raise any exceptions raised during processing (but will not raise an exception on timeout).

Parameters:

  • Timeout (Numeric)

    the maximum time in seconds to wait.

Returns:

  • (Object)

    see Dereferenceable#deref

Raises:

  • (Exception)

    Raises the reason when rejected.



135
136
137
138
139
140
141
142
# File 'lib/garcon/task/obligation.rb', line 135

def value!(timeout = nil)
  wait(timeout)
  if rejected?
    raise self
  else
    deref
  end
end

#wait(timeout = nil) ⇒ Obligation

Wait until obligation is complete or the timeout has been reached.

Parameters:

  • Timeout (Numeric)

    the maximum time in seconds to wait.

Returns:



100
101
102
103
# File 'lib/garcon/task/obligation.rb', line 100

def wait(timeout = nil)
  event.wait(timeout) if timeout != 0 && incomplete?
  self
end

#wait!(timeout = nil) ⇒ Obligation Also known as: no_error!

Wait until obligation is complete or the timeout is reached. Will re-raise any exceptions raised during processing (but will not raise an exception on timeout).

Parameters:

  • timeout (Numeric) (defaults to: nil)

    The maximum time in second to wait.

Returns:

Raises:

  • (Exception)

    Raises the reason when rejected`



117
118
119
# File 'lib/garcon/task/obligation.rb', line 117

def wait!(timeout = nil)
  wait(timeout).tap { raise self if rejected? }
end