Skip to content

Instantly share code, notes, and snippets.

@cice
Created October 25, 2018 13:00
Show Gist options
  • Save cice/1cf4c7036a8e0e9d76d7743193c97648 to your computer and use it in GitHub Desktop.
Save cice/1cf4c7036a8e0e9d76d7743193c97648 to your computer and use it in GitHub Desktop.
class QuitWhenEmpty
def initialize(options = nil)
end
def call(worker, msg, queue)
yield
stop_disposible_worker_if_queue_empty!
end
def stop_disposible_worker_if_queue_empty!
if kill_when_queue_empty?
Rails.logger.info "Shutting down because queue is empty"
my_process.stop! if queue_empty?
end
end
private
def kill_when_queue_empty?
true
end
def my_process
ps = Sidekiq::ProcessSet.new
ps.detect {|p| Process.pid == p['pid']}
end
def queue_empty?
Sidekiq::Queue.new.size == 0
end
def self.stop_if_queue_empty
Thread.new do
sleep 120
QuitWhenEmpty.new.stop_disposible_worker_if_queue_empty!
end
end
end
Sidekiq.configure_server do |config|
config.server_middleware do |chain|
QuitWhenEmpty.stop_if_queue_empty
chain.add QuitWhenEmpty
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment