sidekiq-unique-jobs 3.0.11 → 8.0.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +2163 -25
  3. data/LICENSE.txt +21 -0
  4. data/README.md +984 -47
  5. data/bin/uniquejobs +7 -0
  6. data/lib/sidekiq-unique-jobs.rb +2 -36
  7. data/lib/sidekiq_unique_jobs/batch_delete.rb +120 -0
  8. data/lib/sidekiq_unique_jobs/changelog.rb +68 -0
  9. data/lib/sidekiq_unique_jobs/cli.rb +95 -0
  10. data/lib/sidekiq_unique_jobs/config.rb +306 -33
  11. data/lib/sidekiq_unique_jobs/connection.rb +20 -0
  12. data/lib/sidekiq_unique_jobs/constants.rb +55 -0
  13. data/lib/sidekiq_unique_jobs/core_ext.rb +132 -0
  14. data/lib/sidekiq_unique_jobs/deprecation.rb +65 -0
  15. data/lib/sidekiq_unique_jobs/digests.rb +134 -0
  16. data/lib/sidekiq_unique_jobs/exceptions.rb +105 -0
  17. data/lib/sidekiq_unique_jobs/expiring_digests.rb +14 -0
  18. data/lib/sidekiq_unique_jobs/job.rb +63 -0
  19. data/lib/sidekiq_unique_jobs/json.rb +47 -0
  20. data/lib/sidekiq_unique_jobs/key.rb +98 -0
  21. data/lib/sidekiq_unique_jobs/lock/base_lock.rb +165 -0
  22. data/lib/sidekiq_unique_jobs/lock/client_validator.rb +28 -0
  23. data/lib/sidekiq_unique_jobs/lock/server_validator.rb +27 -0
  24. data/lib/sidekiq_unique_jobs/lock/until_and_while_executing.rb +71 -0
  25. data/lib/sidekiq_unique_jobs/lock/until_executed.rb +48 -0
  26. data/lib/sidekiq_unique_jobs/lock/until_executing.rb +43 -0
  27. data/lib/sidekiq_unique_jobs/lock/until_expired.rb +42 -0
  28. data/lib/sidekiq_unique_jobs/lock/validator.rb +96 -0
  29. data/lib/sidekiq_unique_jobs/lock/while_executing.rb +70 -0
  30. data/lib/sidekiq_unique_jobs/lock/while_executing_reject.rb +21 -0
  31. data/lib/sidekiq_unique_jobs/lock.rb +348 -0
  32. data/lib/sidekiq_unique_jobs/lock_args.rb +127 -0
  33. data/lib/sidekiq_unique_jobs/lock_config.rb +132 -0
  34. data/lib/sidekiq_unique_jobs/lock_digest.rb +79 -0
  35. data/lib/sidekiq_unique_jobs/lock_info.rb +68 -0
  36. data/lib/sidekiq_unique_jobs/lock_timeout.rb +62 -0
  37. data/lib/sidekiq_unique_jobs/lock_ttl.rb +77 -0
  38. data/lib/sidekiq_unique_jobs/lock_type.rb +37 -0
  39. data/lib/sidekiq_unique_jobs/locksmith.rb +390 -0
  40. data/lib/sidekiq_unique_jobs/logging/middleware_context.rb +44 -0
  41. data/lib/sidekiq_unique_jobs/logging.rb +236 -0
  42. data/lib/sidekiq_unique_jobs/lua/delete.lua +49 -0
  43. data/lib/sidekiq_unique_jobs/lua/delete_by_digest.lua +39 -0
  44. data/lib/sidekiq_unique_jobs/lua/delete_job_by_digest.lua +38 -0
  45. data/lib/sidekiq_unique_jobs/lua/find_digest_in_queues.lua +26 -0
  46. data/lib/sidekiq_unique_jobs/lua/lock.lua +108 -0
  47. data/lib/sidekiq_unique_jobs/lua/lock_until_expired.lua +92 -0
  48. data/lib/sidekiq_unique_jobs/lua/locked.lua +35 -0
  49. data/lib/sidekiq_unique_jobs/lua/queue.lua +88 -0
  50. data/lib/sidekiq_unique_jobs/lua/reap_orphans.lua +119 -0
  51. data/lib/sidekiq_unique_jobs/lua/shared/_common.lua +35 -0
  52. data/lib/sidekiq_unique_jobs/lua/shared/_current_time.lua +8 -0
  53. data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_queue.lua +22 -0
  54. data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_sorted_set.lua +29 -0
  55. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_process_set.lua +53 -0
  56. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_queues.lua +43 -0
  57. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_sorted_set.lua +24 -0
  58. data/lib/sidekiq_unique_jobs/lua/shared/_hgetall.lua +13 -0
  59. data/lib/sidekiq_unique_jobs/lua/shared/_upgrades.lua +3 -0
  60. data/lib/sidekiq_unique_jobs/lua/unlock.lua +112 -0
  61. data/lib/sidekiq_unique_jobs/lua/update_version.lua +40 -0
  62. data/lib/sidekiq_unique_jobs/lua/upgrade.lua +66 -0
  63. data/lib/sidekiq_unique_jobs/middleware/client.rb +42 -0
  64. data/lib/sidekiq_unique_jobs/middleware/server.rb +31 -0
  65. data/lib/sidekiq_unique_jobs/middleware.rb +41 -15
  66. data/lib/sidekiq_unique_jobs/normalizer.rb +17 -0
  67. data/lib/sidekiq_unique_jobs/on_conflict/log.rb +24 -0
  68. data/lib/sidekiq_unique_jobs/on_conflict/null_strategy.rb +16 -0
  69. data/lib/sidekiq_unique_jobs/on_conflict/raise.rb +17 -0
  70. data/lib/sidekiq_unique_jobs/on_conflict/reject.rb +75 -0
  71. data/lib/sidekiq_unique_jobs/on_conflict/replace.rb +82 -0
  72. data/lib/sidekiq_unique_jobs/on_conflict/reschedule.rb +39 -0
  73. data/lib/sidekiq_unique_jobs/on_conflict/strategy.rb +51 -0
  74. data/lib/sidekiq_unique_jobs/on_conflict.rb +44 -0
  75. data/lib/sidekiq_unique_jobs/options_with_fallback.rb +78 -0
  76. data/lib/sidekiq_unique_jobs/orphans/lua_reaper.rb +29 -0
  77. data/lib/sidekiq_unique_jobs/orphans/manager.rb +242 -0
  78. data/lib/sidekiq_unique_jobs/orphans/null_reaper.rb +24 -0
  79. data/lib/sidekiq_unique_jobs/orphans/observer.rb +42 -0
  80. data/lib/sidekiq_unique_jobs/orphans/reaper.rb +115 -0
  81. data/lib/sidekiq_unique_jobs/orphans/reaper_resurrector.rb +170 -0
  82. data/lib/sidekiq_unique_jobs/orphans/ruby_reaper.rb +313 -0
  83. data/lib/sidekiq_unique_jobs/redis/entity.rb +112 -0
  84. data/lib/sidekiq_unique_jobs/redis/hash.rb +56 -0
  85. data/lib/sidekiq_unique_jobs/redis/list.rb +32 -0
  86. data/lib/sidekiq_unique_jobs/redis/set.rb +32 -0
  87. data/lib/sidekiq_unique_jobs/redis/sorted_set.rb +102 -0
  88. data/lib/sidekiq_unique_jobs/redis/string.rb +51 -0
  89. data/lib/sidekiq_unique_jobs/redis.rb +11 -0
  90. data/lib/sidekiq_unique_jobs/reflectable.rb +26 -0
  91. data/lib/sidekiq_unique_jobs/reflections.rb +79 -0
  92. data/lib/sidekiq_unique_jobs/rspec/matchers/have_valid_sidekiq_options.rb +51 -0
  93. data/lib/sidekiq_unique_jobs/rspec/matchers.rb +26 -0
  94. data/lib/sidekiq_unique_jobs/script/caller.rb +133 -0
  95. data/lib/sidekiq_unique_jobs/script/client.rb +94 -0
  96. data/lib/sidekiq_unique_jobs/script/config.rb +68 -0
  97. data/lib/sidekiq_unique_jobs/script/dsl.rb +60 -0
  98. data/lib/sidekiq_unique_jobs/script/logging.rb +95 -0
  99. data/lib/sidekiq_unique_jobs/script/lua_error.rb +96 -0
  100. data/lib/sidekiq_unique_jobs/script/script.rb +75 -0
  101. data/lib/sidekiq_unique_jobs/script/scripts.rb +123 -0
  102. data/lib/sidekiq_unique_jobs/script/template.rb +41 -0
  103. data/lib/sidekiq_unique_jobs/script/timing.rb +35 -0
  104. data/lib/sidekiq_unique_jobs/script.rb +46 -0
  105. data/lib/sidekiq_unique_jobs/server.rb +62 -0
  106. data/lib/sidekiq_unique_jobs/sidekiq_unique_ext.rb +110 -37
  107. data/lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb +304 -0
  108. data/lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb +84 -0
  109. data/lib/sidekiq_unique_jobs/testing.rb +132 -9
  110. data/lib/sidekiq_unique_jobs/timer_task.rb +299 -0
  111. data/lib/sidekiq_unique_jobs/timing.rb +58 -0
  112. data/lib/sidekiq_unique_jobs/unlockable.rb +43 -0
  113. data/lib/sidekiq_unique_jobs/update_version.rb +25 -0
  114. data/lib/sidekiq_unique_jobs/upgrade_locks.rb +152 -0
  115. data/lib/sidekiq_unique_jobs/version.rb +5 -1
  116. data/lib/sidekiq_unique_jobs/version_check.rb +114 -0
  117. data/lib/sidekiq_unique_jobs/web/helpers.rb +175 -0
  118. data/lib/sidekiq_unique_jobs/web/views/_paging.erb +10 -0
  119. data/lib/sidekiq_unique_jobs/web/views/changelogs.erb +60 -0
  120. data/lib/sidekiq_unique_jobs/web/views/lock.erb +110 -0
  121. data/lib/sidekiq_unique_jobs/web/views/locks.erb +59 -0
  122. data/lib/sidekiq_unique_jobs/web.rb +109 -0
  123. data/lib/sidekiq_unique_jobs.rb +83 -0
  124. data/lib/tasks/changelog.rake +23 -0
  125. metadata +157 -126
  126. data/.gitignore +0 -10
  127. data/.rspec +0 -3
  128. data/.rubocop.yml +0 -36
  129. data/.travis.yml +0 -25
  130. data/Appraisals +0 -20
  131. data/Gemfile +0 -5
  132. data/LICENSE +0 -22
  133. data/Rakefile +0 -11
  134. data/gemfiles/sidekiq_2.15.gemfile +0 -9
  135. data/gemfiles/sidekiq_2.16.gemfile +0 -9
  136. data/gemfiles/sidekiq_2.17.gemfile +0 -9
  137. data/gemfiles/sidekiq_3.0.gemfile +0 -9
  138. data/gemfiles/sidekiq_develop.gemfile +0 -9
  139. data/lib/sidekiq_unique_jobs/connectors/redis_pool.rb +0 -11
  140. data/lib/sidekiq_unique_jobs/connectors/sidekiq_redis.rb +0 -9
  141. data/lib/sidekiq_unique_jobs/connectors/testing.rb +0 -11
  142. data/lib/sidekiq_unique_jobs/connectors.rb +0 -16
  143. data/lib/sidekiq_unique_jobs/middleware/client/strategies/testing_inline.rb +0 -25
  144. data/lib/sidekiq_unique_jobs/middleware/client/strategies/unique.rb +0 -76
  145. data/lib/sidekiq_unique_jobs/middleware/client/unique_jobs.rb +0 -39
  146. data/lib/sidekiq_unique_jobs/middleware/server/unique_jobs.rb +0 -69
  147. data/lib/sidekiq_unique_jobs/payload_helper.rb +0 -42
  148. data/sidekiq-unique-jobs.gemspec +0 -27
  149. data/spec/lib/.sidekiq_testing_enabled_spec.rb.swp +0 -0
  150. data/spec/lib/client_spec.rb +0 -173
  151. data/spec/lib/middleware/server/unique_jobs_spec.rb +0 -81
  152. data/spec/lib/sidekiq_testing_enabled_spec.rb +0 -123
  153. data/spec/lib/sidekiq_unique_ext_spec.rb +0 -70
  154. data/spec/lib/unlock_order_spec.rb +0 -64
  155. data/spec/spec_helper.rb +0 -37
  156. data/spec/support/my_worker.rb +0 -13
  157. data/spec/support/sidekiq_meta.rb +0 -17
  158. data/spec/support/unique_worker.rb +0 -13
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ module Script
5
+ #
6
+ # Misconfiguration is raised when gem is misconfigured
7
+ #
8
+ # @author Mikael Henriksson <mikael@zoolutions.se>
9
+ #
10
+ class Misconfiguration < RuntimeError
11
+ end
12
+
13
+ # LuaError raised on errors in Lua scripts
14
+ #
15
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
16
+ class LuaError < RuntimeError
17
+ # Reformats errors raised by redis representing failures while executing
18
+ # a lua script. The default errors have confusing messages and backtraces,
19
+ # and a type of +RuntimeError+. This class improves the message and
20
+ # modifies the backtrace to include the lua script itself in a reasonable
21
+ # way.
22
+
23
+ PATTERN = /ERR Error (compiling|running) script \(.*?\): .*?:(\d+): (.*)/.freeze
24
+ LIB_PATH = File.expand_path("..", __dir__).freeze
25
+ CONTEXT_LINE_NUMBER = 2
26
+
27
+ attr_reader :error, :file, :content
28
+
29
+ # Is this error one that should be reformatted?
30
+ #
31
+ # @param error [StandardError] the original error raised by redis
32
+ # @return [Boolean] is this an error that should be reformatted?
33
+ def self.intercepts?(error)
34
+ PATTERN.match?(error.message)
35
+ end
36
+
37
+ # Initialize a new {LuaError} from an existing redis error, adjusting
38
+ # the message and backtrace in the process.
39
+ #
40
+ # @param error [StandardError] the original error raised by redis
41
+ # @param script [Script] a DTO with information about the script
42
+ #
43
+ def initialize(error, script)
44
+ @error = error
45
+ @file = script.path
46
+ @content = script.source
47
+ @backtrace = @error.backtrace
48
+
49
+ @error.message.match(PATTERN) do |regexp_match|
50
+ line_number = regexp_match[2].to_i
51
+ message = regexp_match[3]
52
+ error_context = generate_error_context(content, line_number)
53
+
54
+ super("#{message}\n\n#{error_context}\n\n")
55
+ set_backtrace(generate_backtrace(file, line_number))
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ # :nocov:
62
+ def generate_error_context(content, line_number)
63
+ lines = content.lines.to_a
64
+ beginning_line_number = [1, line_number - CONTEXT_LINE_NUMBER].max
65
+ ending_line_number = [lines.count, line_number + CONTEXT_LINE_NUMBER].min
66
+ line_number_width = ending_line_number.to_s.length
67
+
68
+ (beginning_line_number..ending_line_number).map do |number|
69
+ indicator = (number == line_number) ? "=>" : " "
70
+ formatted_number = format("%#{line_number_width}d", number)
71
+ " #{indicator} #{formatted_number}: #{lines[number - 1]}"
72
+ end.join.chomp
73
+ end
74
+
75
+ # :nocov:
76
+ def generate_backtrace(file, line_number)
77
+ pre_gem = backtrace_before_entering_gem(@backtrace)
78
+ index_of_first_gem_line = (@backtrace.size - pre_gem.size - 1)
79
+
80
+ pre_gem.unshift(@backtrace[index_of_first_gem_line])
81
+ pre_gem.unshift("#{file}:#{line_number}")
82
+ pre_gem
83
+ end
84
+
85
+ # :nocov:
86
+ def backtrace_before_entering_gem(backtrace)
87
+ backtrace.reverse.take_while { |line| !line_from_gem(line) }.reverse
88
+ end
89
+
90
+ # :nocov:
91
+ def line_from_gem(line)
92
+ line.split(":").first.include?(LIB_PATH)
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ module Script
5
+ # Interface to dealing with .lua files
6
+ #
7
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
8
+ class Script
9
+ def self.load(name, root_path, conn)
10
+ script = new(name: name, root_path: root_path)
11
+ script.load(conn)
12
+ end
13
+
14
+ #
15
+ # @!attribute [r] script_name
16
+ # @return [Symbol, String] the name of the script without extension
17
+ attr_reader :name
18
+ #
19
+ # @!attribute [r] script_path
20
+ # @return [String] the path to the script on disk
21
+ attr_reader :path
22
+ #
23
+ # @!attribute [r] root_path
24
+ # @return [Pathname]
25
+ attr_reader :root_path
26
+ #
27
+ # @!attribute [r] source
28
+ # @return [String] the source code of the lua script
29
+ attr_reader :source
30
+ #
31
+ # @!attribute [rw] sha
32
+ # @return [String] the sha of the script
33
+ attr_reader :sha
34
+ #
35
+ # @!attribute [rw] call_count
36
+ # @return [Integer] the number of times the script was called/executed
37
+ attr_reader :call_count
38
+
39
+ def initialize(name:, root_path:)
40
+ @name = name
41
+ @root_path = root_path
42
+ @path = root_path.join("#{name}.lua").to_s
43
+ @source = render_file
44
+ @sha = compiled_sha
45
+ @call_count = 0
46
+ end
47
+
48
+ def ==(other)
49
+ sha == compiled_sha && compiled_sha == other.sha
50
+ end
51
+
52
+ def increment_call_count
53
+ @call_count += 1
54
+ end
55
+
56
+ def changed?
57
+ compiled_sha != sha
58
+ end
59
+
60
+ def render_file
61
+ Template.new(root_path).render(path)
62
+ end
63
+
64
+ def compiled_sha
65
+ Digest::SHA1.hexdigest(source)
66
+ end
67
+
68
+ def load(conn)
69
+ @sha = conn.script(:load, source)
70
+
71
+ self
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,123 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ module Script
5
+ # Interface to dealing with .lua files
6
+ #
7
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
8
+ class Scripts
9
+ #
10
+ # @return [Concurrent::Map] a map with configured script paths
11
+ SCRIPT_PATHS = Concurrent::Map.new
12
+
13
+ #
14
+ # Fetch a scripts configuration for path
15
+ #
16
+ # @param [Pathname] root_path the path to scripts
17
+ #
18
+ # @return [Scripts] a collection of scripts
19
+ #
20
+ def self.fetch(root_path)
21
+ if (scripts = SCRIPT_PATHS.get(root_path))
22
+ return scripts
23
+ end
24
+
25
+ create(root_path)
26
+ end
27
+
28
+ #
29
+ # Create a new scripts collection based on path
30
+ #
31
+ # @param [Pathname] root_path the path to scripts
32
+ #
33
+ # @return [Scripts] a collection of scripts
34
+ #
35
+ def self.create(root_path)
36
+ scripts = new(root_path)
37
+ store(scripts)
38
+ end
39
+
40
+ #
41
+ # Store the scripts collection in memory
42
+ #
43
+ # @param [Scripts] scripts the path to scripts
44
+ #
45
+ # @return [Scripts] the scripts instance that was stored
46
+ #
47
+ def self.store(scripts)
48
+ SCRIPT_PATHS.put(scripts.root_path, scripts)
49
+ scripts
50
+ end
51
+
52
+ #
53
+ # @!attribute [r] scripts
54
+ # @return [Concurrent::Map] a collection of loaded scripts
55
+ attr_reader :scripts
56
+
57
+ #
58
+ # @!attribute [r] root_path
59
+ # @return [Pathname] the path to the directory with lua scripts
60
+ attr_reader :root_path
61
+
62
+ def initialize(path)
63
+ raise ArgumentError, "path needs to be a Pathname" unless path.is_a?(Pathname)
64
+
65
+ @scripts = Concurrent::Map.new
66
+ @root_path = path
67
+ end
68
+
69
+ def fetch(name, conn)
70
+ if (script = scripts.get(name.to_sym))
71
+ return script
72
+ end
73
+
74
+ load(name, conn)
75
+ end
76
+
77
+ def load(name, conn)
78
+ script = Script.load(name, root_path, conn)
79
+ scripts.put(name.to_sym, script)
80
+
81
+ script
82
+ end
83
+
84
+ def delete(script)
85
+ if script.is_a?(Script)
86
+ scripts.delete(script.name)
87
+ else
88
+ scripts.delete(script.to_sym)
89
+ end
90
+ end
91
+
92
+ def kill(conn)
93
+ if conn.respond_to?(:namespace)
94
+ conn.redis.script(:kill)
95
+ else
96
+ conn.script(:kill)
97
+ end
98
+ end
99
+
100
+ #
101
+ # Execute a lua script with given name
102
+ #
103
+ # @note this method is recursive if we need to load a lua script
104
+ # that wasn't previously loaded.
105
+ #
106
+ # @param [Symbol] name the name of the script to execute
107
+ # @param [Redis] conn the redis connection to use for execution
108
+ # @param [Array<String>] keys script keys
109
+ # @param [Array<Object>] argv script arguments
110
+ #
111
+ # @return value from script
112
+ #
113
+ def execute(name, conn, keys: [], argv: [])
114
+ script = fetch(name, conn)
115
+ conn.evalsha(script.sha, keys, argv)
116
+ end
117
+
118
+ def count
119
+ scripts.keys.size
120
+ end
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ # Interface to dealing with .lua files
5
+ #
6
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
7
+ module Script
8
+ #
9
+ # Class Template provides LUA script partial template rendering
10
+ #
11
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
12
+ #
13
+ class Template
14
+ def initialize(script_path)
15
+ @script_path = script_path
16
+ end
17
+
18
+ #
19
+ # Renders a Lua script and includes any partials in that file
20
+ # all `<%= include_partial '' %>` replaced with the actual contents of the partial
21
+ #
22
+ # @param [Pathname] pathname the path to the
23
+ #
24
+ # @return [String] the rendered Luascript
25
+ #
26
+ def render(pathname)
27
+ @partial_templates ||= {}
28
+ ::ERB.new(File.read(pathname)).result(binding)
29
+ end
30
+
31
+ # helper method to include a lua partial within another lua script
32
+ #
33
+ def include_partial(relative_path)
34
+ return if @partial_templates.key?(relative_path)
35
+
36
+ @partial_templates[relative_path] = nil
37
+ render(Pathname.new("#{@script_path}/#{relative_path}"))
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ module Script
5
+ # Handles timing> of things
6
+ #
7
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
8
+ module Timing
9
+ module_function
10
+
11
+ #
12
+ # Used for timing method calls
13
+ #
14
+ #
15
+ # @return [yield return, Float]
16
+ #
17
+ def timed
18
+ start_time = now
19
+
20
+ [yield, now - start_time]
21
+ end
22
+
23
+ #
24
+ # Returns a float representation of the current time.
25
+ # Either from Process or Time
26
+ #
27
+ #
28
+ # @return [Float]
29
+ #
30
+ def now
31
+ (Process.clock_gettime(Process::CLOCK_MONOTONIC) * 1000).to_i
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sidekiq_unique_jobs/script/template"
4
+ require "sidekiq_unique_jobs/script/lua_error"
5
+ require "sidekiq_unique_jobs/script/script"
6
+ require "sidekiq_unique_jobs/script/scripts"
7
+ require "sidekiq_unique_jobs/script/config"
8
+ require "sidekiq_unique_jobs/script/timing"
9
+ require "sidekiq_unique_jobs/script/logging"
10
+ require "sidekiq_unique_jobs/script/dsl"
11
+ require "sidekiq_unique_jobs/script/client"
12
+
13
+ module SidekiqUniqueJobs
14
+ # Interface to dealing with .lua files
15
+ #
16
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
17
+ module Script
18
+ include SidekiqUniqueJobs::Script::DSL
19
+
20
+ configure do |config|
21
+ config.scripts_path = Pathname.new(__FILE__).dirname.join("lua")
22
+ config.logger = Sidekiq.logger # TODO: This becomes a little weird
23
+ end
24
+
25
+ #
26
+ # The current logger
27
+ #
28
+ #
29
+ # @return [Logger] the configured logger
30
+ #
31
+ def self.logger
32
+ config.logger
33
+ end
34
+
35
+ #
36
+ # Set a new logger
37
+ #
38
+ # @param [Logger] other another logger
39
+ #
40
+ # @return [Logger] the new logger
41
+ #
42
+ def self.logger=(other)
43
+ config.logger = other
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ # The unique sidekiq middleware for the server processor
5
+ #
6
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
7
+ class Server
8
+ #
9
+ # @return [Proc] returns a default death handler for the gem to cleanup dead locks
10
+ DEATH_HANDLER = (lambda do |job, _ex|
11
+ return unless (digest = job["lock_digest"])
12
+
13
+ SidekiqUniqueJobs::Digests.new.delete_by_digest(digest)
14
+ end).freeze
15
+ #
16
+ # Configure the server middleware
17
+ #
18
+ #
19
+ # @return [Sidekiq] the sidekiq configuration
20
+ #
21
+ def self.configure(config)
22
+ config.on(:startup) { start }
23
+ config.on(:shutdown) { stop }
24
+
25
+ return unless config.respond_to?(:death_handlers)
26
+
27
+ config.death_handlers << death_handler
28
+ end
29
+
30
+ #
31
+ # Start the sidekiq unique jobs server process
32
+ #
33
+ #
34
+ # @return [void]
35
+ #
36
+ def self.start
37
+ SidekiqUniqueJobs::UpdateVersion.call
38
+ SidekiqUniqueJobs::Orphans::Manager.start
39
+ SidekiqUniqueJobs::Orphans::ReaperResurrector.start
40
+ end
41
+
42
+ #
43
+ # Stop the sidekiq unique jobs server process
44
+ #
45
+ #
46
+ # @return [void]
47
+ #
48
+ def self.stop
49
+ SidekiqUniqueJobs::Orphans::Manager.stop
50
+ end
51
+
52
+ #
53
+ # A death handler for dead jobs
54
+ #
55
+ #
56
+ # @return [lambda]
57
+ #
58
+ def self.death_handler
59
+ DEATH_HANDLER
60
+ end
61
+ end
62
+ end
@@ -1,68 +1,141 @@
1
- require 'sidekiq/api'
1
+ # frozen_string_literal: true
2
2
 
