Class: Beaneater::Tubes
- Inherits:
-
Object
- Object
- Beaneater::Tubes
- Includes:
- Enumerable
- Defined in:
- lib/beaneater/tube/collection.rb
Overview
Represents collection of tube related commands.
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
Instance Method Summary collapse
-
#all ⇒ Array<Beaneater::Tube>
List of all known beanstalk tubes.
-
#each(&block) ⇒ Object
Calls the given block once for each known beanstalk tube, passing that element as a parameter.
-
#find(tube_name) ⇒ Beaneater::Tube
(also: #[])
Finds the specified beanstalk tube.
-
#ignore(*names) ⇒ Object
Ignores specified beanstalkd tubes.
-
#initialize(client) ⇒ Tubes
constructor
Creates new tubes instance.
- #last_used ⇒ Object
- #last_used=(tube_name) ⇒ Object
-
#reserve(timeout = nil, &block) {|job| ... } ⇒ Beaneater::Job
Reserves a ready job looking at all watched tubes.
-
#transmit(command, **options) ⇒ Object
Delegates transmit to the connection object.
-
#use(tube) ⇒ Object
Set specified tube as used.
-
#used ⇒ Beaneater::Tube
Currently used beanstalk tube.
-
#watch(*names) ⇒ Object
Add specified beanstalkd tubes as watched.
-
#watch!(*names) ⇒ Object
Add specified beanstalkd tubes as watched and ignores all other tubes.
-
#watched ⇒ Array<Beaneater::Tube>
List of watched beanstalk tubes.
Constructor Details
#initialize(client) ⇒ Tubes
Creates new tubes instance.
19 20 21 |
# File 'lib/beaneater/tube/collection.rb', line 19 def initialize(client) @client = client end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
11 12 13 |
# File 'lib/beaneater/tube/collection.rb', line 11 def client @client end |
Instance Method Details
#all ⇒ Array<Beaneater::Tube>
List of all known beanstalk tubes.
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.
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.
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.
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_used ⇒ Object
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.
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, **) client.connection.transmit(command, **) end |
#use(tube) ⇒ Object
Set specified tube as 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 |
#used ⇒ Beaneater::Tube
Currently used beanstalk tube.
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.
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.
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 |
#watched ⇒ Array<Beaneater::Tube>
List of watched beanstalk tubes.
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 |