Class: Beanstalk::Job
- Inherits:
-
Object
- Object
- Beanstalk::Job
- 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
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Convenience method for getting ybody elements.
-
#[]=(name, val) ⇒ Object
Convenience method for setting ybody elements.
- #age ⇒ Object
- #bury(newpri = nil) ⇒ Object
- #decay(d = ([1, delay].max * 1.3).ceil) ⇒ Object
- #delay ⇒ Object
-
#delete ⇒ Object
Deletes the job from the queue.
-
#initialize(conn, id, body, reserved = true) ⇒ Job
constructor
A new instance of Job.
- #inspect ⇒ Object
- #pri ⇒ Object
- #put_back(pri = self.pri, delay = 0, ttr = self.ttr) ⇒ Object
-
#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).
- #server ⇒ Object
- #state ⇒ Object
- #stats ⇒ Object
-
#time_left ⇒ Object
Time left (in seconds) that beanstalk has to process the job.
- #timeouts ⇒ Object
- #to_s ⇒ Object
-
#touch ⇒ Object
Ping beanstalkd to to tell it you’re alive and processing.
- #ttr ⇒ Object
-
#ybody ⇒ Object
Return the object that results from loading the body as a yaml stream.
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
#body ⇒ Object (readonly)
Returns the value of attribute body.
21 22 23 |
# File 'lib/beanstalk-client/job.rb', line 21 def body @body end |
#conn ⇒ Object (readonly)
Returns the value of attribute conn.
21 22 23 |
# File 'lib/beanstalk-client/job.rb', line 21 def conn @conn end |
#id ⇒ Object (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 |
#age ⇒ Object
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 |
#delay ⇒ Object
87 |
# File 'lib/beanstalk-client/job.rb', line 87 def delay() stats['delay'] end |
#delete ⇒ Object
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 |
#inspect ⇒ Object
107 108 109 |
# File 'lib/beanstalk-client/job.rb', line 107 def inspect "(job server=#{server} id=#{id} size=#{body.size})" end |
#pri ⇒ Object
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 |
#server ⇒ Object
91 92 93 |
# File 'lib/beanstalk-client/job.rb', line 91 def server() @conn.addr end |
#state ⇒ Object
86 |
# File 'lib/beanstalk-client/job.rb', line 86 def state() stats['state'] end |
#stats ⇒ Object
77 78 79 |
# File 'lib/beanstalk-client/job.rb', line 77 def stats() @conn.job_stats(id) end |
#time_left ⇒ Object
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 |
#timeouts ⇒ Object
81 |
# File 'lib/beanstalk-client/job.rb', line 81 def timeouts() stats['timeouts'] end |
#to_s ⇒ Object
103 104 105 |
# File 'lib/beanstalk-client/job.rb', line 103 def to_s "(job #{body.inspect})" end |
#touch ⇒ Object
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 |
#ttr ⇒ Object
89 |
# File 'lib/beanstalk-client/job.rb', line 89 def ttr() stats['ttr'] end |
#ybody ⇒ Object
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 |