Low commit activity in last 3 years
There's a lot of open issues
No release in over a year
Sidekiq extension that adds the ability to debounce job execution. Worker will postpone its execution after `wait time` have elapsed since the last time it was invoked. Useful for implementing behavior that should only happen after the input has stopped arriving.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.22.1
~> 3.12.0
~> 0.22.0
~> 1.24.3
~> 0.9.6

Runtime

>= 6.5, < 8.0
 Project Readme

Sidekiq::Debouncer

Sidekiq extension that adds the ability to debounce job execution.

Worker will postpone its execution after wait time have elapsed since the last time it was invoked. Useful for implementing behavior that should only happen after the input has stopped arriving. For example: sending one email with multiple notifications.

Installation

Add this line to your application's Gemfile:

gem 'sidekiq-debouncer'

And then execute:

$ bundle

Basic usage

Add middlewares to sidekiq:

Sidekiq.configure_client do |config|
  config.client_middleware do |chain|
    chain.add Sidekiq::Debouncer::Middleware::Client
  end
end

Sidekiq.configure_server do |config|
  config.client_middleware do |chain|
    chain.add Sidekiq::Debouncer::Middleware::Client
  end
  
  config.server_middleware do |chain|
    chain.add Sidekiq::Debouncer::Middleware::Server
  end
end

Add debounce option to worker with by and time keys:

class MyWorker
  include Sidekiq::Worker

  sidekiq_options(
    debounce: {
      by: -> (args) { args[0] }, # debounce by first argument only
      time: 5 * 60
    }
  )

  def perform(group)
    group.each do
      # do some work with group
    end
  end
end

You can also pass symbol as debounce.by matching class method.

class MyWorker
  include Sidekiq::Worker

  sidekiq_options(
    debounce: {
      time: 5 * 60,
      by: :debounce_method
    }
  )
  
  def self.debounce_method(job_args)
    job_args[0]
  end

  def perform(group)
    group.each do
      # do some work with group
    end
  end
end

Keep in mind that the result of the debounce method will be converted to string, so make sure it doesn't return any objects that don't implement to_s method.

In the application, call MyWorker.perform_async(...) as usual. Everytime you call this function, MyWorker's execution will be postponed by 5 minutes. After that time MyWorker will receive a method call perform with an array of arguments that were provided to the MyWorker.perform_async(...) calls.

To avoid keeping leftover keys in redis, all additional keys are created with TTL. It's 24 hours by default and should be ok in most of the cases. If you are debouncing your jobs in higher interval than that, you can overwrite this setting:

Sidekiq.configure_client do |config|
  config.client_middleware do |chain|
    chain.add Sidekiq::Debouncer::Middleware::Client, ttl: 60 * 60 * 24 * 30 # 30 days
  end
end

Testing

In order to test the behavior of sidekiq-debouncer it is necessary to disable testing mode. It is the limitation of internal implementation.

Sidekiq::Debouncer will not debounce the jobs in testing mode, instead they'll be executed like normal jobs (pushed to fake queue or executed inline depending on settings). Server middleware needs to be added to Sidekiq::Testing chain:

Sidekiq::Testing.server_middleware do |chain|
  chain.add Sidekiq::Debouncer::Middleware::Server
end

License

MIT Licensed. See LICENSE.txt for details.

Notes

This gem was renamed from sidekiq-debouce due to name conflict on rubygems.