Class: Tuber::Tube
- Inherits:
-
Object
- Object
- Tuber::Tube
- Defined in:
- lib/tuber/tube/record.rb
Overview
Beanstalk tube which contains jobs which can be inserted, reserved, et al.
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#name ⇒ String
Name of the tube.
Instance Method Summary collapse
-
#clear ⇒ Object
Clears all unreserved jobs in all states from the tube.
-
#config ⇒ Tuber::Configuration
protected
Returns configuration options for tuber.
-
#flush ⇒ Integer
Atomically deletes all jobs from the tube.
-
#flush_buried ⇒ Integer
Atomically deletes all buried jobs from the tube.
-
#initialize(client, name) ⇒ Tube
constructor
Fetches the specified tube.
-
#kick(bounds = 1) ⇒ Hash{String => String, Number}
Kick specified number of jobs from buried to ready state.
-
#pause(delay) ⇒ Array<Hash{String => String, Number}>
Pause the execution of this tube for specified
delay. -
#peek(state) ⇒ Tuber::Job
Peek at next job within this tube in given
state. -
#put(body, options = {}) ⇒ Hash{String => String, Number}
Inserts job with specified body onto tube.
-
#reserve(timeout = nil, &block) {|job| ... } ⇒ Tuber::Job
Reserves the next job from tube.
-
#safe_use(&block) ⇒ Object
protected
Transmits a beanstalk command that requires this tube to be set as used.
-
#stats ⇒ Tuber::StatStruct
Returns related stats for this tube.
-
#to_s ⇒ String
(also: #inspect)
String representation of tube.
-
#transmit(command, options = {}) ⇒ Object
Delegates transmit to the connection object.
Constructor Details
#initialize(client, name) ⇒ Tube
Fetches the specified tube.
20 21 22 23 24 |
# File 'lib/tuber/tube/record.rb', line 20 def initialize(client, name) @client = client @name = name.to_s @mutex = Mutex.new end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
11 |
# File 'lib/tuber/tube/record.rb', line 11 attr_reader :name, :client |
#name ⇒ String
Returns name of the tube.
11 12 13 |
# File 'lib/tuber/tube/record.rb', line 11 def name @name end |
Instance Method Details
#clear ⇒ Object
Clears all unreserved jobs in all states from the tube
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/tuber/tube/record.rb', line 179 def clear client.tubes.watch!(self.name) %w(delayed buried ready).each do |state| while job = self.peek(state.to_sym) begin job.delete rescue Tuber::UnexpectedResponse, Tuber::NotFoundError # swallow any issues end end end client.tubes.ignore(name) rescue Tuber::NotIgnoredError # swallow any issues end |
#config ⇒ Tuber::Configuration (protected)
Returns configuration options for tuber
227 228 229 |
# File 'lib/tuber/tube/record.rb', line 227 def config Tuber.configuration end |
#flush ⇒ Integer
Atomically deletes all jobs from the tube.
156 157 158 159 |
# File 'lib/tuber/tube/record.rb', line 156 def flush res = transmit("flush-tube #{name}") res[:id].to_i end |
#flush_buried ⇒ Integer
Atomically deletes all buried jobs from the tube. Ready, delayed, and reserved jobs are left untouched. (Tuber only.)
169 170 171 172 |
# File 'lib/tuber/tube/record.rb', line 169 def flush_buried res = transmit("flush-buried #{name}") res[:id].to_i end |
#kick(bounds = 1) ⇒ Hash{String => String, Number}
Kick specified number of jobs from buried to ready state.
121 122 123 |
# File 'lib/tuber/tube/record.rb', line 121 def kick(bounds=1) safe_use { transmit("kick #{bounds}") } end |
#pause(delay) ⇒ Array<Hash{String => String, Number}>
Pause the execution of this tube for specified delay.
145 146 147 |
# File 'lib/tuber/tube/record.rb', line 145 def pause(delay) transmit("pause-tube #{name} #{delay}") end |
#peek(state) ⇒ Tuber::Job
Peek at next job within this tube in given state.
88 89 90 91 92 93 94 95 96 |
# File 'lib/tuber/tube/record.rb', line 88 def peek(state) safe_use do res = transmit("peek-#{state}") Job.new(client, res) end rescue Tuber::NotFoundError # Return nil if not found nil end |
#put(body, options = {}) ⇒ Hash{String => String, Number}
Inserts job with specified body onto tube.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/tuber/tube/record.rb', line 53 def put(body, ={}) safe_use do serialized_body = config.job_serializer.call(body) = { :pri => config.default_put_pri, :delay => config.default_put_delay, :ttr => config.default_put_ttr }.merge() = "#{[:pri]} #{[:delay]} #{[:ttr]} #{serialized_body.bytesize}" = [] if [:idp] << ([:idp].is_a?(Array) ? "idp:#{[:idp].join(':')}" : "idp:#{[:idp]}") end if [:con] << ([:con].is_a?(Array) ? "con:#{[:con].join(':')}" : "con:#{[:con]}") end << "grp:#{[:grp]}" if [:grp] << "aft:#{[:aft]}" if [:aft] = "#{} #{.join(' ')}" if .any? transmit("put #{}\r\n#{serialized_body}") end end |
#reserve(timeout = nil, &block) {|job| ... } ⇒ Tuber::Job
Reserves the next job from tube.
108 109 110 111 |
# File 'lib/tuber/tube/record.rb', line 108 def reserve(timeout=nil, &block) client.tubes.watch!(self.name) client.tubes.reserve(timeout, &block) end |
#safe_use(&block) ⇒ Object (protected)
Transmits a beanstalk command that requires this tube to be set as used.
216 217 218 219 220 221 222 |
# File 'lib/tuber/tube/record.rb', line 216 def safe_use(&block) @mutex.lock client.tubes.use(self.name) yield ensure @mutex.unlock end |
#stats ⇒ Tuber::StatStruct
Returns related stats for this tube.
132 133 134 135 |
# File 'lib/tuber/tube/record.rb', line 132 def stats res = transmit("stats-tube #{name}") StatStruct.from_hash(res[:body]) end |
#to_s ⇒ String Also known as: inspect
String representation of tube.
201 202 203 |
# File 'lib/tuber/tube/record.rb', line 201 def to_s "#<Tuber::Tube name=#{name.inspect}>" end |
#transmit(command, options = {}) ⇒ Object
Delegates transmit to the connection object.
29 30 31 32 33 34 |
# File 'lib/tuber/tube/record.rb', line 29 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 |