Class: Beaneater::Tubes

Inherits:
PoolCommand show all
Defined in:
lib/beaneater/tube/collection.rb

Overview

Represents collection of tube related commands.

Instance Attribute Summary

Attributes inherited from PoolCommand

#pool

Instance Method Summary collapse

Methods inherited from PoolCommand

#combine_stats, #method_missing, #sum_items, #transmit_to_all

Constructor Details

#initialize(pool) ⇒ Tubes

Creates new tubes instance.

Examples:

Beaneater::Tubes.new(@pool)

Parameters:


11
12
13
14
# File 'lib/beaneater/tube/collection.rb', line 11

def initialize(pool)
  @last_used = 'default'
  super
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Beaneater::PoolCommand

Instance Method Details

#allArray<Beaneater::Tube>

List of all known beanstalk tubes.

Examples:

@pool.tubes.all
  # => [<Beaneater::Tube name="tube2">, <Beaneater::Tube name="tube3">]

Returns:


57
58
59
# File 'lib/beaneater/tube/collection.rb', line 57

def all
  transmit_to_all('list-tubes', :merge => true)[:body].map { |tube_name| Tube.new(self.pool, tube_name) }
end

#find(tube_name) ⇒ Beaneater::Tube Also known as: []

Finds the specified beanstalk tube.

Examples:

@pool.tubes.find('tube2')
@pool.tubes['tube2']
  # => <Beaneater::Tube name="tube2">

Parameters:

  • tube_name (String)

    Name of the beanstalkd tube

Returns:


26
27
28
# File 'lib/beaneater/tube/collection.rb', line 26

def find(tube_name)
  Tube.new(self.pool, tube_name)
end

#ignore(*names) ⇒ Object

Ignores specified beanstalkd tubes.

Examples:

@pool.tubes.ignore('foo', 'bar')

Parameters:

  • names (*String)

    Name of tubes to ignore


122
123
124
125
126
# File 'lib/beaneater/tube/collection.rb', line 122

def ignore(*names)
  names.each do |w|
    transmit_to_all "ignore #{w}"
  end
end

#reserve(timeout = nil, &block) {|job| ... } ⇒ Beaneater::Job

Reserves a ready job looking at all watched tubes.

Examples:

@conn.tubes.reserve { |job| process(job) }
  # => <Beaneater::Job id=5 body="foo">

Parameters:

  • timeout (Integer) (defaults to: nil)

    Number of seconds before timing out.

  • block (Proc)

    Callback to perform on the reserved job.

Yields:

  • (job)

    Reserved beaneater job.

Returns:


42
43
44
45
46
47
# File 'lib/beaneater/tube/collection.rb', line 42

def reserve(timeout=nil, &block)
  res = transmit_to_rand(timeout ? "reserve-with-timeout #{timeout}" : 'reserve')
  job = Job.new(res)
  block.call(job) if block_given?
  job
end

#use(tube) ⇒ Object

Set specified tube as used.

Examples:

@conn.tubes.use("some-tube")

Parameters:

  • tube (String)

    Tube to be used.


134
135
136
137
138
139
140
# File 'lib/beaneater/tube/collection.rb', line 134

def use(tube)
  return tube if @last_used == tube
  transmit_to_all("use #{tube}")
  @last_used = tube
rescue BadFormatError
  raise InvalidTubeName, "Tube cannot be named '#{tube}'"
end

#usedBeaneater::Tube

Currently used beanstalk tube.

Examples:

@pool.tubes.used
  # => <Beaneater::Tube name="tube2">

Returns:


81
82
83
# File 'lib/beaneater/tube/collection.rb', line 81

def used
  Tube.new(self.pool, transmit_to_rand('list-tube-used')[:id])
end

#watch(*names) ⇒ Object

Add specified beanstalkd tubes as watched.

Examples:

@pool.tubes.watch('foo', 'bar')

Parameters:

  • names (*String)

    Name of tubes to watch

Raises:


93
94
95
96
97
98
99
# File 'lib/beaneater/tube/collection.rb', line 93

def watch(*names)
  names.each do |t|
    transmit_to_all "watch #{t}"
  end
rescue BadFormatError => ex
  raise InvalidTubeName, "Tube in '#{ex.cmd}' is invalid!"
end

#watch!(*names) ⇒ Object

Add specified beanstalkd tubes as watched and ignores all other tubes.

Examples:

@pool.tubes.watch!('foo', 'bar')

Parameters:

  • names (*String)

    Name of tubes to watch

Raises:


109
110
111
112
113
# File 'lib/beaneater/tube/collection.rb', line 109

def watch!(*names)
  old_tubes = watched.map(&:name) - names.map(&:to_s)
  watch(*names)
  ignore(*old_tubes)
end

#watchedArray<Beaneater::Tube>

List of watched beanstalk tubes.

Examples:

@pool.tubes.watched
  # => [<Beaneater::Tube name="tube2">, <Beaneater::Tube name="tube3">]

Returns:


69
70
71
# File 'lib/beaneater/tube/collection.rb', line 69

def watched
  transmit_to_all('list-tubes-watched', :merge => true)[:body].map { |tube_name| Tube.new(self.pool, tube_name) }
end