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.
17 18 19 |
# File 'lib/beaneater/tube/collection.rb', line 17 def initialize(client) @client = client end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
9 10 11 |
# File 'lib/beaneater/tube/collection.rb', line 9 def client @client end |
Instance Method Details
#all ⇒ Array<Beaneater::Tube>
List of all known beanstalk tubes.
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.
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.
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.
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_used ⇒ Object
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.
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, **) client.connection.transmit(command, **) end |
#use(tube) ⇒ Object
Set specified tube as 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 |
#used ⇒ Beaneater::Tube
Currently used beanstalk tube.
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.
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.
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 |
#watched ⇒ Array<Beaneater::Tube>
List of watched beanstalk tubes.
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 |