3
+ require "sidekiq/api"
4
+
5
+ #
6
+ # @api private
7
+ #
3
8
  module Sidekiq
4
- class Job
9
+ # See Sidekiq::Api
10
+ class SortedEntry
11
+ #
12
+ # Provides extensions for unlocking jobs that are removed and deleted
13
+ #
14
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
15
+ #
5
16
  module UniqueExtension
6
- def self.included(base)
7
- base.class_eval do
8
- alias_method :delete_orig, :delete
9
- alias_method :delete, :delete_ext
10
- end
17
+ #
18
+ # Wraps the original method to ensure locks for the job are deleted
19
+ #
20
+ #
21
+ # @return [Hash] the deleted sidekiq job hash
22
+ #
23
+ def delete
24
+ SidekiqUniqueJobs::Unlockable.delete!(item) if super
25
+ item
11
26
  end
12
27
 
13
- def delete_ext
14
- unlock(payload_hash(item))
15
- delete_orig
28
+ private
29
+
30
+ #
31
+ # Wraps the original method to ensure locks for the job are deleted
32
+ #
33
+ #
34
+ # @yieldparam [Hash] message the sidekiq job hash
35
+ def remove_job
36
+ super do |message|
37
+ SidekiqUniqueJobs::Unlockable.delete!(Sidekiq.load_json(message))
38
+ yield message
39
+ end
16
40
  end
