Class: Warg::Executor::Deferred::CallbacksQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/warg.rb

Defined Under Namespace

Classes: Outcome

Instance Method Summary collapse

Constructor Details

#initialize(execution_order) ⇒ CallbacksQueue

Returns a new instance of CallbacksQueue.


1663
1664
1665
1666
# File 'lib/warg.rb', line 1663

def initialize(execution_order)
  @queue = []
  @executor_class = Executor.for(execution_order)
end

Instance Method Details

#<<(callback) ⇒ Object


1668
1669
1670
# File 'lib/warg.rb', line 1668

def <<(callback)
  @queue << callback
end

#drain(execution_result) ⇒ Object


1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
# File 'lib/warg.rb', line 1672

def drain(execution_result)
  drain_queue = @queue.dup

  # NOTE: It would be nice to incorporate the failure callback into the code here
  until drain_queue.empty? || execution_result.failed?
    callback = drain_queue.shift
    executor = @executor_class.new(execution_result)

    execution_result = executor.run do |outcome, result|
      callback_outcome = Outcome.new(outcome)

      begin
        # Use `||=` in case `#value` is set in the callback
        callback_outcome.value ||= callback.(outcome.host, outcome.value, callback_outcome)
      rescue => error
        callback_outcome.error = error
      end

      result.update callback_outcome
    end
  end

  execution_result
end