sidekiq-uniq 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fcd844979290e9e9026c43792d00b64efeb01a57
4
- data.tar.gz: a63b235c7f9e447b14520c329681a2fee401f7e0
3
+ metadata.gz: e977a80b75335fa9eca79ea280b004aca7ddac32
4
+ data.tar.gz: 806236848f432f74c3a56cdf19f75fa18ea75d52
5
5
  SHA512:
6
- metadata.gz: 57d56ee2f997d7a03e75c01d817b50c0d154fe1c1e7b125cc3ea74d4eff38ee6c5b806b6cc4e569de9cf7219a492ed3299402249b4da303dd34f75f4562701e0
7
- data.tar.gz: 8b31a37e790764ad25b480ea76810a8cf01f8e712b2ff47e83288f0250d2ff7797de7feeaae13f15f66de7d388c3dc06afecebf869c843a686e6c75d73858cfe
6
+ metadata.gz: 017099270082b6055b7be2f12af8f487b0d897e9e1a1e4281d001479a9ff5c3c7cc36b8089a9e7d09f02790669ff422746be1462ea971bd4074fe422fdeadf68
7
+ data.tar.gz: 68407f0eba98242888c7a6497d29aeb00aaface0857dc9e2953248c0755eb203a0bdff5ab0df7fd55f92db3b87d51a6f4267e270adc5dee74f0d2f473e787a11
@@ -1,15 +1,33 @@
1
+ require 'sidekiq/uniq/status'
2
+
1
3
  module Sidekiq
2
4
  module Uniq
3
- class Middleware
5
+
6
+ class ClientMiddleware
7
+
8
+ # Runs before job is added to queue
9
+
4
10
  def call(worker_class, msg, queue_name, redis_pool)
5
- unless msg['unique'] === false
6
- queue = Sidekiq::Queue.new(msg['queue'])
7
- queue.each do |job|
8
- return false if job.klass == msg['class'] && job.args === msg['args']
9
- end
11
+ if msg['unique'] === false
12
+ yield
13
+ else
14
+ return false if Status.running_or_enqueued(msg, redis_pool)
15
+ Status.save_status(msg, :enqueued, redis_pool)
16
+ yield
10
17
  end
18
+ end
19
+ end
20
+
21
+ class ServerMiddleware
22
+
23
+ # Runs before job is executed
24
+
25
+ def call(worker_class, msg, queue_name)
26
+ Status.save_status(msg, :running)
11
27
  yield
28
+ Status.save_status(msg, :completed)
12
29
  end
13
30
  end
31
+
14
32
  end
15
33
  end
@@ -7,13 +7,13 @@ module Sidekiq
7
7
 
8
8
  Sidekiq.configure_server do |config|
9
9
  config.client_middleware do |chain|
10
- chain.add Sidekiq::Uniq::Middleware
10
+ chain.add Sidekiq::Uniq::ServerMiddleware
11
11
  end
12
12
  end
13
13
 
14
14
  Sidekiq.configure_client do |config|
15
15
  config.client_middleware do |chain|
16
- chain.add Sidekiq::Uniq::Middleware
16
+ chain.add Sidekiq::Uniq::ClientMiddleware
17
17
  end
18
18
  end
19
19
 
@@ -0,0 +1,57 @@
1
+ module Sidekiq
2
+ module Uniq
3
+ class Status
4
+ KEY = 'sidekiq_uniq_job'
5
+
6
+ def self.expiration
7
+ @expiration ||= 60 * 30
8
+ end
9
+
10
+ def self.expiration=(seconds)
11
+ @expiration = seconds
12
+ end
13
+
14
+ def self.key(msg)
15
+ KEY + ':' + Digest::MD5.hexdigest(msg[:class].to_s + ':' + msg[:args].inspect)
16
+ end
17
+
18
+ def self.save_status(msg, status, redis = nil)
19
+ Status.redis(redis) do |conn|
20
+ key = Status.key(msg)
21
+ conn.multi do
22
+ conn.set key, status.to_s
23
+ conn.expire key, Status.expiration
24
+ end
25
+ end
26
+ end
27
+
28
+ def self.running_or_enqueued(msg, redis = nil)
29
+ status = ''
30
+ Status.redis(redis) do |conn|
31
+ status = conn.get(Status.key(msg)).to_s
32
+ end
33
+ status === 'running' || status === 'enqueued'
34
+ end
35
+
36
+ def self.clear(redis = nil)
37
+ Status.redis(redis) do |conn|
38
+ keys = conn.keys "#{KEY}*"
39
+ keys.each{ |key| conn.del(key) }
40
+ end
41
+ end
42
+
43
+ def self.redis(redis = nil)
44
+ if redis
45
+ redis.with do |conn|
46
+ yield conn
47
+ end
48
+ else
49
+ Sidekiq.redis do |conn|
50
+ yield conn
51
+ end
52
+ end
53
+ end
54
+
55
+ end
56
+ end
57
+ end
@@ -1,5 +1,5 @@
1
1
  module Sidekiq
2
2
  module Uniq
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
Binary file
data/sidekiq-uniq.gemspec CHANGED
@@ -22,4 +22,5 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "bundler", "~> 1.9"
23
23
  spec.add_development_dependency "rake", "~> 10.0"
24
24
  spec.add_dependency "sidekiq", "~> 3.5.1"
25
+ spec.add_dependency "sidekiq-status", "~> 0.5.3"
25
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq-uniq
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Caio SBA
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-28 00:00:00.000000000 Z
11
+ date: 2015-10-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ~>
53
53
  - !ruby/object:Gem::Version
54
54
  version: 3.5.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: sidekiq-status
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.5.3
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 0.5.3
55
69
  description: This is a Sidekiq extension that doesn't allow a job to be enqueued if
56
70
  it's already in the queue
57
71
  email:
@@ -71,8 +85,10 @@ files:
71
85
  - lib/sidekiq/uniq.rb
72
86
  - lib/sidekiq/uniq/middleware.rb
73
87
  - lib/sidekiq/uniq/railtie.rb
88
+ - lib/sidekiq/uniq/status.rb
74
89
  - lib/sidekiq/uniq/version.rb
75
90
  - sidekiq-uniq-0.1.0.gem
91
+ - sidekiq-uniq-0.2.0.gem
76
92
  - sidekiq-uniq.gemspec
77
93
  homepage: https://github.com/caiosba/sidekiq-uniq
78
94
  licenses: