Skip to content

Instantly share code, notes, and snippets.

@samsm
Created August 10, 2014 20:21

Revisions

  1. samsm created this gist Aug 10, 2014.
    30 changes: 30 additions & 0 deletions sidekiq_middleware.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    class QuitWhenEmpty
    def initialize(options=nil)
    end

    def call(worker, msg, queue)
    yield
    stop_disposible_worker_if_queue_empty!
    end

    private

    def stop_disposible_worker_if_queue_empty!
    if kill_when_queue_empty?
    my_process.stop! if queue_empty?
    end
    end

    def kill_when_queue_empty?
    ENV["SIDEKIQ_KILL_WHEN_QUEUE_EMPTY"]
    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
    end