sidekiq-prioritized_queues 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 472c6683a50393966af89094f5c98d32d285f7c8
4
+ data.tar.gz: 86a3a16e91c457682e6e7d95980ed29fac348dbf
5
+ SHA512:
6
+ metadata.gz: 0b2dee492c651c8c3db1ad0e73f0904cb4ee1b4f337c50241ab3f593fd73f560fb48b20e77eb6253314bc3d1ddaf28b7cc84728eae48227b7aac419f1b6d7ea1
7
+ data.tar.gz: 2bd344b5c99597085746c3e66563c4c4bc7f458d48ec15c9958d519bf6dbf80952896b9656f77d7120c90a4bbc2b7984e931fd938e3c014430da7b1ae02ae610
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sidekiq-prioritized_queues.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Publitas
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,75 @@
1
+ # Sidekiq::PrioritizedQueues
2
+
3
+ Adds numeric based priorities to your jobs. This is done by monkey patching the following classes:
4
+
5
+ - `Sidekiq::Client`
6
+ - `Sidekiq::Stats`
7
+ - `Sidekiq::Queue`
8
+ - `Sidekiq::Job`
9
+
10
+ This gem also adds a new priority based Fetcher, and a middleware that sets priority on the jobs.
11
+
12
+ **WARNING: This changes the type of the `queue:<name>` keys. There's no migration helper in place, so the easiest way is to start off with a clean setup.**
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem 'sidekiq-prioritized_queues'
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ Simply having the gem in your Gemfile is enough to get started with prioritized jobs. As a default, priority will be the timestamp at which the job is enqueued, as to simulate the default FIFO order.
25
+
26
+ There are two ways of specifying priority:
27
+
28
+ ### Static
29
+
30
+ This is useful whenever different workers share the same queue. An example would be:
31
+
32
+ ```ruby
33
+ class SatisfySameDayOrderWorker
34
+ include Sidekiq::Worker
35
+ sidekiq_options queue: 'orders', priority: 0
36
+
37
+ def perform(order)
38
+ # Do stuff here
39
+ end
40
+ end
41
+
42
+ class SatisfyOrderWorker
43
+ include Sidekiq::Worker
44
+ sidekiq_options queue: 'orders', priority: 1
45
+
46
+ def perform(order)
47
+ # Do stuff here
48
+ end
49
+ end
50
+ ```
51
+
52
+ For workers on the same queue, the `priority` option can take a `Proc` which will be given the arguments the job was queued with. As an example:
53
+
54
+ ```ruby
55
+ class HeavyWorker
56
+ include Sidekiq::Worker
57
+ sidekiq_options priority: -> (account_id) {
58
+ Account.find(account_id).vip? ? 0 : 10
59
+ }
60
+
61
+ def perform(account_id)
62
+ # Do some work.
63
+ end
64
+ end
65
+ ```
66
+
67
+ The example above would make sure that VIP accounts get processed first.
68
+
69
+ ## Contributing
70
+
71
+ 1. Fork it ( https://github.com/publitas/sidekiq-prioritized_queues/fork )
72
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
73
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
74
+ 4. Push to the branch (`git push origin my-new-feature`)
75
+ 5. Create a new Pull Request
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.test_files = FileList["test/**/*_test.rb"]
7
+ end
8
+
9
+ task :default => :test
10
+
@@ -0,0 +1,21 @@
1
+ require 'sidekiq/prioritized_queues/version'
2
+ require 'sidekiq/prioritized_queues/middleware'
3
+ require 'sidekiq/prioritized_queues/fetch'
4
+ require 'sidekiq/prioritized_queues/monkeypatches'
5
+
6
+ # Add the Client middleware that takes care of setting up the priority property
7
+ # on the messages being queued.
8
+ Sidekiq.configure_server do |config|
9
+ config.client_middleware do |chain|
10
+ chain.add Sidekiq::PrioritizedQueues::Middleware
11
+ end
12
+ end
13
+
14
+ Sidekiq.configure_client do |config|
15
+ config.client_middleware do |chain|
16
+ chain.add Sidekiq::PrioritizedQueues::Middleware
17
+ end
18
+ end
19
+
20
+ # Set up the fetcher as the priority based one too.
21
+ Sidekiq.options[:fetch] = Sidekiq::PrioritizedQueues::Fetch
@@ -0,0 +1,73 @@
1
+ module Sidekiq
2
+ module PrioritizedQueues
3
+ class Fetch
4
+ def initialize(options)
5
+ @strictly_ordered_queues = !!options[:strict]
6
+ @queues = options[:queues].map { |q| "queue:#{q}" }
7
+ @unique_queues = @queues.uniq
8
+ end
9
+
10
+ def retrieve_work
11
+ work = nil
12
+
13
+ Sidekiq.redis do |conn|
14
+ queues.find do |queue|
15
+ response = conn.multi do
16
+ conn.zrange(queue, 0, 0)
17
+ conn.zremrangebyrank(queue, 0, 0)
18
+ end.flatten(1)
19
+
20
+ next if response.length == 1
21
+ work = [queue, response.first]
22
+ break
23
+ end
24
+ end
25
+
26
+ return UnitOfWork.new(*work) if work
27
+ sleep 1; nil
28
+ end
29
+
30
+ # By leaving this as a class method, it can be pluggable and used by the Manager actor. Making it
31
+ # an instance method will make it async to the Fetcher actor
32
+ def self.bulk_requeue(inprogress, options)
33
+ return if inprogress.empty?
34
+
35
+ Sidekiq.logger.debug { "Re-queueing terminated jobs" }
36
+ jobs_to_requeue = {}
37
+ inprogress.each do |unit_of_work|
38
+ jobs_to_requeue[unit_of_work.queue_name] ||= []
39
+ jobs_to_requeue[unit_of_work.queue_name] << unit_of_work.message
40
+ end
41
+
42
+ Sidekiq.redis do |conn|
43
+ conn.pipelined do
44
+ jobs_to_requeue.each do |queue, jobs|
45
+ jobs.each { |job| conn.zadd("queue:#{queue}", 0, job) }
46
+ end
47
+ end
48
+ end
49
+ Sidekiq.logger.info("Pushed #{inprogress.size} messages back to Redis")
50
+ rescue => ex
51
+ Sidekiq.logger.warn("Failed to requeue #{inprogress.size} jobs: #{ex.message}")
52
+ end
53
+
54
+ UnitOfWork = Struct.new(:queue, :message) do
55
+ def acknowledge
56
+ # nothing to do
57
+ end
58
+
59
+ def queue_name
60
+ queue.gsub(/.*queue:/, '')
61
+ end
62
+
63
+ def requeue
64
+ Sidekiq.redis { |conn| conn.zadd("queue:#{queue_name}", 0, message) }
65
+ end
66
+ end
67
+
68
+ def queues
69
+ @strictly_ordered_queues ? @unique_queues.dup : @queues.shuffle.uniq
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,22 @@
1
+ module Sidekiq
2
+ module PrioritizedQueues
3
+ class Middleware
4
+ def call(worker_class, msg, queue, redis_pool)
5
+ klass = case worker_class
6
+ when String then worker_class.constantize
7
+ else worker_class
8
+ end
9
+
10
+ priority = klass.get_sidekiq_options['priority']
11
+
12
+ msg['priority'] = case priority
13
+ when Proc then priority.call(*msg['args'])
14
+ when String then priority.to_i
15
+ else Time.now.to_f
16
+ end
17
+
18
+ yield
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ require 'sidekiq/prioritized_queues/monkeypatches/api'
2
+ require 'sidekiq/prioritized_queues/monkeypatches/client'
3
+ require 'sidekiq/prioritized_queues/monkeypatches/web'
@@ -0,0 +1,70 @@
1
+ require 'sidekiq/api'
2
+
3
+ module Sidekiq
4
+ class Stats
5
+ def queues
6
+ Sidekiq.redis do |conn|
7
+ queues = conn.smembers('queues')
8
+
9
+ lengths = conn.pipelined do
10
+ queues.each do |queue|
11
+ conn.zcard("queue:#{queue}")
12
+ end
13
+ end
14
+
15
+ i = 0
16
+ array_of_arrays = queues.inject({}) do |memo, queue|
17
+ memo[queue] = lengths[i]
18
+ i += 1
19
+ memo
20
+ end.sort_by { |_, size| size }
21
+
22
+ Hash[array_of_arrays.reverse]
23
+ end
24
+ end
25
+ end
26
+
27
+ class Queue
28
+ def size
29
+ Sidekiq.redis { |conn| conn.zcard(@rname) }
30
+ end
31
+
32
+ def latency
33
+ entry = Sidekiq.redis do |conn|
34
+ conn.zrange(@rname, -1, -1)
35
+ end.first
36
+ return 0 unless entry
37
+ Time.now.to_f - Sidekiq.load_json(entry)['enqueued_at']
38
+ end
39
+
40
+ def each(&block)
41
+ initial_size = size
42
+ deleted_size = 0
43
+ page = 0
44
+ page_size = 50
45
+
46
+ loop do
47
+ range_start = page * page_size - deleted_size
48
+ range_end = page * page_size - deleted_size + (page_size - 1)
49
+ entries = Sidekiq.redis do |conn|
50
+ conn.zrevrange @rname, range_start, range_end
51
+ end
52
+ break if entries.empty?
53
+ page += 1
54
+ entries.each do |entry|
55
+ block.call Job.new(entry, @name)
56
+ end
57
+ deleted_size = initial_size - size
58
+ end
59
+ end
60
+ end
61
+
62
+ class Job
63
+ def delete
64
+ count = Sidekiq.redis do |conn|
65
+ conn.zrem("queue:#{@queue}", @value)
66
+ end
67
+ count != 0
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,25 @@
1
+ module Sidekiq
2
+ class Client
3
+
4
+ private
5
+
6
+ def atomic_push(conn, payloads)
7
+ if payloads.first['at']
8
+ conn.zadd('schedule', payloads.map do |hash|
9
+ at = hash.delete('at').to_s
10
+ [at, Sidekiq.dump_json(hash)]
11
+ end)
12
+ else
13
+ q = payloads.first['queue']
14
+
15
+ conn.sadd('queues', q)
16
+
17
+ payloads.each do |entry|
18
+ to_push = Sidekiq.dump_json(entry)
19
+ priority = entry['priority'] || 0
20
+ conn.zadd("queue:#{q}", priority, to_push)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,15 @@
1
+ require 'sinatra'
2
+
3
+ module Sidekiq
4
+ class Web < Sinatra::Base
5
+ get "/queues/:name" do
6
+ halt 404 unless params[:name]
7
+ @count = (params[:count] || 25).to_i
8
+ @name = params[:name]
9
+ @queue = Sidekiq::Queue.new(@name)
10
+ (@current_page, @total_size, @messages) = page("queue:#{@name}", params[:page], @count)
11
+ @messages = @messages.map {|msg, priority| Sidekiq::Job.new(msg, @name) }
12
+ erb :queue
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ module Sidekiq
2
+ module PrioritizedQueues
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sidekiq/prioritized_queues/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sidekiq-prioritized_queues"
8
+ spec.version = Sidekiq::PrioritizedQueues::VERSION
9
+ spec.authors = ["Andre Medeiros"]
10
+ spec.email = ["me@andremedeiros.info"]
11
+ spec.summary = %q{Numeric priorities for queues on Sidekiq}
12
+ spec.description = %q{Changes your queues from FIFO to numeric priority based ones.}
13
+ spec.homepage = "https://github.com/publitas/sidekiq-prioritized_queues"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "sidekiq", "~> 3.0"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "minitest"
26
+ end
@@ -0,0 +1,24 @@
1
+ require 'minitest_helper'
2
+
3
+ module Sidekiq
4
+ module PrioritizedQueues
5
+ describe Fetch do
6
+ before do
7
+ Sidekiq.redis = REDIS
8
+ Sidekiq.redis { |c| c.flushdb }
9
+ end
10
+
11
+ it 'should fetch jobs in the right priority' do
12
+ client = Sidekiq::Client.new
13
+ client.push_bulk('class' => 'MockWorker', 'args' => [[20], [30], [10]])
14
+
15
+ fetcher = Sidekiq::PrioritizedQueues::Fetch.new(queues: ['default'])
16
+
17
+ [100, 200, 300].each do |priority|
18
+ msg = Sidekiq.load_json(fetcher.retrieve_work.message)
19
+ assert_equal priority, msg['priority']
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,22 @@
1
+ require 'minitest_helper'
2
+
3
+ module Sidekiq
4
+ module PrioritizedQueues
5
+ describe Middleware do
6
+ before do
7
+ Sidekiq.redis = REDIS
8
+ Sidekiq.redis { |c| c.flushdb }
9
+ end
10
+
11
+ it 'should add the priority field to jobs' do
12
+ client = Sidekiq::Client.new
13
+ client.push('class' => 'MockWorker', 'args' => [10])
14
+
15
+ json = Sidekiq.redis { |c| c.zrange('queue:default', 0, 0) }.first
16
+ job = Sidekiq.load_json(json)
17
+
18
+ assert_equal 100, job['priority']
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,18 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'sidekiq'
3
+ require 'sidekiq/prioritized_queues'
4
+
5
+ require 'minitest/autorun'
6
+
7
+ REDIS = Sidekiq::RedisConnection.create(
8
+ url: 'redis://localhost/15',
9
+ namespace: 'sidekiq_prioritized_queues_test'
10
+ )
11
+
12
+ class MockWorker
13
+ include Sidekiq::Worker
14
+ sidekiq_options priority: -> (arg) { arg * 10 }
15
+
16
+ def perform(arg)
17
+ end
18
+ end
@@ -0,0 +1,36 @@
1
+ require 'minitest_helper'
2
+
3
+ module Sidekiq
4
+ module PrioritizedQueues
5
+ describe 'Client Monkeypatch' do
6
+ before do
7
+ @redis = Minitest::Mock.new
8
+ def @redis.sadd(*); true; end
9
+ def @redis.with; yield self; end
10
+ def @redis.multi; [yield] * 2 if block_given?; end
11
+ Sidekiq.instance_variable_set(:@redis, @redis)
12
+ Sidekiq::Client.instance_variable_set(:@default, nil)
13
+ end
14
+
15
+ after do
16
+ Sidekiq.redis = REDIS
17
+ Sidekiq::Client.instance_variable_set(:@default, nil)
18
+ end
19
+
20
+ describe 'as an instance' do
21
+ it 'pushes jobs with the right score' do
22
+ @redis.expect :zadd, 1, ['queue:default', 50, String]
23
+ client = Sidekiq::Client.new
24
+ client.push('class' => 'MockWorker', 'args' => [5])
25
+ @redis.verify
26
+ end
27
+ end
28
+
29
+ it 'pushes jobs with the right score' do
30
+ @redis.expect :zadd, 1, ['queue:default', 20, String]
31
+ Sidekiq::Client.push('class' => 'MockWorker', 'args' => [2])
32
+ @redis.verify
33
+ end
34
+ end
35
+ end
36
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq-prioritized_queues
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andre Medeiros
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sidekiq
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Changes your queues from FIFO to numeric priority based ones.
70
+ email:
71
+ - me@andremedeiros.info
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - lib/sidekiq/prioritized_queues.rb
83
+ - lib/sidekiq/prioritized_queues/fetch.rb
84
+ - lib/sidekiq/prioritized_queues/middleware.rb
85
+ - lib/sidekiq/prioritized_queues/monkeypatches.rb
86
+ - lib/sidekiq/prioritized_queues/monkeypatches/api.rb
87
+ - lib/sidekiq/prioritized_queues/monkeypatches/client.rb
88
+ - lib/sidekiq/prioritized_queues/monkeypatches/web.rb
89
+ - lib/sidekiq/prioritized_queues/version.rb
90
+ - sidekiq-prioritized_queues.gemspec
91
+ - test/fetch_test.rb
92
+ - test/middleware_test.rb
93
+ - test/minitest_helper.rb
94
+ - test/monkeypatch_test.rb
95
+ homepage: https://github.com/publitas/sidekiq-prioritized_queues
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.2.2
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Numeric priorities for queues on Sidekiq
119
+ test_files:
120
+ - test/fetch_test.rb
121
+ - test/middleware_test.rb
122
+ - test/minitest_helper.rb
123
+ - test/monkeypatch_test.rb