sidekiq-apriori 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +7 -0
- data/.rspec +1 -0
- data/.simplecov +12 -0
- data/.travis.yml +5 -0
- data/Gemfile +2 -0
- data/LICENSE +20 -0
- data/README.md +4 -0
- data/Rakefile +11 -0
- data/lib/sidekiq-apriori/arb.rb +23 -0
- data/lib/sidekiq-apriori/middleware.rb +14 -0
- data/lib/sidekiq-apriori/prioritizer.rb +20 -0
- data/lib/sidekiq-apriori/version.rb +5 -0
- data/lib/sidekiq-apriori.rb +12 -0
- data/sidekiq-apriori.gemspec +29 -0
- data/spec/sidekiq-apriori_spec.rb +60 -0
- data/spec/spec_helper.rb +10 -0
- data/spec/support/arb.rb +19 -0
- metadata +148 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.simplecov
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
unless ENV['COVERAGE'] == 'false'
|
2
|
+
require 'simplecov-rcov'
|
3
|
+
|
4
|
+
class SimpleCov::Formatter::MergedFormatter
|
5
|
+
def format(result)
|
6
|
+
SimpleCov::Formatter::HTMLFormatter.new.format(result)
|
7
|
+
SimpleCov::Formatter::RcovFormatter.new.format(result)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
SimpleCov.formatter = SimpleCov::Formatter::MergedFormatter
|
12
|
+
end
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013 Blake Thomas
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
RSpec::Core::RakeTask.new
|
6
|
+
task :default => :spec
|
7
|
+
|
8
|
+
desc 'Start IRB with preloaded environment'
|
9
|
+
task :console do
|
10
|
+
exec 'irb', "-I#{File.join(File.dirname(__FILE__), 'lib')}", '-rsidekiq-apriori'
|
11
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Sidekiq::Apriori
|
2
|
+
def self.included(base)
|
3
|
+
return unless defined?(ActiveRecord::Base) && base < ActiveRecord::Base
|
4
|
+
|
5
|
+
base.extend ClassMethods
|
6
|
+
|
7
|
+
## Add validation for priority attribute
|
8
|
+
if base.attribute_names.include?('priority')
|
9
|
+
base.validates_inclusion_of :priority, :in => PRIORITIES
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
## Declarative hook to prioritize instances
|
15
|
+
def prioritize(options = {}, &block)
|
16
|
+
method = ( block_given? && [-1, 0].include?(block.arity) ) ?
|
17
|
+
block : ( options[:using] || options[:with] )
|
18
|
+
|
19
|
+
before_validation method, :on => :create
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'sidekiq'
|
2
|
+
require 'sidekiq-apriori/prioritizer'
|
3
|
+
|
4
|
+
Sidekiq.configure_client do |config|
|
5
|
+
config.client_middleware do |chain|
|
6
|
+
chain.add Sidekiq::Apriori::Prioritizer
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
Sidekiq.configure_server do |config|
|
11
|
+
config.client_middleware do |chain|
|
12
|
+
chain.add Sidekiq::Apriori::Prioritizer
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Sidekiq::Apriori
|
2
|
+
class Prioritizer
|
3
|
+
def call(worker, msg, queue)
|
4
|
+
priority = msg["args"].try(:last)
|
5
|
+
if priorities.include?(priority)
|
6
|
+
msg["queue"] = queue.to_s.sub(priority_regexp,"_#{priority}")
|
7
|
+
end
|
8
|
+
|
9
|
+
yield
|
10
|
+
end
|
11
|
+
|
12
|
+
def priorities
|
13
|
+
Sidekiq::Apriori::PRIORITIES.compact
|
14
|
+
end
|
15
|
+
|
16
|
+
def priority_regexp
|
17
|
+
/(_(#{priorities.join('|')}))*$/
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
$:.push File.expand_path("../lib", __FILE__)
|
4
|
+
require "sidekiq-apriori/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "sidekiq-apriori"
|
8
|
+
s.license = 'MIT'
|
9
|
+
s.version = Sidekiq::Apriori::VERSION
|
10
|
+
s.platform = Gem::Platform::RUBY
|
11
|
+
s.authors = ["Blake Thomas", "Enova"]
|
12
|
+
s.email = ["bwthomas@gmail.com"]
|
13
|
+
s.homepage = "https://github.com/bwthomas/sidekiq-apriori"
|
14
|
+
s.summary = %q{Prioritization Middleware for Sidekiq}
|
15
|
+
s.description = %q{Prioritization middleware for Sidekiq}
|
16
|
+
s.rubyforge_project = "nowarning"
|
17
|
+
|
18
|
+
s.files = `git ls-files`.split("\n")
|
19
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
20
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
21
|
+
s.require_paths = ["lib"]
|
22
|
+
|
23
|
+
s.add_development_dependency "pry"
|
24
|
+
s.add_development_dependency "rake"
|
25
|
+
s.add_development_dependency "rspec"
|
26
|
+
s.add_development_dependency "simplecov"
|
27
|
+
|
28
|
+
s.add_dependency "sidekiq"
|
29
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Sidekiq::Apriori do
|
4
|
+
before(:all) do
|
5
|
+
## See spec/support/arb.rb for Arb class definition
|
6
|
+
class Prioritized < Arb
|
7
|
+
prioritize using: :some_method
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:arb) { Arb.new }
|
12
|
+
let(:prioritized) { Prioritized.new }
|
13
|
+
|
14
|
+
it "should validate priorities" do
|
15
|
+
arb.priority = "none"
|
16
|
+
arb.should be_invalid
|
17
|
+
|
18
|
+
Sidekiq::Apriori::PRIORITIES.each do |priority|
|
19
|
+
arb.priority = priority
|
20
|
+
arb.should be_valid
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should attempt prioritization on creation with named prioritizer" do
|
25
|
+
prioritized.should_receive(:some_method)
|
26
|
+
prioritized.save
|
27
|
+
end
|
28
|
+
|
29
|
+
describe Sidekiq::Apriori::Prioritizer do
|
30
|
+
subject(:middleware) { Sidekiq::Apriori::Prioritizer.new }
|
31
|
+
|
32
|
+
let(:message) { {'args' => [1, nil, 'high'], 'queue' => 'foo'} }
|
33
|
+
let(:queue) { 'foo' }
|
34
|
+
|
35
|
+
describe '#call' do
|
36
|
+
it 'should respond' do
|
37
|
+
middleware.respond_to?(:call).should be_true
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should require three arguments' do
|
41
|
+
expect { middleware.call }.
|
42
|
+
to raise_error( ArgumentError,'wrong number of arguments (0 for 3)')
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should set priority queue' do
|
46
|
+
middleware.call(nil, message, queue) {}
|
47
|
+
message['queue'].should eql('foo_high')
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should allow only one priority suffix' do
|
51
|
+
message['queue'] = 'foo_low_high_immediate'
|
52
|
+
middleware.call(nil, message, queue) {}
|
53
|
+
message['queue'].should eql('foo_high')
|
54
|
+
|
55
|
+
middleware.call(nil, message, queue) {}
|
56
|
+
message['queue'].should eql('foo_high')
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/spec/spec_helper.rb
ADDED
data/spec/support/arb.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'sqlite3'
|
3
|
+
|
4
|
+
ActiveRecord::Base.establish_connection(
|
5
|
+
:adapter => :sqlite3,
|
6
|
+
:database => 'spec/support/test.db'
|
7
|
+
)
|
8
|
+
|
9
|
+
ActiveRecord::Schema.define do
|
10
|
+
drop_table :arbs
|
11
|
+
|
12
|
+
create_table :arbs do |t|
|
13
|
+
t.column :priority, :string
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Arb < ActiveRecord::Base
|
18
|
+
include Sidekiq::Apriori
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sidekiq-apriori
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Blake Thomas
|
9
|
+
- Enova
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2013-11-28 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: pry
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ! '>='
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '0'
|
31
|
+
- !ruby/object:Gem::Dependency
|
32
|
+
name: rake
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
type: :development
|
40
|
+
prerelease: false
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rspec
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
name: simplecov
|
65
|
+
requirement: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
type: :development
|
72
|
+
prerelease: false
|
73
|
+
version_requirements: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
- !ruby/object:Gem::Dependency
|
80
|
+
name: sidekiq
|
81
|
+
requirement: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
type: :runtime
|
88
|
+
prerelease: false
|
89
|
+
version_requirements: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
description: Prioritization middleware for Sidekiq
|
96
|
+
email:
|
97
|
+
- bwthomas@gmail.com
|
98
|
+
executables: []
|
99
|
+
extensions: []
|
100
|
+
extra_rdoc_files: []
|
101
|
+
files:
|
102
|
+
- .gitignore
|
103
|
+
- .rspec
|
104
|
+
- .simplecov
|
105
|
+
- .travis.yml
|
106
|
+
- Gemfile
|
107
|
+
- LICENSE
|
108
|
+
- README.md
|
109
|
+
- Rakefile
|
110
|
+
- lib/sidekiq-apriori.rb
|
111
|
+
- lib/sidekiq-apriori/arb.rb
|
112
|
+
- lib/sidekiq-apriori/middleware.rb
|
113
|
+
- lib/sidekiq-apriori/prioritizer.rb
|
114
|
+
- lib/sidekiq-apriori/version.rb
|
115
|
+
- sidekiq-apriori.gemspec
|
116
|
+
- spec/sidekiq-apriori_spec.rb
|
117
|
+
- spec/spec_helper.rb
|
118
|
+
- spec/support/arb.rb
|
119
|
+
homepage: https://github.com/bwthomas/sidekiq-apriori
|
120
|
+
licenses:
|
121
|
+
- MIT
|
122
|
+
post_install_message:
|
123
|
+
rdoc_options: []
|
124
|
+
require_paths:
|
125
|
+
- lib
|
126
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
127
|
+
none: false
|
128
|
+
requirements:
|
129
|
+
- - ! '>='
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
133
|
+
none: false
|
134
|
+
requirements:
|
135
|
+
- - ! '>='
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '0'
|
138
|
+
requirements: []
|
139
|
+
rubyforge_project: nowarning
|
140
|
+
rubygems_version: 1.8.23
|
141
|
+
signing_key:
|
142
|
+
specification_version: 3
|
143
|
+
summary: Prioritization Middleware for Sidekiq
|
144
|
+
test_files:
|
145
|
+
- spec/sidekiq-apriori_spec.rb
|
146
|
+
- spec/spec_helper.rb
|
147
|
+
- spec/support/arb.rb
|
148
|
+
has_rdoc:
|