41
+ end
17
42
 
18
- protected
43
+ prepend UniqueExtension
44
+ end
19
45
 
20
- def payload_hash(item)
21
- SidekiqUniqueJobs::PayloadHelper.get_payload(item['class'], item['queue'], item['args'])
46
+ # See Sidekiq::Api
47
+ class ScheduledSet
48
+ #
49
+ # Provides extensions for unlocking jobs that are removed and deleted
50
+ #
51
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
52
+ #
53
+ module UniqueExtension
54
+ #
55
+ # Wraps the original method to ensure locks for the job are deleted
56
+ #
57
+ #
58
+ # @param [Integer, Float] score the score in the scheduled set
59
+ # @param [String] job_id the Sidekiq JID
60
+ #
61
+ def delete(score, job_id)
62
+ entry = find_job(job_id)
63
+ SidekiqUniqueJobs::Unlockable.delete!(entry.item) if super(score, job_id)
64
+ entry
22
65
  end
66
+ end
23
67
 
24
- def unlock(payload_hash)
25
- Sidekiq.redis { |conn| conn.del(payload_hash) }
68
+ prepend UniqueExtension
69
+ end
70
+
71
+ # See Sidekiq::Api
72
+ class JobRecord
73
+ #
74
+ # Provides extensions for unlocking jobs that are removed and deleted
75
+ #
76
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
77
+ #
78
+ module UniqueExtension
79
+ #
80
+ # Wraps the original method to ensure locks for the job are deleted
81
+ #
82
+ def delete
83
+ SidekiqUniqueJobs::Unlockable.delete!(item)
84
+ super
26
85
  end
