GithubHelp home page GithubHelp logo

sidekiq-apriori's Introduction

sidekiq-apriori

Prioritization Middleware for Sidekiq

Build Status

Overview

sidekiq-apriori simplifies dynamic prioritization for sidekiq by supplying a simple sidekiq middleware, related active record hooks, & some additional argument handling (for ruby 2 users).

Installation

Manual installation is always an option. From the command line:

$ gem install sidekiq-priority

Or, if you're using bundler, simply include it in your Gemfile:

gem 'sidekiq-priority'

Priorities

By default, sidekiq-apriori supports four priorities: immediate, high, nil (default), and low. If you would like to use different priorities you can add something along these lines to (as an example for you railsy folk) your sidekiq initializer:

## config/initializers/sidekiq.rb

Sidekiq::Apriori::PRIORITIES = ['wut', 'huh', 'ok', nil, 'not_even_a_little']

The nil is meaningful insofar as it represents the default priority. Unless you want to disallow unset priorities, leave the nil in.

sidekiq-apriori is inspired by (a response to?) sidekiq-priority, in which the order of the priorities is important. Contrary to the approach taken by sidekiq-priority, sidekiq-apriori uses sidekiq's built in mechanism for configuring the order of processing. As such, the ordering of priorities is accomplished in the sidekiq.yml.

So, for example, if your sidekiq.yml currently looks like this:

## sidekiq.yml

verbose: false
:pidfile: /tmp/sidekiq.pid
:concurrency: 5
:queues:
  - postback
  - background

You might want to change the 'queues' entry to look more like this:

:queues:
  - postback_wut
  - postback_huh
  - postback_ok
  - postback
  - postback_not_even_a_little
  - background

To route an item to a prioritized queue, append an options hash of the form { :priority => 'wut' } to the end of the arguments. If you're using ruby 2 & have included Sidekiq::Apriori::Worker in your worker class then this should be enough. Otherwise, you'll need to update that method to optionally take an additional argument

Additional Utility

In addition to the use described in the PRIORITIES section, some tooling is provided for active record classes with priority as an attribute:

## app/models/prioritized.rb

class Prioritized < ActiveRecord::Base
  include Sidekiq::Apriori::Arb

  prioritize do
    self.priority = nil unless
      Sidekiq::Apriori::PRIORITIES.include?(self.priority)

      self.priority = (...)
  end
end

Alternatively, you can pass a method name to prioritize:

## app/models/prioritized.rb

class Prioritized < ActiveRecord::Base
  include Sidekiq::Apriori::Arb

  prioritize using: 'some_method'

  def some_method
    (...)
  end
end

If you're lucky enough to be using ruby 2, you can save yourself some work by including Sidekiq::Apriori::Worker instead of Sidekiq::Worker in your worker classes. This will save you the effort of changing the definition of the classes' perform method & all of its invocations. Sidekiq::Apriori::Worker uses prepend to define a perform which will take an optional hash containing a priority designation.

If you're not using ruby 2, you'll need to redefine your perform method to take an additional, optional argument

License

sidekiq-apriori is released under the MIT License. Please see the LICENSE file for details.

sidekiq-apriori's People

Contributors

bwthomas avatar kjg avatar

Watchers

Ng Tze Yang avatar James Cloos 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.