sidekiq-perform_once 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 970595fd431b198d2daa9d8cb04210e06358c039
4
+ data.tar.gz: 7aa571ff56eb08e128d08ef8c5f01c0fff7314c5
5
+ SHA512:
6
+ metadata.gz: dcdaa58dadf8862739cda46ac8f4117384cd27b265851ff29ee74a6102827f04d34784a88c4af26123ca1f480edfc61273f0e26fef49b842f1ba6777952c90bd
7
+ data.tar.gz: 27a7df4ca2836189da49566c7ccd475c75b14e7139c33205e5065fa518034e15ef2f91b4525b4167f3535b0fcb337342a014b7d0bf981b58fe03d0051b038fb9
@@ -0,0 +1,43 @@
1
+ require "active_support/core_ext/array/wrap"
2
+
3
+ module Sidekiq
4
+ module PerformOnce
5
+ class Base
6
+ def initialize(worker, *args)
7
+ @worker, @args = worker, args
8
+ end
9
+
10
+ def jobs
11
+ @jobs ||= Array.wrap(get_queue.select {|j| j.klass == @worker.name && j.args == @args })
12
+ end
13
+
14
+ def keep_one_job_or_perform!
15
+ case jobs
16
+ when (lambda &:one?) then jobs.first.try(:jid)
17
+ when (lambda &:many?) then keep_one_job!
18
+ when (lambda &:empty?) then perform!
19
+ end
20
+ end
21
+
22
+ private
23
+ def get_queue
24
+ Sidekiq::Queue.new(@worker.send(:get_sidekiq_options).try(:fetch, "queue"))
25
+ end
26
+
27
+ def kept_job
28
+ jobs.first
29
+ end
30
+
31
+ def perform!
32
+ @worker.perform_async(*@args)
33
+ end
34
+
35
+ def keep_one_job!
36
+ kept_jid = kept_job.try(:jid)
37
+ return if kept_jid.nil?
38
+ jobs.each {|j| j.delete if j.jid != kept_jid }
39
+ return kept_jid
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,22 @@
1
+ module Sidekiq
2
+ module PerformOnce
3
+ class Delayed < Base
4
+ def initialize(worker, delay, *args)
5
+ @worker, @delay, @args = worker, delay, args
6
+ end
7
+
8
+ private
9
+ def get_queue
10
+ Sidekiq::ScheduledSet.new
11
+ end
12
+
13
+ def kept_job
14
+ jobs.max_by(&:enqueued_at)
15
+ end
16
+
17
+ def perform!
18
+ @worker.perform_in(@delay, *@args)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ require "active_support/concern"
2
+ require "sidekiq/perform_once/base"
3
+ require "sidekiq/perform_once/delayed"
4
+
5
+ module Sidekiq
6
+ module PerformOnce
7
+ extend ActiveSupport::Concern
8
+
9
+ module ClassMethods
10
+ def perform_once(*args)
11
+ Sidekiq::PerformOnce::Base.new(self, *args).keep_one_job_or_perform!
12
+ end
13
+
14
+ def perform_once_in(delay, *args)
15
+ Sidekiq::PerformOnce::Delayed.new(self, delay.to_i, *args).keep_one_job_or_perform!
16
+ end
17
+
18
+ alias_method :perform_once_now, :perform_once
19
+ alias_method :perform_once_at, :perform_once_in
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,2 @@
1
+ require "sidekiq/api"
2
+ require "sidekiq/perform_once"
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq-perform_once
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Tõnis Simo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: sidekiq
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 3.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 3.0.0
41
+ description: Perform sidekiq jobs with the same arguments only once.
42
+ email:
43
+ - anton.estum@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/sidekiq/perform_once.rb
49
+ - lib/sidekiq/perform_once/base.rb
50
+ - lib/sidekiq/perform_once/delayed.rb
51
+ - lib/sidekiq_perform_once.rb
52
+ homepage: https://github.com/estum/sidekiq-perform_once
53
+ licenses: []
54
+ metadata: {}
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project:
71
+ rubygems_version: 2.4.1
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: Sidekiq::PerformOnce
75
+ test_files: []
76
+ has_rdoc: