Class: Beaneater::PoolCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/beaneater/pool_command.rb

Overview

Represents collection of pool related commands.

Direct Known Subclasses

Jobs, Stats, Tube, Tubes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool) ⇒ PoolCommand

Initialize new connection

Parameters:


13
14
15
# File 'lib/beaneater/pool_command.rb', line 13

def initialize(pool)
  @pool = pool
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Delegate missing methods to pool


38
39
40
41
42
43
44
# File 'lib/beaneater/pool_command.rb', line 38

def method_missing(name, *args, &block)
  if pool.respond_to?(name)
    pool.send(name, *args, &block)
  else # not a known pool command
    super
  end
end

Instance Attribute Details

#poolObject

Returns the value of attribute pool


8
9
10
# File 'lib/beaneater/pool_command.rb', line 8

def pool
  @pool
end

Instance Method Details

#combine_stats(k, a, b) ⇒ Set, Integer (protected)

Combine two values for given key

Examples:

self.combine_stats('total_connections', 4, 5) # => 9

Parameters:

  • k (String)

    key name within response hash

Returns:

  • (Set, Integer)

    combined value for stat


75
76
77
# File 'lib/beaneater/pool_command.rb', line 75

def combine_stats(k, a, b)
  ['name', 'version', 'pid'].include?(k) ? Set[a] + Set[b] : a + b
end

#sum_items(items) ⇒ Hash{Symbol => String} (protected)

Selects items from collection and then merges the individual values Supports array of hashes or array of arrays

Examples:

self.sum_items([{ :foo => 1, :bar => 5 }, { :foo => 2, :bar => 3 }])
  => { :foo => 3, :bar => 8 }
self.sum_items([['foo', 'bar'], ['foo', 'bar', 'baz']])
  => ['foo', 'bar', 'baz']

Parameters:

  • hs (Array<Hash, Array>)

    Collection of responses returned from beanstalkd

Returns:

  • (Hash{Symbol => String})

    Merged responses combining values from all the hash bodies


59
60
61
62
63
64
65
66
# File 'lib/beaneater/pool_command.rb', line 59

def sum_items(items)
  if items.first.is_a?(Hash)
    items.select { |h| h.is_a?(Hash) }.
      inject({}) { |a,b| a.merge(b) { |k,o,n| combine_stats(k, o, n) } }
  elsif items.first.is_a?(Array)
    items.flatten.uniq
  end
end

#transmit_to_all(body, options = {}, &block) ⇒ Object

Delegate to Pool#transmit_to_all and if needed will merge responses from beanstalkd.

Examples:

@pool.transmit_to_all("stats")

Parameters:

  • body (String)

    Beanstalkd command

  • options (Hash{String => String, Boolean}) (defaults to: {})

    socket connections options

  • block (Proc)

    Block passed in socket connection object

Options Hash (options):

  • merge (Boolean)

    Ask for merging responses or not


26
27
28
29
30
31
32
33
34
# File 'lib/beaneater/pool_command.rb', line 26

def transmit_to_all(body, options={}, &block)
  merge = options.delete(:merge)
  res = pool.transmit_to_all(body, options, &block)
  first = res.find { |r| r && r[:status] }
  if first && merge
    res = { :status => first[:status], :body => sum_items(res.map { |r| r[:body] }) }
  end
  res
end