Coder Social home page Coder Social logo

will3216 / resque-better_unique Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 3.0 16 KB

A resque plugin which adds more finite control over how uniqueness is determine for a job

Home Page: https://rubygems.org/gems/resque-better_unique

Ruby 98.78% Shell 1.22%
resque ruby

resque-better_unique's Introduction

Resque::BetterUnique

There are currently a number of resque plugins that provide this functionality in some form or another, but one thing they all lack is the ability to control how the unique constraint is defined. Sometimes, a job should only be unique until a worker begins processing it, in other cases you will want the job to remain unique until the job completes, or maybe even long after the job has completed. This allows you to do all of the above and more with a single gem.

The functionality of this gem is based on the sidekiq equivalent sidekiq-unique-jobs.

Installation

Add this line to your application's Gemfile:

gem 'resque-better_unique'

And then execute:

$ bundle

Or install it yourself as:

$ gem install resque-better_unique

Usage

Include this plugin into your job class and call the unique method

require 'resque/plugins/better_unique'
class MyWorker
  include Resque::Plugins::BetterUnique
  unique_job :while_executing, timeout: 5.minutes
end

The unique_job method takes up to two arguments:

  • mode: (default=:until_executed)
    • while_executing: only one distinct job can be processed at a time
    • until_executing: only one job can be queued at a time
    • until_executed: only one job can be queued or processed at a time
    • until_timeout: only one job can be queued or processed in a given time period
  • options: Hash of options
    • timeout - integer or object that responds to to_i - How long should a lock live
    • unique_args - a proc or a symbol which takes the arguments of perform and returns the arguments that should be used to determine uniqueness

Examples:

Specify method to define unique args:

class MyWorker
  include Resque::Plugins::BetterUnique
  unique_job :while_executing, timeout: 5.minutes, unique_args: unique_job_arguments

  private

  def unique_job_arguments(*args)
    [args[0], args[3]]
  end
end

Specify Proc to define unique args:

class MyWorker
  include Resque::Plugins::BetterUnique
  unique_job :while_executing, timeout: 5.minutes, unique_args: ->(*args) { [args[0], args[3]] }
end

Override lock_key method:

class MyWorker
  include Resque::Plugins::BetterUnique
  unique_job :while_executing, timeout: 5.minutes

  def self.lock_key(*args)
    "lock:my_lock:#{args.to_s}"
  end
end

Clear lock for a single job:

class MyWorker
  include Resque::Plugins::BetterUnique
  unique_job :until_executed, timeout: 5.minutes
end

Resque.enqueue(MyWorker, {some: :args})
MyWorker.release_lock({some: :args})

Clear all locks for a worker class:

class MyWorker
  include Resque::Plugins::BetterUnique
  unique_job :while_executing, timeout: 5.minutes
end
100.times { Resque.enqueue(MyWorker, rand(1000))}
MyWorker.release_all_locks

NOTE: release_all_locks requires redis-server >= 2.8. Only removes locks for the class on which it was called.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/resque-better_unique.

resque-better_unique's People

Contributors

konstantinreido avatar will3216 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

resque-better_unique's Issues

Using with ActiveJob

We are using Resque with ActiveJob and this plugin doesn't seem to work with activejob ... is there some way to make it work?

Update method signature with ruby 2.0 double-splat

def unique_job(mode=:until_executed, options={})

If we change the method signature to

def unique_job(mode=:until_executed, **options)

Then we'll be able to use the default mode with other non-default options, e.g.

unique_job timeout: 2.hours

The current code will interpret that as the mode being the options hash (which won't work out as the developer might have expected.

What do you think?

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.