class StalkClimber::Job

Constants

CACHED_ATTRIBUTES

Attributes that return cached values by default

FRESH_ATTRIBUTES

Attributes that are retrieved each request by default

STATS_ATTRIBUTES

All attributes available via a job's stats

STATS_METHODS

Lookup table of which method to call to retrieve each stat

Public Class Methods

new(job_data) → Job click to toggle source

Initializes a Job instance using job_data which should be the Beaneater response to either a put, peek, or stats-job command. Other Beaneater responses are not supported.

No single beanstalk command provides all the data an instance might need, so as more information is required, additional calls are made to beanstalk. For example, accessing both a job's tube and its body requires both a peek and stats-job call.

Put provides only the ID of the job and as such yields the least informed instance. Both a peek and stats-job call may be required to retrieve anything but the ID of the instance

Peek and reserve provide the ID and body of the job. A stats-job call may be required to access anything but the ID or body of the job.

Stats-job provides the most information about the job, but lacks the crtical component of the job body. As such, a peek call would be required to access the body of the job.

reserve_response = connection.transmit('reserve')
StalkClimber::Job.new(reserve_response)
  #=> #<StalkClimber::Job id=1 body="Work to be done">
# File lib/stalk_climber/job.rb, line 100
def initialize(job_data)
  case job_data[:status]
  when 'INSERTED' # put
    @id = job_data[:id].to_i
    @body = @stats = nil
  when 'FOUND', 'RESERVED' # peek, reserve
    @id = job_data[:id].to_i
    @body = job_data[:body]
    @stats = nil
  when 'OK' # stats-job
    @stats = Beaneater::StatStruct.from_hash(job_data[:body])
    @id = @stats.id.to_i
    @body = nil
  else
    raise RuntimeError, "Unexpected job status: #{job_data[:status]}"
  end
  @status = job_data[:status]
  @connection = job_data[:connection]
  # Don't set @reserved to force lookup each time to handle job TTR expiration
end

Public Instance Methods

age(force_refresh = false) → Integer click to toggle source

Retrieves the age of the job from the job's stats. Passing a non-false value for force_refresh will force retrieval of updated stats for the job

job = StalkClimber::Job.new(peek_response)
job.age
  #=> 1024
# File lib/stalk_climber/job.rb, line 169
    
body() → String click to toggle source

Returns or fetches the body of the job. If not already present the body of the job is obtained via the peek command.

job = StalkClimber::Job.new(connection.transmit('reserve'))
job.body
  #=> "Work to be done"
# File lib/stalk_climber/job.rb, line 35
def body
  return @body ||= connection.transmit("peek #{id}")[:body]
end
buries(force_refresh = false) → Integer click to toggle source

Retrieves the number of times the job has been buried from the job's stats. Passing a non-false value for force_refresh will force retrieval of updated stats for the job

job = StalkClimber::Job.new(connection.transmit('peek-buried'))
job.buries
  #=> 1
# File lib/stalk_climber/job.rb, line 180
    
bury() => Hash{Symbol → String,Integer} click to toggle source

Sends command to bury a reserved job. Returns the Beanstalkd response for the command.

job = StalkClimber::Job.new(connection.transmit('reserve'))
job.bury
  #=> {:status=>"BURIED", :body=>nil, :connection=>#<Beaneater::Connection host="localhost" port=11300>}
# File lib/stalk_climber/job.rb, line 211
    
connection() → Beaneater::Connection click to toggle source

Returns the Beaneater::Connection instance which retrieved job.

job = StalkClimber::Job.new(peek_response)
job.connection
  #=> #<Beaneater::Connection host="localhost" port=11300>
# File lib/stalk_climber/job.rb, line 191
    
delay(force_refresh = false) → Integer click to toggle source

Retrieves the the remaining delay before the job is ready from the job's stats. Passing a non-false value for force_refresh will force retrieval of updated stats for the job. By default the value for delay is refreshed every time it is requested.

job = StalkClimber::Job.new(connection.transmit('peek-delayed'))
job.delay
  #=> 1024
# File lib/stalk_climber/job.rb, line 222
    
delete() => Hash{Symbol → String,Integer} click to toggle source

Tries to delete the job from beanstalk. Returns the Beanstalkd response for the command. Raises Beaneater::NotFoundError if the job could not be found.

