lib/polyphony/extensions/fiber.rb
# call-seq:
#   fiber.supervise
#   fiber.supervise(fiber_a, fiber_b)
#   fiber.supervise { |f, r| handle_terminated_fiber(f, r) }
#   fiber.supervise(on_done: ->(f, r) { handle_terminated_fiber(f, r) })
#   fiber.supervise(on_error: ->(f, e) { handle_error(f, e) })
#   fiber.supervise(*fibers, restart: always)
#   fiber.supervise(*fibers, restart: on_error)
#
# Supervises the given fibers or all child fibers. The fiber is put in
# supervision mode, which means any child added after calling `#supervise`
# will automatically be supervised. Depending on the given options, fibers
# may be automatically restarted.
#
# If a block is given, the block is called whenever a supervised fiber has
# terminated. If the `:on_done` option is given, that proc will be called
# when a supervised fiber has terminated. If the `:on_error` option is
# given, that proc will be called when a supervised fiber has terminated
# with an uncaught exception. If the `:restart` option equals `:always`,
# fibers will always be restarted. If the `:restart` option equals
# `:on_error`, fibers will be restarted only when terminated with an
# uncaught exception.
#
# This method blocks indefinitely.
#
# @param *fibers [Array<Fiber>] fibers to supervise
# @param on_done: [Proc, nil] proc to call when a supervised fiber is terminated
# @param on_error: [Proc, nil] proc to call when a supervised fiber is terminated with an exception
# @param restart: [:always, :on_error, nil] whether to restart terminated fibers
# @return [void]


Supervises the given fibers or all child fibers. The fiber is put in
supervision mode, which means any child added after calling `#supervise`
will automatically be supervised. Depending on the given options, fibers
may be automatically restarted.

If a block is given, the block is called whenever a supervised fiber has
terminated. If the `:on_done` option is given, that proc will be called
when a supervised fiber has terminated. If the `:on_error` option is
given, that proc will be called when a supervised fiber has terminated
with an uncaught exception. If the `:restart` option equals `:always`,
fibers will always be restarted. If the `:restart` option equals
`:on_error`, fibers will be restarted only when terminated with an
uncaught exception.

This method blocks indefinitely.
Supervises the given fibers or all child fibers . The fiber is put in supervision mode which means any child added after calling ` #supervise ` will automatically be supervised . Depending on the given options fibers may be automatically restarted . If a block is given the block is called whenever a supervised fiber has terminated . If the ` : on_done ` option is given that proc will be called when a supervised fiber has terminated . If the ` : on_error ` option is given that proc will be called when a supervised fiber has terminated with an uncaught exception . If the ` : restart ` option equals ` : always ` fibers will always be restarted . If the ` : restart ` option equals ` : on_error ` fibers will be restarted only when terminated with an uncaught exception . This method blocks indefinitely .
def supervise(*fibers, **opts, &block)
      block ||= supervise_opts_to_block(opts)
      @supervise_mode = true
      fibers = children if fibers.empty?
      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
    ensure
      @supervise_mode = false
    end
def supervise ( * fibers , ** opts , & block ) block ||= supervise_opts_to_block ( opts ) @supervise_mode = true fibers = children if fibers . empty? 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 ensure @supervise_mode = false end
call-seq:
fiber.supervise
fiber.supervise(fiber_a, fiber_b)
fiber.supervise { |f, r| handle_terminated_fiber(f, r) }
fiber.supervise(on_done: ->(f, r) { handle_terminated_fiber(f, r) })
fiber.supervise(on_error: ->(f, e) { handle_error(f, e) })
fiber.supervise(*fibers, restart: always)
fiber.supervise(*fibers, restart: on_error)
call - seq : fiber . supervise fiber . supervise ( fiber_a fiber_b ) fiber . supervise { |f r| handle_terminated_fiber ( f r ) } fiber . supervise ( on_done : - > ( f r ) { handle_terminated_fiber ( f r ) } ) fiber . supervise ( on_error : - > ( f e ) { handle_error ( f e ) } ) fiber . supervise ( * fibers restart : always ) fiber . supervise ( * fibers restart : on_error )
None
void
fibers
<not_specific>
opts
<not_specific>
block
<not_specific>
*fibers
<not_specific>
fibers to supervise
fibers to supervise
on_done:
<not_specific>
proc to call when a supervised fiber is terminated
proc to call when a supervised fiber is terminated
on_error:
<not_specific>
proc to call when a supervised fiber is terminated with an exception
proc to call when a supervised fiber is terminated with an exception
restart:
<not_specific>
whether to restart terminated fibers
whether to restart terminated fibers