sidekiq-ultimate 0.0.1.alpha → 0.0.1.alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 21eb153f8f9af214ece524d28299bb23e2a1918d90a8b99e41a63031eeb4037b
4
- data.tar.gz: 9b930c25ddedb5177da9ee3bad6756c37f156355e9c639c00ecf8f5803016aeb
3
+ metadata.gz: 668c153d01d544b176281fcdaf5774abcb1757c9466ddbed19504265093ea846
4
+ data.tar.gz: 54cabd4c8392a1754f206489c4ed9dd523f1d6fb4e4711bcb7efc5d3db8540a4
5
5
  SHA512:
6
- metadata.gz: b38096795e63949611c87f0c42f35314ba7c0ec64b459fb91e6bb794e65aef2994c807a7d06873bedf92e78a343fedfcb95ece065478c5c1e0abf6a0b9d614c3
7
- data.tar.gz: f36223cef6b03277f7090c0e5b1d3b33aeb70b7157c31205adbfc24d7a5ce4199499bd49ca215acfeac008a21d86973f321736326651e117594d0cd0fa031b9e
6
+ metadata.gz: 4250c73685481db1974a6fac3ae1eced983d6f8af142d1dc445159593c61769525882a463b9b05bd76403ec65ddf0cd32fbb97ca87b66f3c185703a8bc9e5784
7
+ data.tar.gz: fc8f850e024baad6b917c43ec285b8fff056ab26d9770fb20223ddfb1673037a652b6e02d18dcbdb4ea37edeaedcc4497ff26ee169e28134cc5e12c9202281ae
@@ -3,12 +3,19 @@
3
3
  require "redis/lockers"
4
4
  require "redis/prescription"
5
5
 
6
+ require "sidekiq/ultimate/queue_name"
7
+
6
8
  module Sidekiq
7
9
  module Ultimate
8
10
  # Lost jobs resurrector.
9
11
  module Resurrector
10
- LPOPRPUSH = Redis::Prescription.read("#{__dir__}/lpoprpush.lua")
11
- private_constant :LPOPRPUSH
12
+ RESURRECT = Redis::Prescription.read \
13
+ "#{__dir__}/resurrector/resurrect.lua"
14
+ private_constant :RESURRECT
15
+
16
+ SAFECLEAN = Redis::Prescription.read \
17
+ "#{__dir__}/resurrector/safeclean.lua"
18
+ private_constant :SAFECLEAN
12
19
 
13
20
  MAIN_KEY = "ultimate:resurrector"
14
21
  private_constant :MAIN_KEY
@@ -18,30 +25,36 @@ module Sidekiq
18
25
 
19
26
  class << self
20
27
  def setup!
21
- ctulhu = Concurrent::TimerTask.new(:execution_interval => 5) do
22
- resurrect!
23
- end
28
+ ctulhu = nil
24
29
 
25
30
  Sidekiq.on(:startup) do
26
- register_process!
27
- ctulhu.execute
31
+ defibrillate!
32
+
33
+ ctulhu = Concurrent::TimerTask.execute(:run_now => true) do
34
+ resurrect!
35
+ end
28
36
  end
29
37
 
30
- Sidekiq.on(:shutdown) { ctulhu.shutdown }
38
+ Sidekiq.on(:shutdown) { ctulhu&.shutdown }
39
+
40
+ Sidekiq.on(:heartbeat) { defibrillate! }
31
41
  end
32
42
 
33
43
  def resurrect!
34
44
  lock do
35
45
  casualties.each do |identity|
36
- queues(identity).each { |queue| resurrect(queue) }
37
- cleanup(identity)
46
+ queues = queues_of(identity).each { |queue| resurrect(queue) }
47
+ cleanup(identity, queues.map(&:inproc))
38
48
  end
39
49
  end
50
+ rescue => e
51
+ Sidekiq.logger.error("#{self}.resurrect! failed: #{e}")
52
+ raise
40
53
  end
41
54
 
42
55
  private
43
56
 
44
- def register_process!
57
+ def defibrillate!
45
58
  Sidekiq.redis do |redis|
46
59
  queues = JSON.dump(Sidekiq.options[:queues].uniq)
47
60
  identity = Object.new.tap { |o| o.extend Sidekiq::Util }.identity
@@ -68,7 +81,7 @@ module Sidekiq
68
81
  end
69
82
  end
70
83
 
71
- def queues(identity)
84
+ def queues_of(identity)
72
85
  Sidekiq.redis do |redis|
73
86
  queues = redis.hget(MAIN_KEY, identity)
74
87
 
@@ -82,16 +95,17 @@ module Sidekiq
82
95
 
83
96
  def resurrect(queue)
84
97
  Sidekiq.redis do |redis|
85
- kwargs = { :keys => [queue.inproc, queue.pending] }
86
- count = 0
87
-
88
- count += 1 while LPOPRPUSH.eval(redis, **kwargs)
89
- redis.del(queue.inproc)
98
+ RESURRECT.eval(redis, :keys => [queue.inproc, queue.pending])
90
99
  end
91
100
  end
92
101
 
93
- def cleanup(identity)
94
- Sidekiq.redis { |redis| redis.hdel(MAIN_KEY, identity) }
102
+ def cleanup(identity, inprocs)
103
+ Sidekiq.redis do |redis|
104
+ SAFECLEAN.eval(redis, {
105
+ :keys => [MAIN_KEY, *inprocs],
106
+ :argv => [identity]
107
+ })
108
+ end
95
109
  end
96
110
  end
97
111
  end
@@ -0,0 +1,13 @@
1
+ local src = KEYS[1]
2
+ local dst = KEYS[2]
3
+ local val = nil
4
+
5
+ while true do
6
+ val = redis.call("LPOP", src)
7
+
8
+ if val then
9
+ redis.call("RPUSH", dst, val)
10
+ else
11
+ return
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ local rip = true
2
+
3
+ for i, v in ipairs(KEYS) do
4
+ if 2 < i then
5
+ if 0 == redis.call("LLEN", v) then
6
+ redis.call("DEL", v)
7
+ else
8
+ rip = false
9
+ end
10
+ end
11
+ end
12
+
13
+ if rip then
14
+ redis.call("HDEL", KEYS[1], ARGV[1])
15
+ end
@@ -3,6 +3,6 @@
3
3
  module Sidekiq
4
4
  module Ultimate
5
5
  # Gem version.
6
- VERSION = "0.0.1.alpha"
6
+ VERSION = "0.0.1.alpha.2"
7
7
  end
8
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq-ultimate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.alpha
4
+ version: 0.0.1.alpha.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexey Zapparov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-02-22 00:00:00.000000000 Z
11
+ date: 2018-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -114,9 +114,10 @@ files:
114
114
  - lib/sidekiq/ultimate.rb
115
115
  - lib/sidekiq/ultimate/expirable_list.rb
116
116
  - lib/sidekiq/ultimate/fetch.rb
117
- - lib/sidekiq/ultimate/lpoprpush.lua
118
117
  - lib/sidekiq/ultimate/queue_name.rb
119
118
  - lib/sidekiq/ultimate/resurrector.rb
119
+ - lib/sidekiq/ultimate/resurrector/resurrect.lua
120
+ - lib/sidekiq/ultimate/resurrector/safeclean.lua
120
121
  - lib/sidekiq/ultimate/unit_of_work.rb
121
122
  - lib/sidekiq/ultimate/version.rb
122
123
  - sidekiq-ultimate.gemspec
@@ -1,9 +0,0 @@
1
- local src = KEYS[1]
2
- local dst = KEYS[2]
3
- local val = redis.call("LPOP", src)
4
-
5
- if val then
6
- redis.call("RPUSH", dst, val)
7
- end
8
-
9
- return val