Class: Beanstalk::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/beanstalk-client/job.rb

Constant Summary collapse

DELAY_MAX =

Don’t delay for more than 48 hours at a time.

60 * 60 * 48

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conn, id, body, reserved = true) ⇒ Job

Returns a new instance of Job.



39
40
41
42
43
44
# File 'lib/beanstalk-client/job.rb', line 39

def initialize(conn, id, body, reserved=true)
  @conn = conn
  @id = id
  @body = body
  @reserved = reserved
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



21
22
23
# File 'lib/beanstalk-client/job.rb', line 21

def body
  @body
end

#connObject (readonly)

Returns the value of attribute conn.



21
22
23
# File 'lib/beanstalk-client/job.rb', line 21

def conn
  @conn
end

#idObject (readonly)

Returns the value of attribute id.



21
22
23
# File 'lib/beanstalk-client/job.rb', line 21

def id
  @id
end

Instance Method Details

#[](name) ⇒ Object

Convenience method for getting ybody elements.



24
25
26
# File 'lib/beanstalk-client/job.rb', line 24

def [](name)
  ybody[name]
end

#[]=(name, val) ⇒ Object

Convenience method for setting ybody elements.



29
30
31
# File 'lib/beanstalk-client/job.rb', line 29

def []=(name, val)
  ybody[name] = val
end

#ageObject



85
# File 'lib/beanstalk-client/job.rb', line 85

def age() stats['age'] end

#bury(newpri = nil) ⇒ Object



65
66
67
68
69
# File 'lib/beanstalk-client/job.rb', line 65

def bury(newpri=nil)
  return if !@reserved
  @conn.bury(id, newpri || pri)
  @reserved = false
end

#decay(d = ([1, delay].max * 1.3).ceil) ⇒ Object



98
99
100
101
# File 'lib/beanstalk-client/job.rb', line 98

def decay(d=([1, delay].max * 1.3).ceil)
  return bury() if delay >= DELAY_MAX
  release(pri, d)
end

#delayObject



87
# File 'lib/beanstalk-client/job.rb', line 87

def delay() stats['delay'] end

#deleteObject

Deletes the job from the queue



47
48
49
50
51
# File 'lib/beanstalk-client/job.rb', line 47

def delete()
  return if !@reserved
  @conn.delete(id)
  @reserved = false
end

#inspectObject



107
108
109
# File 'lib/beanstalk-client/job.rb', line 107

def inspect
  "(job server=#{server} id=#{id} size=#{body.size})"
end

#priObject



88
# File 'lib/beanstalk-client/job.rb', line 88

def pri() stats['pri'] end

#put_back(pri = self.pri, delay = 0, ttr = self.ttr) ⇒ Object



54
55
56
# File 'lib/beanstalk-client/job.rb', line 54

def put_back(pri=self.pri, delay=0, ttr=self.ttr)
  @conn.put(body, pri, delay, ttr)
end

#release(newpri = nil, delay = 0) ⇒ Object

Releases the job back to the queue so another consumer can get it (call this if the job failed and want it to be tried again)



59
60
61
62
63
# File 'lib/beanstalk-client/job.rb', line 59

def release(newpri=nil, delay=0)
  return if !@reserved
  @conn.release(id, newpri || pri, delay)
  @reserved = false
end

#serverObject



91
92
93
# File 'lib/beanstalk-client/job.rb', line 91

def server()
  @conn.addr
end

#stateObject



86
# File 'lib/beanstalk-client/job.rb', line 86

def state() stats['state'] end

#statsObject



77
78
79
# File 'lib/beanstalk-client/job.rb', line 77

def stats()
  @conn.job_stats(id)
end

#time_leftObject

Time left (in seconds) that beanstalk has to process the job. When this time expires, beanstalkd automatically reinserts the job in the queue. See the ttr parameter for Beanstalk::Pool#put



84
# File 'lib/beanstalk-client/job.rb', line 84

def time_left() stats['time-left'] end

#timeoutsObject



81
# File 'lib/beanstalk-client/job.rb', line 81

def timeouts() stats['timeouts'] end

#to_sObject



103
104
105
# File 'lib/beanstalk-client/job.rb', line 103

def to_s
  "(job #{body.inspect})"
end

#touchObject

Ping beanstalkd to to tell it you’re alive and processing. If beanstalkd doesn’t hear from you for more than the ttr seconds (specified by the put command), then it assumes the consumer died and reinserts the job back into the queue for another to process.



72
73
74
75
# File 'lib/beanstalk-client/job.rb', line 72

def touch
  return if !@reserved
  @conn.touch(id)
end

#ttrObject



89
# File 'lib/beanstalk-client/job.rb', line 89

def ttr() stats['ttr'] end

#ybodyObject

Return the object that results from loading the body as a yaml stream. Return nil if the body is not a valid yaml stream.



35
36
37
# File 'lib/beanstalk-client/job.rb', line 35

def ybody()
  (@ybody ||= [begin YAML.load(body) rescue nil end])[0]
end