module Polyphony::FiberSupervision

Fiber supervision

Public Instance Methods

supervise(*fibers, **opts, &block) click to toggle source
# File lib/polyphony/extensions/fiber.rb, line 83
def supervise(*fibers, **opts, &block)
  block ||= supervise_opts_to_block(opts)

  fibers.each do |f|
    f.attach_to(self) unless f.parent == self
    f.monitor(self)
  end

  mailbox = monitor_mailbox

  while true
    (fiber, result) = mailbox.shift
    block&.call(fiber, result)
  end
end
supervise_opts_to_block(opts) click to toggle source
# File lib/polyphony/extensions/fiber.rb, line 99
def supervise_opts_to_block(opts)
  block = opts[:on_done] || opts[:on_error]
  return nil unless block || opts[:restart]

  error_only = !!opts[:on_error]
  restart_always = opts[:restart] == :always
  restart_on_error = opts[:restart] == :on_error

  ->(f, r) do
    is_error = r.is_a?(Exception)
    block.(f, r) if block && (!error_only || is_error)
    f.restart if restart_always || (restart_on_error && is_error)
  end
end