Class: Beaneater::Job
- Inherits:
-
Object
- Object
- Beaneater::Job
- Defined in:
- lib/beaneater/job/record.rb
Overview
Represents job related commands.
Instance Attribute Summary collapse
-
#body ⇒ String
The job's body.
-
#client ⇒ Object
Returns the value of attribute client.
-
#id ⇒ Integer
Id for the job.
-
#reserved ⇒ Boolean
Whether the job has been reserved.
Instance Method Summary collapse
-
#bury(options = {}) ⇒ Hash{Symbol => String,Number}
Sends command to bury a reserved job.
-
#delay ⇒ Integer
Returns the delay of this job.
-
#delete ⇒ Hash{Symbol => String,Number}
Sends command to delete a job.
-
#exists? ⇒ Boolean
Check if the job still exists.
-
#initialize(client, res) ⇒ Job
constructor
Initializes a new job object.
-
#kick ⇒ Hash{Symbol => String,Number}
Sends command to kick a buried job.
-
#pri ⇒ Integer
Returns the pri of this job.
-
#release(options = {}) ⇒ Hash{Symbol => String,Number}
Sends command to release a job back to ready state.
-
#reserved? ⇒ Boolean
Check if job is currently in a reserved state.
-
#stats ⇒ Beaneater::StatStruct
Sends command to get stats about job.
-
#to_s ⇒ String
(also: #inspect)
Returns string representation of job.
-
#touch ⇒ Hash{Symbol => String,Number}
Sends command to touch job which extends the ttr.
-
#transmit(cmd, &block) ⇒ Hash{Symbol => String,Number}
protected
Transmit command to beanstalkd instance and fetch response.
-
#ttr ⇒ Integer
Returns the ttr of this job.
-
#tube ⇒ String
Returns the name of the tube this job is in.
-
#with_reserved(cmd, &block) ⇒ Hash{Symbol => String,Number}
protected
Transmits a command which requires the job to be reserved.
Constructor Details
#initialize(client, res) ⇒ Job
Initializes a new job object.
22 23 24 25 26 27 |
# File 'lib/beaneater/job/record.rb', line 22 def initialize(client, res) @client = client @id = res[:id] @body = res[:body] @reserved = res[:status] == 'RESERVED' end |
Instance Attribute Details
#body ⇒ String
Returns the job's body.
15 |
# File 'lib/beaneater/job/record.rb', line 15 attr_reader :id, :body, :reserved, :client |
#client ⇒ Object
Returns the value of attribute client.
15 |
# File 'lib/beaneater/job/record.rb', line 15 attr_reader :id, :body, :reserved, :client |
#id ⇒ Integer
Returns id for the job.
15 16 17 |
# File 'lib/beaneater/job/record.rb', line 15 def id @id end |
#reserved ⇒ Boolean
Returns whether the job has been reserved.
15 |
# File 'lib/beaneater/job/record.rb', line 15 attr_reader :id, :body, :reserved, :client |
Instance Method Details
#bury(options = {}) ⇒ Hash{Symbol => String,Number}
Sends command to bury a reserved job.
40 41 42 43 44 45 |
# File 'lib/beaneater/job/record.rb', line 40 def bury(={}) = { :pri => stats.pri }.merge() with_reserved("bury #{id} #{options[:pri]}") do @reserved = false end end |
#delay ⇒ Integer
Returns the delay of this job
180 181 182 |
# File 'lib/beaneater/job/record.rb', line 180 def delay self.stats.delay end |
#delete ⇒ Hash{Symbol => String,Number}
Sends command to delete a job.
85 86 87 |
# File 'lib/beaneater/job/record.rb', line 85 def delete transmit("delete #{id}") { @reserved = false } end |
#exists? ⇒ Boolean
Check if the job still exists.
132 133 134 135 136 |
# File 'lib/beaneater/job/record.rb', line 132 def exists? !self.stats.nil? rescue Beaneater::NotFoundError false end |
#kick ⇒ Hash{Symbol => String,Number}
Sends command to kick a buried job.
97 98 99 |
# File 'lib/beaneater/job/record.rb', line 97 def kick transmit("kick-job #{id}") end |
#pri ⇒ Integer
Returns the pri of this job
169 170 171 |
# File 'lib/beaneater/job/record.rb', line 169 def pri self.stats.pri end |
#release(options = {}) ⇒ Hash{Symbol => String,Number}
Sends command to release a job back to ready state.
58 59 60 61 62 63 |
# File 'lib/beaneater/job/record.rb', line 58 def release(={}) = { :pri => stats.pri, :delay => stats.delay }.merge() with_reserved("release #{id} #{options[:pri]} #{options[:delay]}") do @reserved = false end end |
#reserved? ⇒ Boolean
Check if job is currently in a reserved state.
121 122 123 |
# File 'lib/beaneater/job/record.rb', line 121 def reserved? @reserved || self.stats.state == "reserved" end |
#stats ⇒ Beaneater::StatStruct
Sends command to get stats about job.
109 110 111 112 |
# File 'lib/beaneater/job/record.rb', line 109 def stats res = transmit("stats-job #{id}") StatStruct.from_hash(res[:body]) end |
#to_s ⇒ String Also known as: inspect
Returns string representation of job
191 192 193 |
# File 'lib/beaneater/job/record.rb', line 191 def to_s "#<Beaneater::Job id=#{id} body=#{body.inspect}>" end |
#touch ⇒ Hash{Symbol => String,Number}
Sends command to touch job which extends the ttr.
73 74 75 |
# File 'lib/beaneater/job/record.rb', line 73 def touch with_reserved("touch #{id}") end |
#transmit(cmd, &block) ⇒ Hash{Symbol => String,Number} (protected)
Transmit command to beanstalkd instance and fetch response.
206 207 208 209 210 |
# File 'lib/beaneater/job/record.rb', line 206 def transmit(cmd, &block) res = client.connection.transmit(cmd) yield if block_given? res end |
#ttr ⇒ Integer
Returns the ttr of this job
158 159 160 |
# File 'lib/beaneater/job/record.rb', line 158 def ttr @ttr ||= self.stats.ttr end |
#tube ⇒ String
Returns the name of the tube this job is in
146 147 148 |
# File 'lib/beaneater/job/record.rb', line 146 def tube @tube ||= self.stats.tube end |
#with_reserved(cmd, &block) ⇒ Hash{Symbol => String,Number} (protected)
Transmits a command which requires the job to be reserved.
220 221 222 223 |
# File 'lib/beaneater/job/record.rb', line 220 def with_reserved(cmd, &block) raise JobNotReserved unless reserved? transmit(cmd, &block) end |