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.



19
20
21
# File 'lib/beaneater/tube/collection.rb', line 19

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



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

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:



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

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.



93
94
95
# File 'lib/beaneater/tube/collection.rb', line 93

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:



48
49
50
# File 'lib/beaneater/tube/collection.rb', line 48

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



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

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

#last_usedObject



23
24
25
# File 'lib/beaneater/tube/collection.rb', line 23

def last_used
  client.connection.tube_used
end

#last_used=(tube_name) ⇒ Object



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

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:



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

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.



34
35
36
# File 'lib/beaneater/tube/collection.rb', line 34

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.



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

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:



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

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:



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

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:



151
152
153
154
155
# File 'lib/beaneater/tube/collection.rb', line 151

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:



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

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