Class: Beaneater::Tubes

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/beaneater/tube/collection.rb

Overview

Represents collection of tube related commands.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Tubes

Creates new tubes instance.

Examples:

Beaneater::Tubes.new(@client)

Parameters:

  • client (Beaneater)

    The beaneater client instance.



17
18
19
# File 'lib/beaneater/tube/collection.rb', line 17

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



9
10
11
# File 'lib/beaneater/tube/collection.rb', line 9

def client
  @client
end

Instance Method Details

#allArray<Beaneater::Tube>

List of all known beanstalk tubes.

Examples:

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

Returns:



78
79
80
81
82
# File 'lib/beaneater/tube/collection.rb', line 78

def all
  transmit('list-tubes')[:body].map do |tube_name|
    Tube.new(client, tube_name)
  end
end

#each(&block) ⇒ Object

Calls the given block once for each known beanstalk tube, passing that element as a parameter.

Examples:

@pool.tubes.each {|t| puts t.name}

Returns:

  • An Enumerator is returned if no block is given.



91
92
93
# File 'lib/beaneater/tube/collection.rb', line 91

def each(&block)
  all.each(&block)
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:



46
47
48
# File 'lib/beaneater/tube/collection.rb', line 46

def find(tube_name)
  Tube.new(client, tube_name)
end

#ignore(*names) ⇒ Object

Ignores specified beanstalkd tubes.

Examples:

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

Parameters:

  • names (*String)

    Name of tubes to ignore



162
163
164
165
166
167
# File 'lib/beaneater/tube/collection.rb', line 162

def ignore(*names)
  names.each do |w|
    transmit "ignore #{w}"
    client.connection.remove_from_watched(w)
  end
end

#last_usedObject



21
22
23
# File 'lib/beaneater/tube/collection.rb', line 21

def last_used
  client.connection.tube_used
end

#last_used=(tube_name) ⇒ Object



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

def last_used=(tube_name)
  client.connection.tube_used = tube_name
end

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

Reserves a ready job looking at all watched tubes.

Examples:

@client.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:



62
63
64
65
66
67
68
# File 'lib/beaneater/tube/collection.rb', line 62

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

#transmit(command, **options) ⇒ Object

Delegates transmit to the connection object.



32
33
34
# File 'lib/beaneater/tube/collection.rb', line 32

def transmit(command, **options)
  client.connection.transmit(command, **options)
end

#use(tube) ⇒ Object

Set specified tube as used.

Examples:

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

Parameters:

  • tube (String)

    Tube to be used.



175
176
177
178
179
180
181
# File 'lib/beaneater/tube/collection.rb', line 175

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

#usedBeaneater::Tube

Currently used beanstalk tube.

Examples:

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

Returns:



119
120
121
122
# File 'lib/beaneater/tube/collection.rb', line 119

def used
  last_used = transmit('list-tube-used')[:id]
  Tube.new(client, last_used)
end

#watch(*names) ⇒ Object

Add specified beanstalkd tubes as watched.

Examples:

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

Parameters:

  • names (*String)

    Name of tubes to watch

Raises:



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

def watch(*names)
  names.each do |t|
    transmit "watch #{t}"
    client.connection.add_to_watched(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:

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

Parameters:

  • names (*String)

    Name of tubes to watch

Raises:



149
150
151
152
153
# File 'lib/beaneater/tube/collection.rb', line 149

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:

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

Returns:



103
104
105
106
107
108
109
# File 'lib/beaneater/tube/collection.rb', line 103

def watched
  last_watched = transmit('list-tubes-watched')[:body]
  client.connection.tubes_watched = last_watched.dup
  last_watched.map do |tube_name|
    Tube.new(client, tube_name)
  end
end