class Beaneater::Job

Represents job related commands.

Attributes

body[R]

@!attribute id

@return [Integer] id for the job.

@!attribute body

@return [String] the job's body.

@!attribute reserved

@return [Boolean] whether the job has been reserved.

@!attribute client

@return [Beaneater] returns the client instance
client[R]

@!attribute id

@return [Integer] id for the job.

@!attribute body

@return [String] the job's body.

@!attribute reserved

@return [Boolean] whether the job has been reserved.

@!attribute client

@return [Beaneater] returns the client instance
id[R]

@!attribute id

@return [Integer] id for the job.

@!attribute body

@return [String] the job's body.

@!attribute reserved

@return [Boolean] whether the job has been reserved.

@!attribute client

@return [Beaneater] returns the client instance
reserved[R]

@!attribute id

@return [Integer] id for the job.

@!attribute body

@return [String] the job's body.

@!attribute reserved

@return [Boolean] whether the job has been reserved.

@!attribute client

@return [Beaneater] returns the client instance

Public Class Methods

new(client, res) click to toggle source

Initializes a new job object.

@param [Hash{Symbol => String,Number}] res Result from beanstalkd response

# File lib/beaneater/job/record.rb, line 20
def initialize(client, res)
  @client     = client
  @id         = res[:id]
  @body       = res[:body]
  @reserved   = res[:status] == 'RESERVED'
end

Public Instance Methods

bury(options={}) click to toggle source

Sends command to bury a reserved job.

@param [Hash{Symbol => Integer}] options Settings to bury job @option options [Integer] pri Assign new priority to job @return [Hash{Symbol => String,Number}] Beanstalkd response for the command.

@example

@job.bury({:pri => 100})
  # => {:status=>"BURIED", :body=>nil}

@api public

# File lib/beaneater/job/record.rb, line 38
def bury(options={})
  options = { :pri => stats.pri }.merge(options)
  with_reserved("bury #{id} #{options[:pri]}") do
    @reserved = false
  end
end
delay() click to toggle source

Returns the delay of this job

@return [Integer] The delay of this job @example

@beaneater.jobs.find(123).delay
  # => 5
# File lib/beaneater/job/record.rb, line 178
def delay
  self.stats.delay
end
delete() click to toggle source

Sends command to delete a job.

@return [Hash{Symbol => String,Number}] Beanstalkd response for the command. @example

@beaneater.jobs.find(123).delete
  # => {:status=>"DELETED", :body=>nil}

@api public

# File lib/beaneater/job/record.rb, line 83
def delete
  transmit("delete #{id}") { @reserved = false }
end
exists?() click to toggle source

Check if the job still exists.

@return [Boolean] Returns true if the job still exists @example

@beaneater.jobs.find(123).exists?

@api public

# File lib/beaneater/job/record.rb, line 130
def exists?
  !self.stats.nil?
rescue Beaneater::NotFoundError
  false
end
inspect()
Alias for: to_s
kick() click to toggle source

Sends command to kick a buried job.

@return [Hash{Symbol => String,Number}] Beanstalkd response for the command. @example

@beaneater.jobs.find(123).kick
  # => {:status=>"KICKED", :body=>nil}

@api public

# File lib/beaneater/job/record.rb, line 95
def kick
  transmit("kick-job #{id}")
end
pri() click to toggle source

Returns the pri of this job

@return [Integer] The pri of this job @example

@beaneater.jobs.find(123).pri
  # => 1
# File lib/beaneater/job/record.rb, line 167
def pri
  self.stats.pri
end
release(options={}) click to toggle source

Sends command to release a job back to ready state.

@param [Hash{String => Integer}] options Settings to release job @option options [Integer] pri Assign new priority to job @option options [Integer] delay Assign new delay to job @return [Hash{Symbol => String,Number}] Beanstalkd response for the command. @example

@beaneater.jobs.find(123).release(:pri => 10, :delay => 5)
  # => {:status=>"RELEASED", :body=>nil}

@api public

# File lib/beaneater/job/record.rb, line 56
def release(options={})
  options = { :pri => stats.pri, :delay => stats.delay }.merge(options)
  with_reserved("release #{id} #{options[:pri]} #{options[:delay]}") do
    @reserved = false
  end
end
reserved?() click to toggle source

Check if job is currently in a reserved state.

@return [Boolean] Returns true if the job is in a reserved state @example

@beaneater.jobs.find(123).reserved?

@api public

# File lib/beaneater/job/record.rb, line 119
def reserved?
  @reserved || self.stats.state == "reserved"
end
stats() click to toggle source

Sends command to get stats about job.

@return [Beaneater::StatStruct] struct filled with relevant job stats @example

@beaneater.jobs.find(123).stats
@job.stats.tube # => "some-tube"

@api public

# File lib/beaneater/job/record.rb, line 107
def stats
  res = transmit("stats-job #{id}")
  StatStruct.from_hash(res[:body])
end
to_s() click to toggle source

Returns string representation of job

@return [String] string representation @example

@beaneater.jobs.find(123).to_s
@beaneater.jobs.find(123).inspect
# File lib/beaneater/job/record.rb, line 189
def to_s
  "#<Beaneater::Job id=#{id} body=#{body.inspect}>"
end
Also aliased as: inspect
touch() click to toggle source

Sends command to touch job which extends the ttr.

@return [Hash{Symbol => String,Number}] Beanstalkd response for the command. @example

@beaneater.jobs.find(123).touch
  # => {:status=>"TOUCHED", :body=>nil}

@api public

# File lib/beaneater/job/record.rb, line 71
def touch
  with_reserved("touch #{id}")
end
ttr() click to toggle source

Returns the ttr of this job

@return [Integer] The ttr of this job @example

@beaneater.jobs.find(123).ttr
  # => 123

@api public

# File lib/beaneater/job/record.rb, line 156
def ttr
  @ttr ||= self.stats.ttr
end
tube() click to toggle source

Returns the name of the tube this job is in

@return [String] The name of the tube for this job @example

@beaneater.jobs.find(123).tube
  # => "some-tube"

@api public

# File lib/beaneater/job/record.rb, line 144
def tube
  @tube ||= self.stats.tube
end

Protected Instance Methods

transmit(cmd) { || ... } click to toggle source

Transmit command to beanstalkd instance and fetch response.

@param [String] cmd Beanstalkd command to send. @return [Hash{Symbol => String,Number}] Beanstalkd response for the command. @example

transmit('stats')
transmit('stats') { 'success' }
# File lib/beaneater/job/record.rb, line 204
def transmit(cmd, &block)
  res = client.connection.transmit(cmd)
  yield if block_given?
  res
end
with_reserved(cmd, &block) click to toggle source

Transmits a command which requires the job to be reserved.

@param [String] cmd Beanstalkd command to send. @return [Hash{Symbol => String,Number}] Beanstalkd response for the command. @raise [Beaneater::JobNotReserved] Command cannot execute since job is not reserved. @example

with_reserved("bury 26") { @reserved = false }
# File lib/beaneater/job/record.rb, line 218
def with_reserved(cmd, &block)
  raise JobNotReserved unless reserved?
  transmit(cmd, &block)
end