job = StalkClimber::Job.new(connection.transmit('reserve'))
job.delete
  #=> {:status=>"DELETED", :body=>nil, :connection=>#<Beaneater::Connection host="localhost" port=11300>}
Calls superclass method
# File lib/stalk_climber/job.rb, line 49
def delete
  result = super
  @status = 'DELETED'
  @stats = nil
  @body = nil
  return result
end
exists?() → Boolean click to toggle source

Determines if a job exists by retrieving stats for the job. If Beaneater can't find the job then it does not exist and false is returned. The stats command is used because it will return a response of a near constant size, whereas, depending on the job, the peek command could return a much larger response. Rather than waste the trip to the server, stats are updated each time the method is called.

job = StalkClimber::Job.new(connection.transmit('reserve'))
job.exists?
  #=> true
job.delete
job.exists?
  #=> false
Calls superclass method
# File lib/stalk_climber/job.rb, line 73
def exists?
  return @status == 'DELETED' ? false : super
end
id() → Integer click to toggle source

Returns the id of the job.

job = StalkClimber::Job.new(peek_response)
job.id
  #=> 1
# File lib/stalk_climber/job.rb, line 201
    
Alias for: to_s
kick() => Hash{Symbol → String,Integer} click to toggle source

Sends command to kick a buried job. Returns the Beanstalkd response for the command.

job = StalkClimber::Job.new(connection.transmit('peek-buried'))
job.kick
  #=> {:status=>"KICKED", :body=>nil, :connection=>#<Beaneater::Connection host="localhost" port=11300>}
# File lib/stalk_climber/job.rb, line 234
    
kicks(force_refresh = false) → Integer click to toggle source

Retrieves the number of times the job has been kicked from the job's stats. Passing a non-false value for force_refresh will force retrieval of updated stats for the job By default the value for kicks is cached and will not be updated unless forced.

job = StalkClimber::Job.new(connection.transmit('peek-buried'))
job.kicks
  #=> 5
# File lib/stalk_climber/job.rb, line 244
    
pri(force_refresh = false) → Integer click to toggle source

Retrieves the priority of the job from the job's stats. Passing a non-false value for force_refresh will force retrieval of updated stats for the job. By default the the value for pri is refreshed every time it is requested.

job = StalkClimber::Job.new(connection.transmit('peek-ready'))
job.pri
  #=> 0
# File lib/stalk_climber/job.rb, line 256
    
release(options = {}) => Hash{Symbol → String,Integer} click to toggle source

Sends command to release a job back to ready state. Return value is the Beanstalkd response to the command.

Options may include an Integer pri to set a new pri for the job and/or an Integer delay to assign a new delay to the job

job = StalkClimber::Job.new(connection.transmit('reserve'))
job.release(:delay => 120, :pri => 1024)
  #=> {:status=>"RELEASED", :body=>nil, :connection=>#<Beaneater::Connection host="localhost" port=11300>}
# File lib/stalk_climber/job.rb, line 268
    
releases(force_refresh = false) → Integer click to toggle source

Retrieves the number of times the job has been released from the job's stats. Passing a non-false value for force_refresh will force retrieval of updated stats for the job By default the value for releases is cached and will not be updated unless forced.

job = StalkClimber::Job.new(connection.transmit('peek-ready'))
job.releases
  #=> 3
# File lib/stalk_climber/job.rb, line 282
    
reserved?() → Boolean click to toggle source

Returns true if a job is currently in a reserved state.

job = StalkClimber::Job.new(connection.transmit('reserve'))
job.reserved?
  #=> true
# File lib/stalk_climber/job.rb, line 294
    
reserves(force_refresh = false) → Integer click to toggle source

Retrieves the number of times the job has been reserved from the job's stats. Passing a non-false value for force_refresh will force retrieval of updated stats for the job By default the value for reserves is cached and will not be updated unless forced.

job = StalkClimber::Job.new(connection.transmit('reserve'))
job.reserves
  #=> 1
# File lib/stalk_climber/job.rb, line 304
    
state(force_refresh = false) → String click to toggle source

Retrieves the state of the job from the job's stats. Value will be one of “ready”, “delayed”, “reserved”, or “buried”. Passing a non-false value for force_refresh will force retrieval of updated stats for the job By default the the value for state is refreshed every time it is requested.

