Class: Tuber::Jobs
- Inherits:
-
Object
- Object
- Tuber::Jobs
- Defined in:
- lib/tuber/job/collection.rb
Overview
Represents collection of job-related commands.
Constant Summary collapse
- MAX_RETRIES =
Number of retries to process a job.
3- RELEASE_DELAY =
Delay in seconds before to make job ready again.
1- RESERVE_TIMEOUT =
Number of seconds to wait for a job before checking a different server.
nil
Instance Attribute Summary collapse
-
#client ⇒ Tuber
Returns the client instance.
-
#current_job ⇒ Object
Returns the value of attribute current_job.
-
#processors ⇒ Array<Proc>
Returns Collection of proc to handle beanstalkd jobs.
Instance Method Summary collapse
-
#find(id) ⇒ Tuber::Job
(also: #peek, #[])
Peek (or find) job by id from beanstalkd.
-
#initialize(client) ⇒ Jobs
constructor
Creates new jobs instance.
-
#process!(options = {}) ⇒ Object
Watch, reserve, process and delete or bury or release jobs.
-
#register(tube_name, options = {}, &block) ⇒ Object
Register a processor to handle beanstalkd job on particular tube.
-
#stop! ⇒ Object
Sets flag to indicate that process loop should stop after current job.
-
#stop? ⇒ Boolean
Returns whether the process loop should stop.
-
#touch_all ⇒ Integer
Extends the ttr of every job currently reserved by this connection.
-
#transmit(command, options = {}) ⇒ Object
Delegates transmit to the connection object.
Constructor Details
#initialize(client) ⇒ Jobs
Creates new jobs instance.
34 35 36 |
# File 'lib/tuber/job/collection.rb', line 34 def initialize(client) @client = client end |
Instance Attribute Details
#client ⇒ Tuber
Returns the client instance
17 |
# File 'lib/tuber/job/collection.rb', line 17 attr_reader :processors, :client, :current_job |
#current_job ⇒ Object
Returns the value of attribute current_job.
17 |
# File 'lib/tuber/job/collection.rb', line 17 attr_reader :processors, :client, :current_job |
#processors ⇒ Array<Proc>
Returns Collection of proc to handle beanstalkd jobs
17 18 19 |
# File 'lib/tuber/job/collection.rb', line 17 def processors @processors end |
Instance Method Details
#find(id) ⇒ Tuber::Job Also known as: peek, []
Peek (or find) job by id from beanstalkd.
58 59 60 61 62 63 |
# File 'lib/tuber/job/collection.rb', line 58 def find(id) res = transmit("peek #{id}") Job.new(client, res) rescue Tuber::NotFoundError nil end |
#process!(options = {}) ⇒ Object
Watch, reserve, process and delete or bury or release jobs.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/tuber/job/collection.rb', line 131 def process!(={}) release_delay = .delete(:release_delay) || RELEASE_DELAY reserve_timeout = .delete(:reserve_timeout) || RESERVE_TIMEOUT client.tubes.watch!(*processors.keys) while !stop? do begin @current_job = client.tubes.reserve(reserve_timeout) processor = processors[@current_job.tube] begin processor[:block].call(@current_job) @current_job.delete rescue *processor[:retry_on] if @current_job.stats.releases < processor[:max_retries] @current_job.release(:delay => release_delay) end end rescue AbortProcessingError break rescue Tuber::JobNotReserved, Tuber::NotFoundError, Tuber::TimedOutError retry rescue StandardError # handles unspecified errors @current_job.bury if @current_job ensure # bury if still reserved @current_job.bury if @current_job && @current_job.exists? && @current_job.reserved? @current_job = nil end end end |
#register(tube_name, options = {}, &block) ⇒ Object
Register a processor to handle beanstalkd job on particular tube.
105 106 107 108 109 110 |
# File 'lib/tuber/job/collection.rb', line 105 def register(tube_name, ={}, &block) @processors ||= {} max_retries = [:max_retries] || MAX_RETRIES retry_on = Array([:retry_on]) @processors[tube_name.to_s] = { :block => block, :retry_on => retry_on, :max_retries => max_retries } end |
#stop! ⇒ Object
Sets flag to indicate that process loop should stop after current job
113 114 115 |
# File 'lib/tuber/job/collection.rb', line 113 def stop! @stop = true end |
#stop? ⇒ Boolean
Returns whether the process loop should stop
120 121 122 |
# File 'lib/tuber/job/collection.rb', line 120 def stop? !!@stop end |
#touch_all ⇒ Integer
Extends the ttr of every job currently reserved by this connection.
A single heartbeat for a whole +reserve_batch+ window: the server already tracks the reserved set per connection, so no ids are sent and jobs that were already deleted, released, buried or lost to a ttr timeout are simply absent. Each job keeps its own ttr; deadlines are extended individually.
The returned count is how many jobs the connection actually still holds. If it is lower than expected, jobs hit their ttr and went back to the queue while the worker was busy.
83 84 85 |
# File 'lib/tuber/job/collection.rb', line 83 def touch_all transmit("touch-all")[:id].to_i end |
#transmit(command, options = {}) ⇒ Object
Delegates transmit to the connection object.
41 42 43 44 45 46 |
# File 'lib/tuber/job/collection.rb', line 41 def transmit(command, ={}) # Empty **options must not be forwarded: on Ruby 2.7 it arrives as an # extra positional {} at the receiver. return client.connection.transmit(command) if .empty? client.connection.transmit(command, **) end |