GithubHelp home page GithubHelp logo

todddickerson / sidekiq-middleware Goto Github PK

View Code? Open in Web Editor NEW

This project forked from krasnoukhov/sidekiq-middleware

0.0 1.0 0.0 121 KB

Additional sidekiq middleware

License: MIT License

Ruby 100.00%

sidekiq-middleware's Introduction

Additional sidekiq middleware Build Status

This gem provides additional middleware for Sidekiq.

See Sidekiq Wiki for more details.

Installation

Add this line to your application's Gemfile:

gem 'sidekiq-middleware'

And then execute:

$ bundle

Or install it yourself as:

$ gem install sidekiq-middleware

Contents

UniqueJobs

Provides uniqueness for jobs.

Usage

Example worker:

class UniqueWorker
  include Sidekiq::Worker

  sidekiq_options({
    # Should be set to true (enables uniqueness for async jobs)
    # or :all (enables uniqueness for both async and scheduled jobs)
    unique: :all,

    # Unique expiration (optional, default is 30 minutes)
    # For scheduled jobs calculates automatically based on schedule time and expiration period
    expiration: 24 * 60 * 60
  })

  def perform
    # Your code goes here
  end
end

Custom lock key and manual expiration:

class UniqueWorker
  include Sidekiq::Worker

  sidekiq_options({
    unique: :all,
    expiration: 24 * 60 * 60,
    
    # Set this to true when you need to handle locks manually.
    # You'll be able to handle unique expiration inside your worker.
    # Please see example below.
    manual: true
  })
  
  # Implement your own lock string
  def self.lock(id)
    "locks:unique:#{id}"
  end
  
  # Implement method to handle lock removing manually
  def self.unlock!(id)
  	lock = self.lock(id)
    Sidekiq.redis { |conn| conn.del(lock) }
  end

  def perform(id)
    # Your code goes here
    # You are able to re-schedule job from perform method,
    # Just remove lock manually before performing job again.
    sleep 5
    
    # Re-schedule!
    self.class.unlock!(id)
    self.class.perform_async(id)
  end
end

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

sidekiq-middleware's People

Contributors

bnorton avatar dmeremyanin avatar krasnoukhov avatar tjsingleton avatar todddickerson avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.