sidekiq-middleware 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ branches:
5
+ only:
6
+ - master
7
+ services:
8
+ - redis
data/Gemfile CHANGED
@@ -1,2 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
  gemspec
3
+
4
+ group :test do
5
+ gem 'simplecov', :require => false
6
+ end
data/README.md CHANGED
@@ -1,10 +1,8 @@
1
- # Additional sidekiq middleware
1
+ # Additional sidekiq middleware [![Build Status](https://secure.travis-ci.org/krasnoukhov/sidekiq-middleware.png)](http://travis-ci.org/krasnoukhov/sidekiq-middleware)
2
2
 
3
3
  This gem provides additional middleware for [Sidekiq](github.com/mperham/sidekiq/).
4
4
 
5
- Now it contains the following middlewares:
6
-
7
- * UniqueJobs (both client and server)
5
+ See [Sidekiq Wiki](https://github.com/mperham/sidekiq/wiki/Middleware) for more details.
8
6
 
9
7
  ## Installation
10
8
 
@@ -20,26 +18,38 @@ Or install it yourself as:
20
18
 
21
19
  $ gem install sidekiq-middleware
22
20
 
23
- ## Usage
24
-
25
- For example (put this code in initialize section):
26
-
27
- Sidekiq.configure_server do |config|
28
- config.server_middleware do |chain|
29
- chain.add Sidekiq::Middleware::Server::UniqueJobs
30
- end
31
- config.client_middleware do |chain|
32
- chain.add Sidekiq::Middleware::Client::UniqueJobs
33
- end
34
- end
35
-
36
- Sidekiq.configure_client do |config|
37
- config.client_middleware do |chain|
38
- chain.add Sidekiq::Middleware::Client::UniqueJobs
39
- end
40
- end
41
-
42
- See [Sidekiq Wiki](https://github.com/mperham/sidekiq/wiki/Middleware) for more details.
21
+ ## Contents
22
+
23
+ ### UniqueJobs
24
+
25
+ Provides uniqueness for jobs.
26
+
27
+ **Usage**
28
+
29
+ Example worker:
30
+
31
+ ```ruby
32
+ class UniqueWorker
33
+ include Sidekiq::Worker
34
+
35
+ sidekiq_options({
36
+ # Should be set to true (enables uniqueness for async jobs)
37
+ # or :all (enables uniqueness for both async and scheduled jobs)
38
+ unique: :all,
39
+
40
+ # Set this to true in case your job schedules itself
41
+ forever: true,
42
+
43
+ # Unique expiration (optional, default is 30 minutes)
44
+ # For scheduled jobs calculates automatically if not provided
45
+ expiration: 24 * 60 * 60
46
+ })
47
+
48
+ def perform
49
+ # Your code goes here
50
+ end
51
+ end
52
+ ```
43
53
 
44
54
  ## Contributing
45
55
 
data/Rakefile CHANGED
@@ -1,2 +1,9 @@
1
1
  #!/usr/bin/env rake
2
2
  require "bundler/gem_tasks"
3
+ require "rake/testtask"
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << 'test'
6
+ test.pattern = 'test/**/test_*.rb'
7
+ end
8
+
9
+ task :default => :test
@@ -1,3 +1,5 @@
1
+ require "digest"
1
2
  require "sidekiq-middleware/version"
2
3
  require "sidekiq-middleware/server/unique_jobs"
3
- require "sidekiq-middleware/client/unique_jobs"
4
+ require "sidekiq-middleware/client/unique_jobs"
5
+ require "sidekiq-middleware/middleware"
@@ -1,5 +1,3 @@
1
- require 'digest'
2
-
3
1
  module Sidekiq
4
2
  module Middleware
5
3
  module Client
@@ -18,13 +16,12 @@ module Sidekiq
18
16
  payload = item.clone
19
17
  payload.delete('at')
20
18
  payload.delete('jid')
21
- payload_hash = Digest::MD5.hexdigest(Sidekiq.dump_json(Hash[payload.sort]))
22
19
  else
23
20
  expiration = worker_class.get_sidekiq_options['expiration'] || HASH_KEY_EXPIRATION
24
21
  payload = item.clone
25
22
  payload.delete('jid')
26
- payload_hash = Digest::MD5.hexdigest(Sidekiq.dump_json(Hash[payload.sort]))
27
23
  end
24
+ payload_hash = Digest::MD5.hexdigest(Sidekiq.dump_json(Hash[payload.sort]))
28
25
 
29
26
  Sidekiq.redis do |conn|
30
27
  conn.watch(payload_hash)
@@ -0,0 +1,14 @@
1
+ Sidekiq.configure_server do |config|
2
+ config.server_middleware do |chain|
3
+ chain.add Sidekiq::Middleware::Server::UniqueJobs
4
+ end
5
+ config.client_middleware do |chain|
6
+ chain.add Sidekiq::Middleware::Client::UniqueJobs
7
+ end
8
+ end
9
+
10
+ Sidekiq.configure_client do |config|
11
+ config.client_middleware do |chain|
12
+ chain.add Sidekiq::Middleware::Client::UniqueJobs
13
+ end
14
+ end
@@ -25,12 +25,11 @@ module Sidekiq
25
25
  payload = item.clone
26
26
  payload.delete('at')
27
27
  payload.delete('jid')
28
- payload_hash = Digest::MD5.hexdigest(Sidekiq.dump_json(Hash[payload.sort]))
29
28
  else
30
29
  payload = item.clone
31
30
  payload.delete('jid')
32
- payload_hash = Digest::MD5.hexdigest(Sidekiq.dump_json(Hash[payload.sort]))
33
31
  end
32
+ payload_hash = Digest::MD5.hexdigest(Sidekiq.dump_json(Hash[payload.sort]))
34
33
 
35
34
  Sidekiq.redis { |conn| conn.del(payload_hash) }
36
35
  end
@@ -1,5 +1,5 @@
1
1
  module Sidekiq
2
2
  module Middleware
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -14,5 +14,7 @@ Gem::Specification.new do |gem|
14
14
  gem.require_paths = ["lib"]
15
15
  gem.version = Sidekiq::Middleware::VERSION
16
16
 
17
- gem.add_dependency 'sidekiq'
17
+ gem.add_dependency 'sidekiq'
18
+ gem.add_development_dependency 'rake'
19
+ gem.add_development_dependency 'minitest', '~> 3'
18
20
  end
data/test/helper.rb ADDED
@@ -0,0 +1,17 @@
1
+ ENV['RACK_ENV'] = ENV['RAILS_ENV'] = 'test'
2
+ if ENV.has_key?('SIMPLECOV')
3
+ require 'simplecov'
4
+ SimpleCov.start
5
+ end
6
+
7
+ require 'minitest/unit'
8
+ require 'minitest/pride'
9
+ require 'minitest/autorun'
10
+
11
+ require 'sidekiq'
12
+ require 'sidekiq/util'
13
+ require 'sidekiq-middleware'
14
+ Sidekiq.logger.level = Logger::ERROR
15
+
16
+ require 'sidekiq/redis_connection'
17
+ REDIS = Sidekiq::RedisConnection.create(:url => "redis://localhost/15", :namespace => 'testy')
@@ -0,0 +1,69 @@
1
+ require 'securerandom'
2
+ require 'helper'
3
+ require 'sidekiq/client'
4
+ require 'sidekiq/worker'
5
+ require 'sidekiq/processor'
6
+ require 'sidekiq-middleware'
7
+
8
+ class TestUniqueJobs < MiniTest::Unit::TestCase
9
+ describe 'with real redis' do
10
+ before do
11
+ @boss = MiniTest::Mock.new
12
+ @processor = ::Sidekiq::Processor.new(@boss)
13
+ Celluloid.logger = nil
14
+
15
+ Sidekiq.redis = REDIS
16
+ Sidekiq.redis {|c| c.flushdb }
17
+ end
18
+
19
+ class UniqueWorker
20
+ include Sidekiq::Worker
21
+ sidekiq_options queue: :unique_queue, unique: true
22
+
23
+ def perform(x)
24
+ end
25
+ end
26
+
27
+ it 'does not duplicate messages with enabled unique option' do
28
+ 5.times { UniqueWorker.perform_async('args') }
29
+ assert_equal 1, Sidekiq.redis { |c| c.llen('queue:unique_queue') }
30
+ end
31
+
32
+ class NotUniqueWorker
33
+ include Sidekiq::Worker
34
+ sidekiq_options queue: :not_unique_queue, unique: false
35
+
36
+ def perform(x)
37
+ end
38
+ end
39
+
40
+ it 'duplicates messages with disabled unique option' do
41
+ 5.times { NotUniqueWorker.perform_async('args') }
42
+ assert_equal 5, Sidekiq.redis { |c| c.llen('queue:not_unique_queue') }
43
+ end
44
+
45
+ class UniqueScheduledWorker
46
+ include Sidekiq::Worker
47
+ sidekiq_options queue: :unique_scheduled_queue, unique: :all, forever: true
48
+
49
+ def perform(x)
50
+ UniqueScheduledWorker.perform_in(60, x)
51
+ end
52
+ end
53
+
54
+ it 'does not duplicate scheduled messages with enabled unique option' do
55
+ 5.times { |t| UniqueScheduledWorker.perform_in((t+1)*60, 'args') }
56
+ assert_equal 1, Sidekiq.redis { |c| c.zrangebyscore('schedule', '-inf', '+inf').length }
57
+ end
58
+
59
+ it 'once schedules job in future with enabled forever option' do
60
+ 5.times {
61
+ msg = Sidekiq.dump_json({ 'class' => UniqueScheduledWorker.to_s, 'args' => ['forever'] })
62
+ @boss.expect(:processor_done!, nil, [@processor])
63
+ @processor.process(msg, 'default')
64
+ @boss.verify
65
+ }
66
+ assert_equal 1, Sidekiq.redis { |c| c.zrangebyscore('schedule', '-inf', '+inf').length }
67
+ end
68
+ end
69
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq-middleware
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-21 00:00:00.000000000 Z
12
+ date: 2012-09-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sidekiq
@@ -27,6 +27,38 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: minitest
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '3'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '3'
30
62
  description: Additional sidekiq middleware
31
63
  email:
32
64
  - dmitry@krasnoukhov.com
@@ -35,15 +67,19 @@ extensions: []
35
67
  extra_rdoc_files: []
36
68
  files:
37
69
  - .gitignore
70
+ - .travis.yml
38
71
  - Gemfile
39
72
  - LICENSE
40
73
  - README.md
41
74
  - Rakefile
42
75
  - lib/sidekiq-middleware.rb
43
76
  - lib/sidekiq-middleware/client/unique_jobs.rb
77
+ - lib/sidekiq-middleware/middleware.rb
44
78
  - lib/sidekiq-middleware/server/unique_jobs.rb
45
79
  - lib/sidekiq-middleware/version.rb
46
80
  - sidekiq-middleware.gemspec
81
+ - test/helper.rb
82
+ - test/test_unique_jobs.rb
47
83
  homepage: http://github.com/krasnoukhov/sidekiq-middleware
48
84
  licenses: []
49
85
  post_install_message:
@@ -56,16 +92,24 @@ required_ruby_version: !ruby/object:Gem::Requirement
56
92
  - - ! '>='
57
93
  - !ruby/object:Gem::Version
58
94
  version: '0'
95
+ segments:
96
+ - 0
97
+ hash: -2849267342138572133
59
98
  required_rubygems_version: !ruby/object:Gem::Requirement
60
99
  none: false
61
100
  requirements:
62
101
  - - ! '>='
63
102
  - !ruby/object:Gem::Version
64
103
  version: '0'
104
+ segments:
105
+ - 0
106
+ hash: -2849267342138572133
65
107
  requirements: []
66
108
  rubyforge_project:
67
109
  rubygems_version: 1.8.24
68
110
  signing_key:
69
111
  specification_version: 3
70
112
  summary: Additional sidekiq middleware
71
- test_files: []
113
+ test_files:
114
+ - test/helper.rb
115
+ - test/test_unique_jobs.rb