sidekiq-unique-jobs 6.0.25 → 7.1.33

Sign up to get free protection for your applications and to get access to all the features.
Files changed (130) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1157 -41
  3. data/README.md +825 -291
  4. data/lib/sidekiq_unique_jobs/batch_delete.rb +124 -0
  5. data/lib/sidekiq_unique_jobs/changelog.rb +78 -0
  6. data/lib/sidekiq_unique_jobs/cli.rb +57 -29
  7. data/lib/sidekiq_unique_jobs/config.rb +319 -0
  8. data/lib/sidekiq_unique_jobs/connection.rb +6 -5
  9. data/lib/sidekiq_unique_jobs/constants.rb +46 -25
  10. data/lib/sidekiq_unique_jobs/core_ext.rb +80 -0
  11. data/lib/sidekiq_unique_jobs/deprecation.rb +65 -0
  12. data/lib/sidekiq_unique_jobs/digests.rb +70 -102
  13. data/lib/sidekiq_unique_jobs/exceptions.rb +88 -12
  14. data/lib/sidekiq_unique_jobs/expiring_digests.rb +14 -0
  15. data/lib/sidekiq_unique_jobs/job.rb +46 -12
  16. data/lib/sidekiq_unique_jobs/json.rb +47 -0
  17. data/lib/sidekiq_unique_jobs/key.rb +98 -0
  18. data/lib/sidekiq_unique_jobs/lock/base_lock.rb +111 -82
  19. data/lib/sidekiq_unique_jobs/lock/client_validator.rb +28 -0
  20. data/lib/sidekiq_unique_jobs/lock/server_validator.rb +27 -0
  21. data/lib/sidekiq_unique_jobs/lock/until_and_while_executing.rb +40 -15
  22. data/lib/sidekiq_unique_jobs/lock/until_executed.rb +30 -7
  23. data/lib/sidekiq_unique_jobs/lock/until_executing.rb +26 -2
  24. data/lib/sidekiq_unique_jobs/lock/until_expired.rb +27 -15
  25. data/lib/sidekiq_unique_jobs/lock/validator.rb +96 -0
  26. data/lib/sidekiq_unique_jobs/lock/while_executing.rb +26 -12
  27. data/lib/sidekiq_unique_jobs/lock/while_executing_reject.rb +3 -3
  28. data/lib/sidekiq_unique_jobs/lock.rb +342 -0
  29. data/lib/sidekiq_unique_jobs/lock_args.rb +127 -0
  30. data/lib/sidekiq_unique_jobs/lock_config.rb +126 -0
  31. data/lib/sidekiq_unique_jobs/lock_digest.rb +79 -0
  32. data/lib/sidekiq_unique_jobs/lock_info.rb +68 -0
  33. data/lib/sidekiq_unique_jobs/lock_timeout.rb +62 -0
  34. data/lib/sidekiq_unique_jobs/lock_ttl.rb +77 -0
  35. data/lib/sidekiq_unique_jobs/lock_type.rb +37 -0
  36. data/lib/sidekiq_unique_jobs/locksmith.rb +305 -101
  37. data/lib/sidekiq_unique_jobs/logging/middleware_context.rb +44 -0
  38. data/lib/sidekiq_unique_jobs/logging.rb +202 -33
  39. data/lib/sidekiq_unique_jobs/lua/delete.lua +51 -0
  40. data/lib/sidekiq_unique_jobs/lua/delete_by_digest.lua +42 -0
  41. data/lib/sidekiq_unique_jobs/lua/delete_job_by_digest.lua +38 -0
  42. data/lib/sidekiq_unique_jobs/lua/find_digest_in_queues.lua +26 -0
  43. data/lib/sidekiq_unique_jobs/lua/lock.lua +99 -0
  44. data/lib/sidekiq_unique_jobs/lua/lock_until_expired.lua +92 -0
  45. data/lib/sidekiq_unique_jobs/lua/locked.lua +35 -0
  46. data/lib/sidekiq_unique_jobs/lua/queue.lua +87 -0
  47. data/lib/sidekiq_unique_jobs/lua/reap_orphans.lua +122 -0
  48. data/lib/sidekiq_unique_jobs/lua/shared/_common.lua +40 -0
  49. data/lib/sidekiq_unique_jobs/lua/shared/_current_time.lua +8 -0
  50. data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_queue.lua +22 -0
  51. data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_sorted_set.lua +18 -0
  52. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_process_set.lua +53 -0
  53. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_queues.lua +43 -0
  54. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_sorted_set.lua +24 -0
  55. data/lib/sidekiq_unique_jobs/lua/shared/_hgetall.lua +13 -0
  56. data/lib/sidekiq_unique_jobs/lua/shared/_upgrades.lua +3 -0
  57. data/lib/sidekiq_unique_jobs/lua/unlock.lua +107 -0
  58. data/lib/sidekiq_unique_jobs/lua/update_version.lua +40 -0
  59. data/lib/sidekiq_unique_jobs/lua/upgrade.lua +68 -0
  60. data/lib/sidekiq_unique_jobs/middleware/client.rb +42 -0
  61. data/lib/sidekiq_unique_jobs/middleware/server.rb +31 -0
  62. data/lib/sidekiq_unique_jobs/middleware.rb +29 -43
  63. data/lib/sidekiq_unique_jobs/normalizer.rb +4 -4
  64. data/lib/sidekiq_unique_jobs/on_conflict/log.rb +9 -5
  65. data/lib/sidekiq_unique_jobs/on_conflict/null_strategy.rb +1 -1
  66. data/lib/sidekiq_unique_jobs/on_conflict/raise.rb +1 -1
  67. data/lib/sidekiq_unique_jobs/on_conflict/reject.rb +63 -17
  68. data/lib/sidekiq_unique_jobs/on_conflict/replace.rb +54 -14
  69. data/lib/sidekiq_unique_jobs/on_conflict/reschedule.rb +16 -5
  70. data/lib/sidekiq_unique_jobs/on_conflict/strategy.rb +25 -6
  71. data/lib/sidekiq_unique_jobs/on_conflict.rb +23 -10
  72. data/lib/sidekiq_unique_jobs/options_with_fallback.rb +39 -36
  73. data/lib/sidekiq_unique_jobs/orphans/lua_reaper.rb +29 -0
  74. data/lib/sidekiq_unique_jobs/orphans/manager.rb +241 -0
  75. data/lib/sidekiq_unique_jobs/orphans/null_reaper.rb +24 -0
  76. data/lib/sidekiq_unique_jobs/orphans/observer.rb +42 -0
  77. data/lib/sidekiq_unique_jobs/orphans/reaper.rb +114 -0
  78. data/lib/sidekiq_unique_jobs/orphans/reaper_resurrector.rb +170 -0
  79. data/lib/sidekiq_unique_jobs/orphans/ruby_reaper.rb +298 -0
  80. data/lib/sidekiq_unique_jobs/redis/entity.rb +112 -0
  81. data/lib/sidekiq_unique_jobs/redis/hash.rb +56 -0
  82. data/lib/sidekiq_unique_jobs/redis/list.rb +32 -0
  83. data/lib/sidekiq_unique_jobs/redis/set.rb +32 -0
  84. data/lib/sidekiq_unique_jobs/redis/sorted_set.rb +86 -0
  85. data/lib/sidekiq_unique_jobs/redis/string.rb +51 -0
  86. data/lib/sidekiq_unique_jobs/redis.rb +11 -0
  87. data/lib/sidekiq_unique_jobs/reflectable.rb +26 -0
  88. data/lib/sidekiq_unique_jobs/reflections.rb +79 -0
  89. data/lib/sidekiq_unique_jobs/rspec/matchers/have_valid_sidekiq_options.rb +51 -0
  90. data/lib/sidekiq_unique_jobs/rspec/matchers.rb +26 -0
  91. data/lib/sidekiq_unique_jobs/script/caller.rb +127 -0
  92. data/lib/sidekiq_unique_jobs/script.rb +15 -0
  93. data/lib/sidekiq_unique_jobs/server.rb +61 -0
  94. data/lib/sidekiq_unique_jobs/sidekiq_unique_ext.rb +114 -65
  95. data/lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb +252 -36
  96. data/lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb +47 -32
  97. data/lib/sidekiq_unique_jobs/testing.rb +102 -29
  98. data/lib/sidekiq_unique_jobs/timer_task.rb +299 -0
  99. data/lib/sidekiq_unique_jobs/timing.rb +58 -0
  100. data/lib/sidekiq_unique_jobs/unlockable.rb +20 -4
  101. data/lib/sidekiq_unique_jobs/update_version.rb +25 -0
  102. data/lib/sidekiq_unique_jobs/upgrade_locks.rb +155 -0
  103. data/lib/sidekiq_unique_jobs/version.rb +3 -1
  104. data/lib/sidekiq_unique_jobs/version_check.rb +23 -4
  105. data/lib/sidekiq_unique_jobs/web/helpers.rb +138 -13
  106. data/lib/sidekiq_unique_jobs/web/views/_paging.erb +4 -4
  107. data/lib/sidekiq_unique_jobs/web/views/changelogs.erb +54 -0
  108. data/lib/sidekiq_unique_jobs/web/views/lock.erb +110 -0
  109. data/lib/sidekiq_unique_jobs/web/views/locks.erb +54 -0
  110. data/lib/sidekiq_unique_jobs/web.rb +82 -32
  111. data/lib/sidekiq_unique_jobs.rb +54 -7
  112. data/lib/tasks/changelog.rake +16 -16
  113. metadata +134 -177
  114. data/lib/sidekiq_unique_jobs/client/middleware.rb +0 -56
  115. data/lib/sidekiq_unique_jobs/scripts.rb +0 -118
  116. data/lib/sidekiq_unique_jobs/server/middleware.rb +0 -46
  117. data/lib/sidekiq_unique_jobs/timeout/calculator.rb +0 -63
  118. data/lib/sidekiq_unique_jobs/timeout.rb +0 -8
  119. data/lib/sidekiq_unique_jobs/unique_args.rb +0 -150
  120. data/lib/sidekiq_unique_jobs/util.rb +0 -103
  121. data/lib/sidekiq_unique_jobs/web/views/unique_digest.erb +0 -28
  122. data/lib/sidekiq_unique_jobs/web/views/unique_digests.erb +0 -46
  123. data/redis/acquire_lock.lua +0 -21
  124. data/redis/convert_legacy_lock.lua +0 -13
  125. data/redis/delete.lua +0 -14
  126. data/redis/delete_by_digest.lua +0 -23
  127. data/redis/delete_job_by_digest.lua +0 -60
  128. data/redis/lock.lua +0 -62
  129. data/redis/release_stale_locks.lua +0 -90
  130. data/redis/unlock.lua +0 -35
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ module Redis
5
+ #
6
+ # Class SortedSet provides convenient access to redis sorted sets
7
+ #
8
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
9
+ #
10
+ class SortedSet < Entity
11
+ #
12
+ # Return entries for this sorted set
13
+ #
14
+ # @param [true,false] with_scores true return
15
+ #
16
+ # @return [Array<Object>] when given with_scores: false
17
+ # @return [Hash] when given with_scores: true
18
+ #
19
+ def entries(with_scores: true)
20
+ entrys = redis { |conn| conn.zrange(key, 0, -1, with_scores: with_scores) }
21
+ return entrys unless with_scores
22
+
23
+ entrys.each_with_object({}) { |pair, hash| hash[pair[0]] = pair[1] }
24
+ end
25
+
26
+ #
27
+ # Adds a value to the sorted set
28
+ #
29
+ # @param [Array<Float, String>, String] values the values to add
30
+ #
31
+ # @return [Boolean, Integer] <description>
32
+ #
33
+ def add(values)
34
+ redis do |conn|
35
+ if values.is_a?(Array)
36
+ conn.zadd(key, values)
37
+ else
38
+ conn.zadd(key, now_f, values)
39
+ end
40
+ end
41
+ end
42
+
43
+ #
44
+ # Return the zrak of the member
45
+ #
46
+ # @param [String] member the member to pull rank on
47
+ #
48
+ # @return [Integer]
49
+ #
50
+ def rank(member)
51
+ redis { |conn| conn.zrank(key, member) }
52
+ end
53
+
54
+ #
55
+ # Return score for a member
56
+ #
57
+ # @param [String] member the member for which score
58
+ #
59
+ # @return [Integer, Float]
60
+ #
61
+ def score(member)
62
+ redis { |conn| conn.zscore(key, member) }
63
+ end
64
+
65
+ #
66
+ # Clears the sorted set from all entries
67
+ #
68
+ #
69
+ # @return [Integer] number of entries removed
70
+ #
71
+ def clear
72
+ redis { |conn| conn.zremrangebyrank(key, 0, count) }
73
+ end
74
+
75
+ #
76
+ # Returns the count for this sorted set
77
+ #
78
+ #
79
+ # @return [Integer] the number of entries
80
+ #
81
+ def count
82
+ redis { |conn| conn.zcard(key) }
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ module Redis
5
+ #
6
+ # Class String provides convenient access to redis strings
7
+ #
8
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
9
+ #
10
+ class String < Entity
11
+ #
12
+ # Returns the value of the key
13
+ #
14
+ #
15
+ # @return [String]
16
+ #
17
+ def value
18
+ redis { |conn| conn.get(key) }
19
+ end
20
+
21
+ #
22
+ # Sets the value of the key to given object
23
+ #
24
+ # @param [String] obj the object to update the key with
25
+ #
26
+ # @return [true, false]
27
+ #
28
+ def set(obj, pipeline = nil)
29
+ return pipeline.set(key, obj) if pipeline
30
+
31
+ redis { |conn| conn.set(key, obj) }
32
+ end
33
+
34
+ #
35
+ # Removes the key from redis
36
+ #
37
+ def del(*)
38
+ redis { |conn| conn.del(key) }
39
+ end
40
+
41
+ #
42
+ # Used only for compatibility with other keys
43
+ #
44
+ # @return [1] when key exists
45
+ # @return [0] when key does not exists
46
+ def count
47
+ exist? ? 1 : 0
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ #
5
+ # Augments and enhances redis with object oriented methods
6
+ #
7
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
8
+ #
9
+ module Redis
10
+ end
11
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ #
5
+ # Module Reflectable provides a method to notify subscribers
6
+ #
7
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
8
+ #
9
+ module Reflectable
10
+ #
11
+ # Reflects on specific event
12
+ #
13
+ # @param [Symbol] reflection the reflected event
14
+ # @param [Array] args arguments to provide to reflector
15
+ #
16
+ # @return [void]
17
+ #
18
+ def reflect(reflection, *args)
19
+ SidekiqUniqueJobs.reflections.dispatch(reflection, *args)
20
+ nil
21
+ rescue UniqueJobsError => ex
22
+ SidekiqUniqueJobs.logger.error(ex)
23
+ nil
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ #
5
+ # Class NotificationCollection provides a collection with known notifications
6
+ #
7
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
8
+ #
9
+ class Reflections
10
+ #
11
+ # @return [Array<Symbol>] list of notifications
12
+ REFLECTIONS = [
13
+ :after_unlock_callback_failed,
14
+ :debug,
15
+ :duplicate,
16
+ :error,
17
+ :execution_failed,
18
+ :lock_failed,
19
+ :locked,
20
+ :reschedule_failed,
21
+ :rescheduled,
22
+ :timeout,
23
+ :unknown_sidekiq_worker,
24
+ :unlock_failed,
25
+ :unlocked,
26
+ ].freeze
27
+
28
+ #
29
+ # @return [Hash<Symbol, Array<Symbol, String>>] a hash with deprecated notifications
30
+ DEPRECATIONS = {}.freeze
31
+
32
+ REFLECTIONS.each do |reflection|
33
+ class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
34
+ def #{reflection}(*args, &block) # def unlock_failed(*args, &block)
35
+ raise NoBlockGiven, "block required" unless block_given? # raise NoBlockGiven, "block required" unless block_given?
36
+ @reflections[:#{reflection}] = block # @notifications[:unlock_failed] = block
37
+ end # end
38
+ RUBY
39
+ end
40
+
41
+ def initialize
42
+ @reflections = {}
43
+ end
44
+
45
+ #
46
+ # Dispatch a reflected event
47
+ #
48
+ # @param [reflection] reflection the reflected event
49
+ # @param [Array] args the arguments to provide to the block
50
+ #
51
+ # @return [void] <description>
52
+ #
53
+ def dispatch(reflection, *args)
54
+ if (block = @reflections[reflection])
55
+ block.call(*args)
56
+
57
+ if DEPRECATIONS.key?(reflection)
58
+ replacement, removal_version = DEPRECATIONS[reflection]
59
+ SidekiqUniqueJobs::Deprecation.warn(
60
+ "#{reflection} is deprecated and will be removed in version #{removal_version}." \
61
+ " Use #{replacement} instead.",
62
+ )
63
+ end
64
+ elsif misconfigured?(reflection)
65
+ raise NoSuchNotificationError, reflection
66
+ end
67
+
68
+ nil
69
+ end
70
+
71
+ def configured?(reflection)
72
+ REFLECTIONS.include?(reflection)
73
+ end
74
+
75
+ def misconfigured?(reflection)
76
+ !configured?(reflection)
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ module RSpec
5
+ #
6
+ # Module Matchers provides RSpec matcher for your workers
7
+ #
8
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
9
+ #
10
+ module Matchers
11
+ #
12
+ # Class HaveValidSidekiqOptions validates the unique/lock configuration for a worker.
13
+ #
14
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
15
+ #
16
+ class HaveValidSidekiqOptions
17
+ attr_reader :worker, :lock_config, :sidekiq_options
18
+
19
+ def matches?(worker)
20
+ @worker = worker
21
+ @sidekiq_options = worker.get_sidekiq_options
22
+ @lock_config = SidekiqUniqueJobs.validate_worker(sidekiq_options)
23
+ lock_config.valid?
24
+ end
25
+
26
+ # :nodoc:
27
+ def failure_message
28
+ <<~FAILURE_MESSAGE
29
+ Expected #{worker} to have valid sidekiq options but found the following problems:
30
+ #{lock_config.errors_as_string}
31
+ FAILURE_MESSAGE
32
+ end
33
+
34
+ # :nodoc:
35
+ def description
36
+ "have valid sidekiq options"
37
+ end
38
+ end
39
+
40
+ #
41
+ # RSpec matcher method for validating that a sidekiq worker has valid unique/lock configuration
42
+ #
43
+ #
44
+ # @return [HaveValidSidekiqOptions] an RSpec matcher
45
+ #
46
+ def have_valid_sidekiq_options(*args) # rubocop:disable Naming/PredicateName
47
+ HaveValidSidekiqOptions.new(*args)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ #
5
+ # We all know what the hell RSpec is no?
6
+ #
7
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
8
+ #
9
+ module RSpec
10
+ #
11
+ # Module Matchers provides RSpec matcher for your workers
12
+ #
13
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
14
+ #
15
+ module Matchers
16
+ end
17
+ end
18
+ end
19
+
20
+ if defined?(RSpec)
21
+ require_relative "matchers/have_valid_sidekiq_options"
22
+
23
+ RSpec.configure do |config|
24
+ config.include SidekiqUniqueJobs::RSpec::Matchers
25
+ end
26
+ end
@@ -0,0 +1,127 @@
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
+ # Module Caller provides the convenience method #call_script
10
+ #
11
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
12
+ #
13
+ module Caller
14
+ module_function
15
+
16
+ # includes "SidekiqUniqueJobs::Connection"
17
+ # @!parse include SidekiqUniqueJobs::Connection
18
+ include SidekiqUniqueJobs::Connection
19
+
20
+ #
21
+ # Convenience method to reduce typing,
22
+ # calls redis lua scripts.
23
+ #
24
+ #
25
+ # @overload call_script(file_name, keys, argv, conn)
26
+ # Call script without options hash
27
+ # @param [Symbol] file_name the name of the file
28
+ # @param [Array<String>] keys to pass to the the script
29
+ # @param [Array<String>] argv arguments to pass to the script
30
+ # @param [Redis] conn a redis connection
31
+ # @overload call_script(file_name, conn, keys:, argv:)
32
+ # Call script with options hash
33
+ # @param [Symbol] file_name the name of the file
34
+ # @param [Redis] conn a redis connection
35
+ # @param [Hash] options arguments to pass to the script file
36
+ # @option options [Array] :keys to pass to the script
37
+ # @option options [Array] :argv arguments to pass to the script
38
+ #
39
+ # @return [true,false,String,Integer,Float,nil] returns the return value of the lua script
40
+ #
41
+ def call_script(file_name, *args)
42
+ conn, keys, argv = extract_args(*args)
43
+ return do_call(file_name, conn, keys, argv) if conn
44
+
45
+ pool = defined?(redis_pool) ? redis_pool : nil
46
+
47
+ redis(pool) do |new_conn|
48
+ result = do_call(file_name, new_conn, keys, argv)
49
+ yield result if block_given?
50
+ result
51
+ end
52
+ end
53
+
54
+ # Only used to reduce a little bit of duplication
55
+ # @see call_script
56
+ def do_call(file_name, conn, keys, argv)
57
+ argv = argv.dup.push(
58
+ now_f,
59
+ debug_lua,
60
+ max_history,
61
+ file_name,
62
+ redis_version,
63
+ )
64
+ Script.execute(file_name, conn, keys: keys, argv: argv)
65
+ end
66
+
67
+ #
68
+ # Utility method to allow both symbol keys and arguments
69
+ #
70
+ # @overload call_script(file_name, keys, argv, conn)
71
+ # Call script without options hash
72
+ # @param [Symbol] file_name the name of the file
73
+ # @param [Array<String>] keys to pass to the the script
74
+ # @param [Array<String>] argv arguments to pass to the script
75
+ # @param [Redis] conn a redis connection
76
+ # @overload call_script(file_name, conn, keys:, argv:)
77
+ # Call script with options hash
78
+ # @param [Symbol] file_name the name of the file
79
+ # @param [Redis] conn a redis connection
80
+ # @param [Hash] options arguments to pass to the script file
81
+ # @option options [Array] :keys to pass to the script
82
+ # @option options [Array] :argv arguments to pass to the script
83
+ #
84
+ # @return [Array<Redis, Array, Array>] <description>
85
+ #
86
+ def extract_args(*args)
87
+ options = args.extract_options!
88
+ if options.length.positive?
89
+ [args.pop, options.fetch(:keys) { [] }, options.fetch(:argv) { [] }]
90
+ else
91
+ keys, argv = args.shift(2)
92
+ keys ||= []
93
+ argv ||= []
94
+ [args.pop, keys, argv]
95
+ end
96
+ end
97
+
98
+ #
99
+ # @see SidekiqUniqueJobs#now_f
100
+ #
101
+ def now_f
102
+ SidekiqUniqueJobs.now_f
103
+ end
104
+
105
+ #
106
+ # @see SidekiqUniqueJobs::Config#debug_lua
107
+ #
108
+ def debug_lua
109
+ SidekiqUniqueJobs.config.debug_lua
110
+ end
111
+
112
+ #
113
+ # @see SidekiqUniqueJobs::Config#max_history
114
+ #
115
+ def max_history
116
+ SidekiqUniqueJobs.config.max_history
117
+ end
118
+
119
+ #
120
+ # @see SidekiqUniqueJobs::Config#max_history
121
+ #
122
+ def redis_version
123
+ SidekiqUniqueJobs.config.redis_version
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,15 @@
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
+ include Brpoplpush::RedisScript::DSL
9
+
10
+ configure do |config|
11
+ config.scripts_path = Pathname.new(__FILE__).dirname.join("lua")
12
+ config.logger = Sidekiq.logger # TODO: This becomes a little weird
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,61 @@
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
+ DEATH_HANDLER = (lambda do |job, _ex|
9
+ return unless (digest = job["lock_digest"])
10
+
11
+ SidekiqUniqueJobs::Digests.new.delete_by_digest(digest)
12
+ end).freeze
13
+ #
14
+ # Configure the server middleware
15
+ #
16
+ #
17
+ # @return [Sidekiq] the sidekiq configuration
18
+ #
19
+ def self.configure(config)
20
+ config.on(:startup) { start }
21
+ config.on(:shutdown) { stop }
22
+
23
+ return unless config.respond_to?(:death_handlers)
24
+
25
+ config.death_handlers << death_handler
26
+ end
27
+
28
+ #
29
+ # Start the sidekiq unique jobs server process
30
+ #
31
+ #
32
+ # @return [void]
33
+ #
34
+ def self.start
35
+ SidekiqUniqueJobs::UpdateVersion.call
36
+ SidekiqUniqueJobs::UpgradeLocks.call
37
+ SidekiqUniqueJobs::Orphans::Manager.start
38
+ SidekiqUniqueJobs::Orphans::ReaperResurrector.start
39
+ end
40
+
41
+ #
42
+ # Stop the sidekiq unique jobs server process
43
+ #
44
+ #
45
+ # @return [void]
46
+ #
47
+ def self.stop
48
+ SidekiqUniqueJobs::Orphans::Manager.stop
49
+ end
50
+
51
+ #
52
+ # A death handler for dead jobs
53
+ #
54
+ #
55
+ # @return [lambda]
56
+ #
57
+ def self.death_handler
58
+ DEATH_HANDLER
59
+ end
60
+ end
61
+ end