job = StalkClimber::Job.new(connection.transmit('peek-ready'))
job.state
  #=> 'ready'
# File lib/stalk_climber/job.rb, line 316
    
stats(force_refresh = true) → Beaneater::StatStruct click to toggle source

Returns or retrieves stats for the job. Optionally, a retrieve may be forced by passing a non-false value for force_refresh

job = StalkClimber::Job.new(peek_response)
job.stats
  #=> #<Beaneater::StatStruct id=2523, tube="stalk_climber", state="ready", pri=0, age=25, delay=0, ttr=120, time_left=0, file=0, reserves=0, timeouts=0, releases=0, buries=0, kicks=0>
Calls superclass method
# File lib/stalk_climber/job.rb, line 131
def stats(force_refresh = true)
  if @stats.nil? || force_refresh
    @stats = super()
  end
  return @stats
end
time_left(force_refresh = false) → Integer click to toggle source

Retrieves the number of seconds left until the server puts this job into the ready queue. This number is only meaningful if the job is reserved or delayed. If the job is reserved and this amount of time elapses before its state changes, it is considered to have timed out. Passing a non-false value for force_refresh will force retrieval of updated stats for the job By default the the value for time_left is refreshed every time it is requested.

job = StalkClimber::Job.new(connection.transmit('reserve'))
job.time_left
  #=> 119
# File lib/stalk_climber/job.rb, line 329
    
timeouts(force_refresh = false) → Integer click to toggle source

Retrieves the number of times the job has timed out from the job's stats. Passing a non-false value for force_refresh will force retrieval of updated stats for the job By default the value for timeouts is cached and will not be updated unless forced.

job = StalkClimber::Job.new(connection.transmit('reserve'))
job.timeouts
  #=> 0
# File lib/stalk_climber/job.rb, line 344
    
to_h() → Hash click to toggle source

Returns a hash of all job attributes derived from updated stats

job = StalkClimber::Job.new(peek_response)
job.to_h
  #=> {"age"=>144, "body"=>"Work to be done", "buries"=>0, "connection"=>#<Beaneater::Connection host="localhost" port=11300>, "delay"=>0, "id"=>2523, "kicks"=>0, "pri"=>0, "releases"=>0, "reserves"=>0, "state"=>"ready", "time-left"=>0, "timeouts"=>0, "ttr"=>120, "tube"=>"stalk_climber"}
# File lib/stalk_climber/job.rb, line 147
def to_h
  stats
  stats_pairs = STATS_METHODS.map { |stat, method_name| [stat, stats(false)[method_name]]}
  stats_pairs.concat([['body', body], ['connection', connection], ['id', id]]).sort_by!(&:first)
  return Hash[stats_pairs]
end
to_s() → String click to toggle source

Returns string representation of job

job = StalkClimber::Job.new(peek_response)
job.to_s
  #=> #<StalkClimber::Job id=1 body="Work to be done">
# File lib/stalk_climber/job.rb, line 163
def to_s
  "#<StalkClimber::Job id=#{id} body=#{body.inspect}>"
end
Also aliased as: inspect
touch => Hash{Symbol → String,Integer} click to toggle source

Sends command to touch job which extends the ttr. Returns the Beanstalkd response for the command.

job = StalkClimber::Job.new(connection.transmit('reserve'))
job.touch
  #=> {:status=>"TOUCHED", :body=>nil, :connection=>#<Beaneater::Connection host="localhost" port=11300>}
# File lib/stalk_climber/job.rb, line 356
    
ttr(force_refresh = false) → Integer click to toggle source

Retrieves the time to run for the job, the number of seconds a worker is allowed to work to run the job. Passing a non-false value for force_refresh will force retrieval of updated stats for the job By default the value for ttr is cached and will not be updated unless forced.

job = StalkClimber::Job.new(connection.transmit('reserve'))
job.ttr
  #=> 120
# File lib/stalk_climber/job.rb, line 367
    
tube → String click to toggle source

Retrieves the name of the tube that contains job. Passing a non-false value for force_refresh will force retrieval of updated stats for the job By default the value for tube is cached and will not be updated unless forced.

job = StalkClimber::Job.new(connection.transmit('reserve'))
job.tube
  #=> "stalk_climber"
# File lib/stalk_climber/job.rb, line 380