Class: Zoidberg::Supervisor

Inherits:
Object
  • Object
show all
Includes:
SoftShell
Defined in:
lib/zoidberg/supervisor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SoftShell

#_zoidberg_thread, #async, #defer, included, #sleep

Constructor Details

#initializeself

Create a new supervisor



14
15
16
# File 'lib/zoidberg/supervisor.rb', line 14

def initialize
  @registry = Registry.new
end

Instance Attribute Details

#registryRegistry (readonly)

Returns current supervision registry.

Returns:

  • (Registry)

    current supervision registry



9
10
11
# File 'lib/zoidberg/supervisor.rb', line 9

def registry
  @registry
end

Instance Method Details

#[](k) ⇒ Object

Fetch the supervised instance or pool

Parameters:

  • k (String, Symbol)

    name of supervised item

Returns:

  • (Object)

    supervised object



22
23
24
# File 'lib/zoidberg/supervisor.rb', line 22

def [](k)
  registry[k]
end

#pool(klass, args = {}, &block) ⇒ Object

Supervise a pool

Parameters:

  • klass (Class)

    class of instance

  • args (Hash) (defaults to: {})

    initialization arguments

Options Hash (args):

  • :as (String)

    name of pool

  • :size (Integer)

    size of pool

  • :args (Array<Object>)

    initialization arguments

Returns:

  • (Object)

    new pool



47
48
49
50
51
52
53
54
55
# File 'lib/zoidberg/supervisor.rb', line 47

def pool(klass, args={}, &block)
  name = args[:as]
  size = args[:size].to_i
  args = args.fetch(:args, [])
  klass = supervised_class(klass)
  s_pool = Pool.new(klass, *args, &block)
  s_pool._worker_count(size > 0 ? size : 1)
  registry[name] = s_pool
end

#supervise_as(name, klass, *args, &block) ⇒ Object

Supervise an instance

Parameters:

  • name (String, Symbol)

    name of item to supervise

  • klass (Class)

    class of instance

  • args (Object)

    initialization arguments

Returns:

  • (Object)

    new instance



33
34
35
36
# File 'lib/zoidberg/supervisor.rb', line 33

def supervise_as(name, klass, *args, &block)
  klass = supervised_class(klass)
  registry[name] = klass.new(*args, &block)
end

#terminateObject

Destroy all supervised instances prior to destruction



58
59
60
61
62
# File 'lib/zoidberg/supervisor.rb', line 58

def terminate
  registry.values.each do |item|
    item._zoidberg_destroy!
  end
end