Class: Sidekiq::Middleware::Server::MaxJobs
- Inherits:
-
Object
- Object
- Sidekiq::Middleware::Server::MaxJobs
- Defined in:
- lib/sidekiq/middleware/server/max_jobs.rb
Constant Summary collapse
- VERSION =
File.read( File.join( File.dirname(__FILE__), '..', '..', '..', '..', 'VERSION' ) ).strip
Class Method Summary collapse
- .counter ⇒ Object
- .increment_counter! ⇒ Object
- .log_info(message) ⇒ Object
- .log_initialization! ⇒ Object
- .max_jobs ⇒ Object
- .max_jobs_jitter ⇒ Object
- .max_jobs_with_jitter ⇒ Object
- .mutex ⇒ Object
- .pid ⇒ Object
Instance Method Summary collapse
Class Method Details
.counter ⇒ Object
19 20 21 |
# File 'lib/sidekiq/middleware/server/max_jobs.rb', line 19 def counter @counter ||= 0 end |
.increment_counter! ⇒ Object
23 24 25 |
# File 'lib/sidekiq/middleware/server/max_jobs.rb', line 23 def increment_counter! @counter = counter.next end |
.log_info(message) ⇒ Object
27 28 29 |
# File 'lib/sidekiq/middleware/server/max_jobs.rb', line 27 def log_info() ::Sidekiq.logger.info() if defined?(::Sidekiq.logger) end |
.log_initialization! ⇒ Object
31 32 33 |
# File 'lib/sidekiq/middleware/server/max_jobs.rb', line 31 def log_initialization! log_info("Max-Jobs middleware enabled, shutting down pid: #{pid} after: #{max_jobs_with_jitter} job(s)") end |
.max_jobs ⇒ Object
35 36 37 |
# File 'lib/sidekiq/middleware/server/max_jobs.rb', line 35 def max_jobs @max_jobs ||= (ENV['MAX_JOBS'] || 100).to_i end |
.max_jobs_jitter ⇒ Object
39 40 41 |
# File 'lib/sidekiq/middleware/server/max_jobs.rb', line 39 def max_jobs_jitter @max_jobs_jitter ||= rand((ENV['MAX_JOBS_JITTER'] || 1).to_i) end |
.max_jobs_with_jitter ⇒ Object
43 44 45 |
# File 'lib/sidekiq/middleware/server/max_jobs.rb', line 43 def max_jobs_with_jitter @max_jobs_with_jitter ||= max_jobs + max_jobs_jitter end |
.mutex ⇒ Object
47 48 49 |
# File 'lib/sidekiq/middleware/server/max_jobs.rb', line 47 def mutex @mutex ||= ::Mutex.new end |
.pid ⇒ Object
51 52 53 |
# File 'lib/sidekiq/middleware/server/max_jobs.rb', line 51 def pid @pid ||= ::Process.pid end |
Instance Method Details
#call(_, _, _) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/sidekiq/middleware/server/max_jobs.rb', line 56 def call( _, # worker-instance _, # item _ # queue ) exception_raised = false begin yield rescue Exception # Set the `exception_raised` boolean to `true` so that the # job-counter *is not* incremented in the `ensure` block exception_raised = true # Re-raise the `Exception` so that _Sidekiq_ can deal w/ it raise ensure if !exception_raised self.class.mutex.synchronize do self.class.increment_counter! if self.class.counter == self.class.max_jobs_with_jitter self.class.log_info("Max-Jobs quota met, shutting down pid: #{self.class.pid}") ::Process.kill('TERM', self.class.pid) end end end end end |