sidekiq-dup-guard 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +3 -0
- data/LICENSE +21 -0
- data/README.md +71 -0
- data/lib/sidekiq-dup-guard/middleware.rb +17 -0
- data/lib/sidekiq-dup-guard/sidekiq_unique_job_filter.rb +59 -0
- data/lib/sidekiq-dup-guard/version.rb +5 -0
- data/lib/sidekiq-dup-guard.rb +4 -0
- data/sidekiq-dup-guard.gemspec +17 -0
- metadata +66 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: cc503b1844bd20c32c69b767aaa974b6afd66d730e118736c1b0f0d95148f730
|
4
|
+
data.tar.gz: 1dd020d8cd934a58d39284af4e410e6fed5a2f37cad1344c5b8fc84394d4474f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: eee95aac75f2f4b96de311f13d2ea20ff84aecca3a39fa31d9e7d579c907debe58f732df0ff0b488036599a0f6a479beb3a76a5235481fa63032882502196869
|
7
|
+
data.tar.gz: bafc054d73461aa5fa63f8b3298297584dea5383f88c1662a7bc6eddcef5de6464a8a4e0697b4c49fb2f4308c74e6a569d7cafa7ef7aa2c3e4c5b09e72ee3848
|
data/CHANGELOG.md
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2021 Amagi Media Private Ltd.
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, 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,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# sidekiq-dup-guard
|
2
|
+
|
3
|
+
This gem provides a Sidekiq middleware to prevent duplicate jobs from getting enqueued.
|
4
|
+
Uniqueness can be configured at worker level or at function level.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Include the gem in your Application Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'sidekiq-dup-guard'
|
12
|
+
```
|
13
|
+
and then execute
|
14
|
+
|
15
|
+
```bash
|
16
|
+
$ bundle install
|
17
|
+
```
|
18
|
+
|
19
|
+
## Configuring at Function level
|
20
|
+
|
21
|
+
At a time there can be only one job in queue with same arguments for the method's configured in `dup_guard_methods`.
|
22
|
+
|
23
|
+
### Worker Example
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class FooWorker
|
27
|
+
include Sidekiq::Workers
|
28
|
+
|
29
|
+
# Only method1 and method3 should have unique jobs
|
30
|
+
sidekiq_options :queue => :foo, :dup_guard_methods => ['method1', 'method3']
|
31
|
+
|
32
|
+
def perform(args)
|
33
|
+
# Do work
|
34
|
+
end
|
35
|
+
|
36
|
+
def method1(args)
|
37
|
+
# Do work
|
38
|
+
end
|
39
|
+
|
40
|
+
def method2(args)
|
41
|
+
# Do work
|
42
|
+
end
|
43
|
+
|
44
|
+
def method3(args)
|
45
|
+
# Do work
|
46
|
+
end
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
This will ensure duplicate job's are not enqueued to queue from `FooWorker.perform_async` until `FooWorker.new.perform` is called for `method1` and `method3`. If a job is already present for `method1` and `method3` with same arguments then Sidekiq job will not be enqueued.
|
51
|
+
|
52
|
+
## Configuring at Worker Level
|
53
|
+
|
54
|
+
At a time there can be only one job in queue with same arguments for all the method's of a Worker.
|
55
|
+
|
56
|
+
### Worker Example
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
class FooWorker
|
60
|
+
include Sidekiq::Workers
|
61
|
+
|
62
|
+
# All the methods of FooWorker should have unique jobs
|
63
|
+
sidekiq_options :queue => :foo, :dup_guard_methods => 'all'
|
64
|
+
|
65
|
+
def perform(args)
|
66
|
+
# Do work
|
67
|
+
end
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
71
|
+
This will ensure duplicate job are not enqueued to queue from `FooWorker.perform_async` until `FooWorker.new.perform` is called for all functions of `FooWorker`. If a job is already present for a method with same arguments then Sidekiq job will not be enqueued.
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# Configure Sidekiq Middleware
|
2
|
+
|
3
|
+
# Sidekiq Wiki for more info: https://github.com/mperham/sidekiq/wiki/Middleware
|
4
|
+
|
5
|
+
# :nocov:
|
6
|
+
Sidekiq.configure_server do |config|
|
7
|
+
config.client_middleware do |chain|
|
8
|
+
chain.add SidekiqDupGuard::SidekiqUniqueJobFilter
|
9
|
+
end
|
10
|
+
end
|
11
|
+
# :nocov:
|
12
|
+
|
13
|
+
Sidekiq.configure_client do |config|
|
14
|
+
config.client_middleware do |chain|
|
15
|
+
chain.add SidekiqDupGuard::SidekiqUniqueJobFilter
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
|
2
|
+
# Before job is enqueued this middleware will be called.
|
3
|
+
module SidekiqDupGuard
|
4
|
+
class SidekiqUniqueJobFilter
|
5
|
+
|
6
|
+
# get Sidekiq logger
|
7
|
+
def logger
|
8
|
+
Sidekiq.logger
|
9
|
+
end
|
10
|
+
|
11
|
+
##
|
12
|
+
# checks if Sidekiq Job is already present in queue
|
13
|
+
#
|
14
|
+
# - If job is already present in queue with same arguments then Sidekiq job will be ignored
|
15
|
+
# - If job is not present with similar arguments then Sidekiq job will be queued
|
16
|
+
#
|
17
|
+
# @param worker [String]: Worker class name
|
18
|
+
# @param item [Hash]: Args passed to create a SidekiqJob
|
19
|
+
# - item["dup_guard_methods"]: *all* --: All jobs enqueued to all the function of a worker should be unique
|
20
|
+
# - item["dup_guard_methods"]: [array of method names] --: All jobs enqueued to a method should be unique
|
21
|
+
# @param queue [String]: queue name
|
22
|
+
# @param redis_pool [ConnectionPool]: Redis connection pool
|
23
|
+
#
|
24
|
+
# @return nil
|
25
|
+
#
|
26
|
+
|
27
|
+
def call(worker, item, queue, redis_pool)
|
28
|
+
if (item["dup_guard_methods"] != nil) and !item["dup_guard_methods"].empty? and (item["dup_guard_methods"] == "all" or item["dup_guard_methods"].include?(item["args"][0]))
|
29
|
+
jid = get_sidekiq_job(queue, item["args"][0], item["args"][1])
|
30
|
+
unless (jid == nil)
|
31
|
+
logger.info("SidekiqDupGuard: #{item["class"]}##{item["args"][0]} '#{item["args"][1]}' job already exists in queue with JID #{jid}, skipping enqueue.")
|
32
|
+
return
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
yield
|
37
|
+
end
|
38
|
+
|
39
|
+
##
|
40
|
+
# checks if Sidekiq job is present
|
41
|
+
#
|
42
|
+
# @param [String] queue
|
43
|
+
# @param [String] method
|
44
|
+
# @param [Hash] args
|
45
|
+
#
|
46
|
+
# @return [String] sidekiq job ID
|
47
|
+
#
|
48
|
+
def get_sidekiq_job(queue, method, args)
|
49
|
+
q = Sidekiq::Queue.new(queue)
|
50
|
+
args.transform_keys!(&:to_s)
|
51
|
+
q.each do |j|
|
52
|
+
if (j.args[0] == method.to_s) and (j.args[1] == args)
|
53
|
+
return j.jid
|
54
|
+
end
|
55
|
+
end
|
56
|
+
return nil
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require_relative "lib/sidekiq-dup-guard/version"
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = 'sidekiq-dup-guard'
|
5
|
+
spec.version = SidekiqDupGuard::VERSION
|
6
|
+
spec.summary = "Sidekiq middleware to prevent enqueue of duplicate jobs"
|
7
|
+
spec.description = <<-EOS
|
8
|
+
This gem provides a Sidekiq middleware to prevent duplicate jobs from getting enqueued to the queue.
|
9
|
+
EOS
|
10
|
+
spec.authors = ["Sowmya S K"]
|
11
|
+
spec.email = ['cloudport.team@amagi.com']
|
12
|
+
spec.homepage = "https://github.com/amagimedia/sidekiq-dup-guard"
|
13
|
+
spec.license = "MIT"
|
14
|
+
spec.files = ["sidekiq-dup-guard.gemspec", "README.md", "CHANGELOG.md", "LICENSE"] + `git ls-files | grep -E '^(lib)'`.split("\n")
|
15
|
+
|
16
|
+
spec.add_dependency 'sidekiq'
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sidekiq-dup-guard
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sowmya S K
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-09-07 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: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: " This gem provides a Sidekiq middleware to prevent duplicate jobs
|
28
|
+
from getting enqueued to the queue.\n"
|
29
|
+
email:
|
30
|
+
- cloudport.team@amagi.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- CHANGELOG.md
|
36
|
+
- LICENSE
|
37
|
+
- README.md
|
38
|
+
- lib/sidekiq-dup-guard.rb
|
39
|
+
- lib/sidekiq-dup-guard/middleware.rb
|
40
|
+
- lib/sidekiq-dup-guard/sidekiq_unique_job_filter.rb
|
41
|
+
- lib/sidekiq-dup-guard/version.rb
|
42
|
+
- sidekiq-dup-guard.gemspec
|
43
|
+
homepage: https://github.com/amagimedia/sidekiq-dup-guard
|
44
|
+
licenses:
|
45
|
+
- MIT
|
46
|
+
metadata: {}
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubygems_version: 3.1.2
|
63
|
+
signing_key:
|
64
|
+
specification_version: 4
|
65
|
+
summary: Sidekiq middleware to prevent enqueue of duplicate jobs
|
66
|
+
test_files: []
|