Class: Beaneater::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/beaneater/job/record.rb

Overview

Represents job related commands.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(res) ⇒ Job

Initializes a new job object.

Parameters:

  • res (Hash{Symbol => String,Number})

    Result from beanstalkd response


20
21
22
23
24
25
# File 'lib/beaneater/job/record.rb', line 20

def initialize(res)
  @id         = res[:id]
  @body       = res[:body]
  @connection = res[:connection]
  @reserved   = res[:status] == 'RESERVED'
end

Instance Attribute Details

#bodyString

Returns the job's body.

Returns:

  • (String)

    the job's body.


13
# File 'lib/beaneater/job/record.rb', line 13

attr_reader :id, :body, :connection, :reserved

#connectionBeaneater::Connection

Returns connection which has retrieved job.

Returns:


13
# File 'lib/beaneater/job/record.rb', line 13

attr_reader :id, :body, :connection, :reserved

#idInteger

Returns id for the job.

Returns:

  • (Integer)

    id for the job.


13
14
15
# File 'lib/beaneater/job/record.rb', line 13

def id
  @id
end

#reservedObject

Returns the value of attribute reserved


13
# File 'lib/beaneater/job/record.rb', line 13

attr_reader :id, :body, :connection, :reserved

Instance Method Details

#bury(options = {}) ⇒ Hash{Symbol => String,Number}

Sends command to bury a reserved job.

Examples:

@beaneater_connection.bury({:pri => 100})
  # => {:status=>"BURIED", :body=>nil, :connection=>#<Beaneater::Connection host="localhost" port=11300>}

Parameters:

  • options (Hash{Symbol => Integer}) (defaults to: {})

    Settings to bury job

Options Hash (options):

  • pri (Integer)

    Assign new priority to job

Returns:

  • (Hash{Symbol => String,Number})

    Beanstalkd response for the command.


38
39
40
41
42
43
# 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

#delayInteger

Returns the delay of this job

Examples:

@beaneater_connection.jobs.find(123).delay
  # => 5

Returns:

  • (Integer)

    The delay of this job


178
179
180
# File 'lib/beaneater/job/record.rb', line 178

def delay
  self.stats.delay
end

#deleteHash{Symbol => String,Number}

Sends command to delete a job.

Examples:

@beaneater_connection.jobs.find(123).delete
  # => {:status=>"DELETED", :body=>nil, :connection=>#<Beaneater::Connection host="localhost" port=11300>}

Returns:

  • (Hash{Symbol => String,Number})

    Beanstalkd response for the command.


83
84
85
# File 'lib/beaneater/job/record.rb', line 83

def delete
  transmit("delete #{id}") { @reserved = false }
end

#exists?Boolean

Check if the job still exists.

Examples:

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

Returns:

  • (Boolean)

    Returns true if the job still exists


130
131
132
133
134
# File 'lib/beaneater/job/record.rb', line 130

def exists?
  !self.stats.nil?
rescue Beaneater::NotFoundError
  false
end

#kickHash{Symbol => String,Number}

Sends command to kick a buried job.

Examples:

@beaneater_connection.jobs.find(123).kick
  # => {:status=>"KICKED", :body=>nil, :connection=>#<Beaneater::Connection host="localhost" port=11300>}

Returns:

  • (Hash{Symbol => String,Number})

    Beanstalkd response for the command.


95
96
97
# File 'lib/beaneater/job/record.rb', line 95

def kick
  transmit("kick-job #{id}")
end

#priInteger

Returns the pri of this job

Examples:

@beaneater_connection.jobs.find(123).pri
  # => 1

Returns:

  • (Integer)

    The pri of this job


167
168
169
# File 'lib/beaneater/job/record.rb', line 167

def pri
  self.stats.pri
end

#release(options = {}) ⇒ Hash{Symbol => String,Number}

Sends command to release a job back to ready state.

Examples:

@beaneater_connection.jobs.find(123).release(:pri => 10, :delay => 5)
  # => {:status=>"RELEASED", :body=>nil, :connection=>#<Beaneater::Connection host="localhost" port=11300>}

Parameters:

  • options (Hash{String => Integer}) (defaults to: {})

    Settings to release job

Options Hash (options):

  • pri (Integer)

    Assign new priority to job

  • delay (Integer)

    Assign new delay to job

Returns:

  • (Hash{Symbol => String,Number})

    Beanstalkd response for the command.


56
57
58
59
60
61
# 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?Boolean

Check if job is currently in a reserved state.

Examples:

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

Returns:

  • (Boolean)

    Returns true if the job is in a reserved state


119
120
121
# File 'lib/beaneater/job/record.rb', line 119

def reserved?
  @reserved || self.stats.state == "reserved"
end

#statsBeaneater::StatStruct

Sends command to get stats about job.

Examples:

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

Returns:


107
108
109
110
# File 'lib/beaneater/job/record.rb', line 107

def stats
  res = transmit("stats-job #{id}")
  StatStruct.from_hash(res[:body])
end

#to_sString Also known as: inspect

Returns string representation of job

Examples:

@beaneater_connection.jobs.find(123).to_s
@beaneater_connection.jobs.find(123).inspect

Returns:

  • (String)

    string representation


189
190
191
# File 'lib/beaneater/job/record.rb', line 189

def to_s
  "#<Beaneater::Job id=#{id} body=#{body.inspect}>"
end

#touchHash{Symbol => String,Number}

Sends command to touch job which extends the ttr.

Examples:

@beaneater_connection.jobs.find(123).touch
  # => {:status=>"TOUCHED", :body=>nil, :connection=>#<Beaneater::Connection host="localhost" port=11300>}

Returns:

  • (Hash{Symbol => String,Number})

    Beanstalkd response for the command.


71
72
73
# File 'lib/beaneater/job/record.rb', line 71

def touch
  with_reserved("touch #{id}")
end

#transmit(cmd, &block) ⇒ Hash{Symbol => String,Number} (protected)

Transmit command to beanstalkd instances and fetch response.

Examples:

transmit('stats')
transmit('stats') { 'success' }

Parameters:

  • cmd (String)

    Beanstalkd command to send.

Returns:

  • (Hash{Symbol => String,Number})

    Beanstalkd response for the command.


204
205
206
207
208
# File 'lib/beaneater/job/record.rb', line 204

def transmit(cmd, &block)
  res = connection.transmit(cmd)
  yield if block_given?
  res
end

#ttrInteger

Returns the ttr of this job

Examples:

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

Returns:

  • (Integer)

    The ttr of this job


156
157
158
# File 'lib/beaneater/job/record.rb', line 156

def ttr
  @ttr ||= self.stats.ttr
end

#tubeString

Returns the name of the tube this job is in

Examples:

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

Returns:

  • (String)

    The name of the tube for this job


144
145
146
# File 'lib/beaneater/job/record.rb', line 144

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.

Examples:

with_reserved("bury 26") { @reserved = false }

Parameters:

  • cmd (String)

    Beanstalkd command to send.

Returns:

  • (Hash{Symbol => String,Number})

    Beanstalkd response for the command.

Raises:


218
219
220
221
# File 'lib/beaneater/job/record.rb', line 218

def with_reserved(cmd, &block)
  raise JobNotReserved unless reserved?
  transmit(cmd, &block)
end