27
86
  end
28
- include UniqueExtension
87
+
88
+ prepend UniqueExtension
29
89
  end
30
90
 
91
+ # See Sidekiq::Api
31
92
  class Queue
93
+ #
94
+ # Provides extensions for unlocking jobs that are removed and deleted
95
+ #
96
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
97
+ #
32
98
  module UniqueExtension
33
- def self.included(base)
34
- base.class_eval do
35
- alias_method :clear_orig, :clear
36
- alias_method :clear, :clear_ext
37
- end
38
- end
39
-
40
- def clear_ext
99
+ #
100
+ # Wraps the original method to ensure locks for the job are deleted
101
+ #
102
+ def clear
41
103
  each(&:delete)
42
- clear_orig
104
+ super
43
105
  end
44
106
  end
45
107
 
46
- include UniqueExtension
108
+ prepend UniqueExtension
47
109
  end
48
110
 
111
+ # See Sidekiq::Api
49
112
  class JobSet
113
+ #
114
+ # Provides extensions for unlocking jobs that are removed and deleted
115
+ #
116
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
117
+ #
50
118
  module UniqueExtension
51
- def self.included(base)
52
- base.class_eval do
53
- if base.method_defined?(:clear)
54
- alias_method :clear_orig, :clear
55
- alias_method :clear, :clear_ext
56
- end
57
- end
119
+ #
120
+ # Wraps the original method to ensure locks for the job are deleted
121
+ #
122
+ def clear
123
+ each(&:delete)
124
+ super
58
125
  end
59
126
 
60
- def clear_ext
61
- each(&:delete_ext)
62
- clear_orig
127
+ #
128
+ # Wraps the original method to ensure locks for the job are deleted
129
+ #
130
+ #
131
+ # @param [String] name the name of the key
132
+ # @param [String] value a sidekiq job hash
133
+ #
134
+ def delete_by_value(name, value)
135
+ SidekiqUniqueJobs::Unlockable.delete!(Sidekiq.load_json(value)) if super(name, value)
63
136
  end
64
137
  end
65
138
 
66
- include UniqueExtension
139
+ prepend UniqueExtension
67
140
  end
68
141
  end