lib/beanstalk-client/job.rb in beanstalk-client-1.0.2 vs lib/beanstalk-client/job.rb in beanstalk-client-1.1.0

- old
+ new

@@ -34,38 +34,54 @@ # Return nil if the body is not a valid yaml stream. def ybody() (@ybody ||= [begin YAML.load(body) rescue nil end])[0] end - def initialize(conn, id, body) + def initialize(conn, id, body, reserved=true) @conn = conn @id = id @body = body - @deleted = false + @reserved = reserved end + # Deletes the job from the queue def delete() - @conn.delete(id) if !@deleted - @deleted = true + return if !@reserved + @conn.delete(id) + @reserved = false end + def put_back(pri=self.pri, delay=0, ttr=self.ttr) @conn.put(body, pri, delay, ttr) end - def release(newpri=pri, delay=0) - @conn.release(id, newpri, delay) + # 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) + def release(newpri=nil, delay=0) + return if !@reserved + @conn.release(id, newpri || pri, delay) + @reserved = false end - def bury(newpri=pri) - @conn.bury(id, newpri) + def bury(newpri=nil) + return if !@reserved + @conn.bury(id, newpri || pri) + @reserved = false end + # 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. + def touch + return if !@reserved + @conn.touch(id) + end + def stats() @conn.job_stats(id) end def timeouts() stats['timeouts'] end + + # 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 def time_left() stats['time-left'] end def age() stats['age'] end def state() stats['state'] end def delay() stats['delay'] end def pri() stats['pri'] end