sidekiq-middleware 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +2 -0
- data/LICENSE +22 -0
- data/README.md +50 -0
- data/Rakefile +2 -0
- data/lib/sidekiq-middleware.rb +3 -0
- data/lib/sidekiq-middleware/client/unique_jobs.rb +50 -0
- data/lib/sidekiq-middleware/server/unique_jobs.rb +41 -0
- data/lib/sidekiq-middleware/version.rb +5 -0
- data/sidekiq-middleware.gemspec +18 -0
- metadata +71 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Dmitry Krasnoukhov
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# Additional sidekiq middleware
|
2
|
+
|
3
|
+
This gem provides additional middleware for [Sidekiq](github.com/mperham/sidekiq/).
|
4
|
+
|
5
|
+
Now it contains the following middlewares:
|
6
|
+
|
7
|
+
* UniqueJobs (both client and server)
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'sidekiq-middleware'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install sidekiq-middleware
|
22
|
+
|
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.
|
43
|
+
|
44
|
+
## Contributing
|
45
|
+
|
46
|
+
1. Fork it
|
47
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
48
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
49
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
50
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'digest'
|
2
|
+
|
3
|
+
module Sidekiq
|
4
|
+
module Middleware
|
5
|
+
module Client
|
6
|
+
class UniqueJobs
|
7
|
+
HASH_KEY_EXPIRATION = 30 * 60
|
8
|
+
|
9
|
+
def call(worker_class, item, queue)
|
10
|
+
enabled = worker_class.get_sidekiq_options['unique']
|
11
|
+
|
12
|
+
if enabled
|
13
|
+
unique = false
|
14
|
+
|
15
|
+
# Enabled unique scheduled
|
16
|
+
if enabled == :all && item.has_key?('at')
|
17
|
+
expiration = worker_class.get_sidekiq_options['expiration'] || (item['at'].to_i - Time.new.to_i)
|
18
|
+
payload = item.clone
|
19
|
+
payload.delete('at')
|
20
|
+
payload.delete('jid')
|
21
|
+
payload_hash = Digest::MD5.hexdigest(Sidekiq.dump_json(Hash[payload.sort]))
|
22
|
+
else
|
23
|
+
expiration = worker_class.get_sidekiq_options['expiration'] || HASH_KEY_EXPIRATION
|
24
|
+
payload = item.clone
|
25
|
+
payload.delete('jid')
|
26
|
+
payload_hash = Digest::MD5.hexdigest(Sidekiq.dump_json(Hash[payload.sort]))
|
27
|
+
end
|
28
|
+
|
29
|
+
Sidekiq.redis do |conn|
|
30
|
+
conn.watch(payload_hash)
|
31
|
+
|
32
|
+
if conn.get(payload_hash)
|
33
|
+
conn.unwatch
|
34
|
+
else
|
35
|
+
unique = conn.multi do
|
36
|
+
conn.setex(payload_hash, expiration, 1)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
yield if unique
|
42
|
+
else
|
43
|
+
yield
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Sidekiq
|
2
|
+
module Middleware
|
3
|
+
module Server
|
4
|
+
class UniqueJobs
|
5
|
+
|
6
|
+
def call(worker_instance, item, queue)
|
7
|
+
forever = worker_instance.class.get_sidekiq_options['forever']
|
8
|
+
|
9
|
+
# Delete lock first if forever is set
|
10
|
+
# Used for jobs which may scheduling self in future
|
11
|
+
clear(worker_instance, item, queue) if forever
|
12
|
+
|
13
|
+
begin
|
14
|
+
yield
|
15
|
+
ensure
|
16
|
+
clear(worker_instance, item, queue) unless forever
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def clear(worker_instance, item, queue)
|
21
|
+
enabled = worker_instance.class.get_sidekiq_options['unique']
|
22
|
+
|
23
|
+
# Enabled unique scheduled
|
24
|
+
if enabled == :all && item.has_key?('at')
|
25
|
+
payload = item.clone
|
26
|
+
payload.delete('at')
|
27
|
+
payload.delete('jid')
|
28
|
+
payload_hash = Digest::MD5.hexdigest(Sidekiq.dump_json(Hash[payload.sort]))
|
29
|
+
else
|
30
|
+
payload = item.clone
|
31
|
+
payload.delete('jid')
|
32
|
+
payload_hash = Digest::MD5.hexdigest(Sidekiq.dump_json(Hash[payload.sort]))
|
33
|
+
end
|
34
|
+
|
35
|
+
Sidekiq.redis { |conn| conn.del(payload_hash) }
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/sidekiq-middleware/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Dmitry Krasnoukhov"]
|
6
|
+
gem.email = ["dmitry@krasnoukhov.com"]
|
7
|
+
gem.description = gem.summary = "Additional sidekiq middleware"
|
8
|
+
gem.homepage = "http://github.com/krasnoukhov/sidekiq-middleware"
|
9
|
+
|
10
|
+
gem.files = `git ls-files`.split($\)
|
11
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
12
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
13
|
+
gem.name = "sidekiq-middleware"
|
14
|
+
gem.require_paths = ["lib"]
|
15
|
+
gem.version = Sidekiq::Middleware::VERSION
|
16
|
+
|
17
|
+
gem.add_dependency 'sidekiq'
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sidekiq-middleware
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Dmitry Krasnoukhov
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-21 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: sidekiq
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: Additional sidekiq middleware
|
31
|
+
email:
|
32
|
+
- dmitry@krasnoukhov.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- lib/sidekiq-middleware.rb
|
43
|
+
- lib/sidekiq-middleware/client/unique_jobs.rb
|
44
|
+
- lib/sidekiq-middleware/server/unique_jobs.rb
|
45
|
+
- lib/sidekiq-middleware/version.rb
|
46
|
+
- sidekiq-middleware.gemspec
|
47
|
+
homepage: http://github.com/krasnoukhov/sidekiq-middleware
|
48
|
+
licenses: []
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
requirements: []
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.8.24
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: Additional sidekiq middleware
|
71
|
+
test_files: []
|