Class: Tuber::Tubes
- Inherits:
-
Object
- Object
- Tuber::Tubes
- Includes:
- Enumerable
- Defined in:
- lib/tuber/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<Tuber::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) ⇒ Tuber::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| ... } ⇒ Tuber::Job
Reserves a ready job looking at all watched tubes.
-
#reserve_batch(count, timeout = nil) ⇒ Array<Tuber::Job>
Reserves a batch of ready jobs from watched tubes.
-
#reserve_job(id) ⇒ Tuber::Job?
Reserves a specific job by its ID.
-
#reserve_mode(mode) ⇒ Hash
Sets the reserve mode for the connection.
-
#stats_group(group) ⇒ Tuber::StatStruct
Returns stats for a job group.
-
#transmit(command, **options) ⇒ Object
Delegates transmit to the connection object.
-
#use(tube) ⇒ Object
Set specified tube as used.
-
#used ⇒ Tuber::Tube
Currently used beanstalk tube.
-
#watch(*names, weight: nil) ⇒ Object
Add specified beanstalkd tubes as watched.
-
#watch!(*names) ⇒ Object
Add specified beanstalkd tubes as watched and ignores all other tubes.
-
#watched ⇒ Array<Tuber::Tube>
List of watched beanstalk tubes.
Constructor Details
#initialize(client) ⇒ Tubes
Creates new tubes instance.
19 20 21 |
# File 'lib/tuber/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/tuber/tube/collection.rb', line 11 def client @client end |
Instance Method Details
#all ⇒ Array<Tuber::Tube>
List of all known beanstalk tubes.
147 148 149 150 151 |
# File 'lib/tuber/tube/collection.rb', line 147 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.
160 161 162 |
# File 'lib/tuber/tube/collection.rb', line 160 def each(&block) all.each(&block) end |
#find(tube_name) ⇒ Tuber::Tube Also known as: []
Finds the specified beanstalk tube.
51 52 53 |
# File 'lib/tuber/tube/collection.rb', line 51 def find(tube_name) Tube.new(client, tube_name) end |
#ignore(*names) ⇒ Object
Ignores specified beanstalkd tubes.
232 233 234 235 236 237 |
# File 'lib/tuber/tube/collection.rb', line 232 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/tuber/tube/collection.rb', line 23 def last_used client.connection.tube_used end |
#last_used=(tube_name) ⇒ Object
27 28 29 |
# File 'lib/tuber/tube/collection.rb', line 27 def last_used=(tube_name) client.connection.tube_used = tube_name end |
#reserve(timeout = nil, &block) {|job| ... } ⇒ Tuber::Job
Reserves a ready job looking at all watched tubes.
67 68 69 70 71 72 73 |
# File 'lib/tuber/tube/collection.rb', line 67 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 |
#reserve_batch(count, timeout = nil) ⇒ Array<Tuber::Job>
Reserves a batch of ready jobs from watched tubes.
Without a +timeout+ the call is non-blocking and may return an empty array. With a positive +timeout+ it long-polls, blocking up to +timeout+ seconds for the first job before draining whatever is ready, up to +count+.
91 92 93 94 |
# File 'lib/tuber/tube/collection.rb', line 91 def reserve_batch(count, timeout = nil) results = client.connection.reserve_batch(count, timeout) results.map { |res| Job.new(client, res) } end |
#reserve_job(id) ⇒ Tuber::Job?
Reserves a specific job by its ID.
118 119 120 121 122 123 |
# File 'lib/tuber/tube/collection.rb', line 118 def reserve_job(id) res = transmit("reserve-job #{id}") Job.new(client, res) rescue Tuber::NotFoundError nil end |
#reserve_mode(mode) ⇒ Hash
Sets the reserve mode for the connection.
105 106 107 |
# File 'lib/tuber/tube/collection.rb', line 105 def reserve_mode(mode) transmit("reserve-mode #{mode}") end |
#stats_group(group) ⇒ Tuber::StatStruct
Returns stats for a job group.
134 135 136 137 |
# File 'lib/tuber/tube/collection.rb', line 134 def stats_group(group) res = transmit("stats-group #{group}") StatStruct.from_hash(res[:body]) end |
#transmit(command, **options) ⇒ Object
Delegates transmit to the connection object.
34 35 36 37 38 39 |
# File 'lib/tuber/tube/collection.rb', line 34 def transmit(command, **) # Empty **options must not be forwarded: on Ruby 2.7 it arrives as an # extra positional {} at the receiver. return client.connection.transmit(command) if .empty? client.connection.transmit(command, **) end |
#use(tube) ⇒ Object
Set specified tube as used.
245 246 247 248 249 250 251 |
# File 'lib/tuber/tube/collection.rb', line 245 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 ⇒ Tuber::Tube
Currently used beanstalk tube.
188 189 190 191 |
# File 'lib/tuber/tube/collection.rb', line 188 def used last_used = transmit('list-tube-used')[:id] Tube.new(client, last_used) end |
#watch(*names, weight: nil) ⇒ Object
Add specified beanstalkd tubes as watched.
201 202 203 204 205 206 207 208 209 |
# File 'lib/tuber/tube/collection.rb', line 201 def watch(*names, weight: nil) names.each do |t| cmd = weight ? "watch #{t} #{weight}" : "watch #{t}" transmit cmd 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.
219 220 221 222 223 |
# File 'lib/tuber/tube/collection.rb', line 219 def watch!(*names) old_tubes = watched.map(&:name) - names.map(&:to_s) watch(*names) ignore(*old_tubes) end |
#watched ⇒ Array<Tuber::Tube>
List of watched beanstalk tubes.
172 173 174 175 176 177 178 |
# File 'lib/tuber/tube/collection.rb', line 172 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 |