Class: Woodhouse::Layout

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/woodhouse/layout.rb

Overview

A Layout describes the configuration of a set of Woodhouse Server instances. Each Server runs all of the workers assigned to a single Node.

Layouts and their contents (Node and Worker instances) are all plain data, suitable to being serialized, saved out, passed around, etc.

Woodhouse clients do not need to know anything about the Layout to dispatch jobs, but servers rely on the Layout to know which jobs to serve. The basic process of setting up a Woodhouse server is to create a layout with one or more nodes and then pass it to Woodhouse::Server to serve.

There is a default layout suitable for many applications, available as Woodhouse::Layout.default. It has a single node named :default, which has the default node configuration – one worker for every job. If you do not need to distribute different sets of jobs to different workers, the default layout should serve you.

TODO: A nicer DSL for creating and tweaking Layouts.

Defined Under Namespace

Classes: Changes, Node, Worker

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Layout) initialize

Returns a new instance of Layout



26
27
28
# File 'lib/woodhouse/layout.rb', line 26

def initialize
  @nodes = []
end

Instance Attribute Details

- (Object) nodes

Returns a frozen list of the nodes assigned to this layout.



31
32
33
# File 'lib/woodhouse/layout.rb', line 31

def nodes
  @nodes.frozen? ? @nodes : @nodes.dup.freeze
end

Class Method Details

+ (Object) default

The default layout, for convenience purposes. Has one node :default, which has the default configuration (see Woodhouse::Layout::Node#default_configuration!)



92
93
94
95
96
97
# File 'lib/woodhouse/layout.rb', line 92

def self.default
  new.tap do |layout|
    layout.add_node :default
    layout.node(:default).default_configuration!(Woodhouse.global_configuration)
  end
end

+ (Object) load(dumped, serializer = Woodhouse::LayoutSerializer)



86
87
88
# File 'lib/woodhouse/layout.rb', line 86

def self.load(dumped, serializer = Woodhouse::LayoutSerializer)
  serializer.load(dumped)
end

Instance Method Details

- (Object) add_node(node)

Adds a Node to this layout. If node is a Symbol, a Node will be automatically created with that name.

# Example:

layout.add_node Woodhouse::Layout::Node.new(:isis)

# Is equivalent to

layout.add_node :isis


46
47
48
49
50
51
52
53
# File 'lib/woodhouse/layout.rb', line 46

def add_node(node)
  if node.respond_to?(:to_sym)
    node = Woodhouse::Layout::Node.new(node.to_sym)  
  end
  expect_arg :node, Woodhouse::Layout::Node, node
  @nodes << node
  node
end

- (Object) changes_from(other_layout, node)

Returns a set of Changes necessary to move from other_layout to this layout. This is used to permit live reconfiguration of servers by only spinning up and down nodes/workers which have changed.



78
79
80
# File 'lib/woodhouse/layout.rb', line 78

def changes_from(other_layout, node)
  Woodhouse::Layout::Changes.new(self, other_layout, node)
end

- (Object) dump(serializer = Woodhouse::LayoutSerializer)



82
83
84
# File 'lib/woodhouse/layout.rb', line 82

def dump(serializer = Woodhouse::LayoutSerializer)
  serializer.dump(self)
end

- (Object) frozen_clone

Returns a frozen copy of this Layout and all of its child Node and Worker objects. Woodhouse::Server always takes a frozen copy of the layout it is given. It is thus safe to modify the same layout subsequently, and the changes only take effect when the layout is passed to the server again and Woodhouse::Server#reload is called.



68
69
70
71
72
73
# File 'lib/woodhouse/layout.rb', line 68

def frozen_clone
  clone.tap do |cloned|
    cloned.nodes = @nodes.map{|node| node.frozen_clone }.freeze
    cloned.freeze
  end
end

- (Object) node(name)

Looks up a Node by name and returns it.



56
57
58
59
60
61
# File 'lib/woodhouse/layout.rb', line 56

def node(name)
  name = name.to_sym
  @nodes.detect{|node|
    node.name == name
  }
end