class Nursery

Constants

CancelledJob
VERSION

Attributes

child_class[RW]
cancelled[R]
children[R]
default_config[R]
finished[R]

Public Class Methods

checkpoint() { || ... } click to toggle source
# File lib/nursery/nursery.rb, line 52
def self.checkpoint
        if Thread.current[:nursery] && Thread.current[:nursery].cancelled
                yield if block_given?
                raise CancelledJob
        end
end
new(default_config = {}) click to toggle source
# File lib/nursery/nursery.rb, line 7
def initialize(default_config = {})
        @children = []
        @cancelled = false
        @finished = false
        @default_config = default_config
end
with_nursery(config = {}, &block) click to toggle source
# File lib/nursery/nursery.rb, line 40
def self.with_nursery(config = {}, &block)
        nursery = new(config)
        begin
                retval = block.call(nursery)
        rescue StandardError
                nursery.cancel
                raise
        end
        nursery.finish
        retval
end

Public Instance Methods

cancel() click to toggle source
# File lib/nursery/nursery.rb, line 35
def cancel
        @cancelled = true
        finish                
end
finish() click to toggle source
# File lib/nursery/nursery.rb, line 22
def finish
        exceptions = children.map do |child|
                begin
                        child.finish
                        nil
                rescue Exception => e
                        e
                end
        end.compact
        exceptions.each { |e| raise(e) }
        @finished = true
end
run(config = default_config, &job) click to toggle source
# File lib/nursery/nursery.rb, line 14
def run(config = default_config, &job)
        Nursery.checkpoint
        raise("Nursery#run called outside of with_nursery block") if finished
        child = self.class.child_class.new(self, config, job)
        children << child
        child
end