sidekiq-unique-jobs 6.0.24 → 7.0.4

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sidekiq-unique-jobs might be problematic. Click here for more details.

Files changed (122) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +707 -25
  3. data/README.md +516 -105
  4. data/lib/sidekiq_unique_jobs.rb +48 -7
  5. data/lib/sidekiq_unique_jobs/batch_delete.rb +123 -0
  6. data/lib/sidekiq_unique_jobs/changelog.rb +78 -0
  7. data/lib/sidekiq_unique_jobs/cli.rb +34 -31
  8. data/lib/sidekiq_unique_jobs/config.rb +263 -0
  9. data/lib/sidekiq_unique_jobs/connection.rb +6 -5
  10. data/lib/sidekiq_unique_jobs/constants.rb +46 -24
  11. data/lib/sidekiq_unique_jobs/core_ext.rb +80 -0
  12. data/lib/sidekiq_unique_jobs/digests.rb +71 -100
  13. data/lib/sidekiq_unique_jobs/exceptions.rb +78 -12
  14. data/lib/sidekiq_unique_jobs/job.rb +41 -12
  15. data/lib/sidekiq_unique_jobs/json.rb +40 -0
  16. data/lib/sidekiq_unique_jobs/key.rb +93 -0
  17. data/lib/sidekiq_unique_jobs/lock.rb +325 -0
  18. data/lib/sidekiq_unique_jobs/lock/base_lock.rb +66 -50
  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 +7 -10
  22. data/lib/sidekiq_unique_jobs/lock/until_executed.rb +6 -6
  23. data/lib/sidekiq_unique_jobs/lock/until_executing.rb +1 -1
  24. data/lib/sidekiq_unique_jobs/lock/until_expired.rb +4 -21
  25. data/lib/sidekiq_unique_jobs/lock/validator.rb +96 -0
  26. data/lib/sidekiq_unique_jobs/lock/while_executing.rb +13 -9
  27. data/lib/sidekiq_unique_jobs/lock/while_executing_reject.rb +3 -3
  28. data/lib/sidekiq_unique_jobs/lock_args.rb +123 -0
  29. data/lib/sidekiq_unique_jobs/lock_config.rb +122 -0
  30. data/lib/sidekiq_unique_jobs/lock_digest.rb +79 -0
  31. data/lib/sidekiq_unique_jobs/lock_info.rb +68 -0
  32. data/lib/sidekiq_unique_jobs/lock_timeout.rb +62 -0
  33. data/lib/sidekiq_unique_jobs/lock_ttl.rb +77 -0
  34. data/lib/sidekiq_unique_jobs/locksmith.rb +261 -101
  35. data/lib/sidekiq_unique_jobs/logging.rb +149 -23
  36. data/lib/sidekiq_unique_jobs/logging/middleware_context.rb +44 -0
  37. data/lib/sidekiq_unique_jobs/lua/delete.lua +51 -0
  38. data/lib/sidekiq_unique_jobs/lua/delete_by_digest.lua +42 -0
  39. data/lib/sidekiq_unique_jobs/lua/delete_job_by_digest.lua +38 -0
  40. data/lib/sidekiq_unique_jobs/lua/find_digest_in_queues.lua +26 -0
  41. data/lib/sidekiq_unique_jobs/lua/lock.lua +93 -0
  42. data/lib/sidekiq_unique_jobs/lua/locked.lua +35 -0
  43. data/lib/sidekiq_unique_jobs/lua/queue.lua +87 -0
  44. data/lib/sidekiq_unique_jobs/lua/reap_orphans.lua +94 -0
  45. data/lib/sidekiq_unique_jobs/lua/shared/_common.lua +40 -0
  46. data/lib/sidekiq_unique_jobs/lua/shared/_current_time.lua +8 -0
  47. data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_queue.lua +22 -0
  48. data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_sorted_set.lua +18 -0
  49. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_process_set.lua +53 -0
  50. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_queues.lua +43 -0
  51. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_sorted_set.lua +24 -0
  52. data/lib/sidekiq_unique_jobs/lua/shared/_hgetall.lua +13 -0
  53. data/lib/sidekiq_unique_jobs/lua/shared/_upgrades.lua +3 -0
  54. data/lib/sidekiq_unique_jobs/lua/unlock.lua +95 -0
  55. data/lib/sidekiq_unique_jobs/lua/update_version.lua +40 -0
  56. data/lib/sidekiq_unique_jobs/lua/upgrade.lua +68 -0
  57. data/lib/sidekiq_unique_jobs/middleware.rb +29 -31
  58. data/lib/sidekiq_unique_jobs/middleware/client.rb +42 -0
  59. data/lib/sidekiq_unique_jobs/middleware/server.rb +27 -0
  60. data/lib/sidekiq_unique_jobs/normalizer.rb +4 -4
  61. data/lib/sidekiq_unique_jobs/on_conflict.rb +23 -10
  62. data/lib/sidekiq_unique_jobs/on_conflict/log.rb +9 -5
  63. data/lib/sidekiq_unique_jobs/on_conflict/null_strategy.rb +1 -1
  64. data/lib/sidekiq_unique_jobs/on_conflict/raise.rb +1 -1
  65. data/lib/sidekiq_unique_jobs/on_conflict/reject.rb +61 -15
  66. data/lib/sidekiq_unique_jobs/on_conflict/replace.rb +54 -14
  67. data/lib/sidekiq_unique_jobs/on_conflict/reschedule.rb +12 -5
  68. data/lib/sidekiq_unique_jobs/on_conflict/strategy.rb +25 -6
  69. data/lib/sidekiq_unique_jobs/options_with_fallback.rb +41 -27
  70. data/lib/sidekiq_unique_jobs/orphans/lua_reaper.rb +29 -0
  71. data/lib/sidekiq_unique_jobs/orphans/manager.rb +212 -0
  72. data/lib/sidekiq_unique_jobs/orphans/null_reaper.rb +24 -0
  73. data/lib/sidekiq_unique_jobs/orphans/observer.rb +42 -0
  74. data/lib/sidekiq_unique_jobs/orphans/reaper.rb +114 -0
  75. data/lib/sidekiq_unique_jobs/orphans/ruby_reaper.rb +201 -0
  76. data/lib/sidekiq_unique_jobs/redis.rb +11 -0
  77. data/lib/sidekiq_unique_jobs/redis/entity.rb +106 -0
  78. data/lib/sidekiq_unique_jobs/redis/hash.rb +56 -0
  79. data/lib/sidekiq_unique_jobs/redis/list.rb +32 -0
  80. data/lib/sidekiq_unique_jobs/redis/set.rb +32 -0
  81. data/lib/sidekiq_unique_jobs/redis/sorted_set.rb +86 -0
  82. data/lib/sidekiq_unique_jobs/redis/string.rb +49 -0
  83. data/lib/sidekiq_unique_jobs/rspec/matchers.rb +26 -0
  84. data/lib/sidekiq_unique_jobs/rspec/matchers/have_valid_sidekiq_options.rb +51 -0
  85. data/lib/sidekiq_unique_jobs/script.rb +15 -0
  86. data/lib/sidekiq_unique_jobs/script/caller.rb +125 -0
  87. data/lib/sidekiq_unique_jobs/server.rb +48 -0
  88. data/lib/sidekiq_unique_jobs/sidekiq_unique_ext.rb +92 -65
  89. data/lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb +185 -34
  90. data/lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb +11 -5
  91. data/lib/sidekiq_unique_jobs/testing.rb +62 -21
  92. data/lib/sidekiq_unique_jobs/timer_task.rb +78 -0
  93. data/lib/sidekiq_unique_jobs/timing.rb +58 -0
  94. data/lib/sidekiq_unique_jobs/unlockable.rb +20 -4
  95. data/lib/sidekiq_unique_jobs/update_version.rb +25 -0
  96. data/lib/sidekiq_unique_jobs/upgrade_locks.rb +155 -0
  97. data/lib/sidekiq_unique_jobs/version.rb +3 -1
  98. data/lib/sidekiq_unique_jobs/version_check.rb +23 -4
  99. data/lib/sidekiq_unique_jobs/web.rb +50 -27
  100. data/lib/sidekiq_unique_jobs/web/helpers.rb +125 -10
  101. data/lib/sidekiq_unique_jobs/web/views/changelogs.erb +54 -0
  102. data/lib/sidekiq_unique_jobs/web/views/lock.erb +108 -0
  103. data/lib/sidekiq_unique_jobs/web/views/locks.erb +52 -0
  104. data/lib/tasks/changelog.rake +5 -5
  105. metadata +117 -177
  106. data/lib/sidekiq_unique_jobs/client/middleware.rb +0 -56
  107. data/lib/sidekiq_unique_jobs/scripts.rb +0 -118
  108. data/lib/sidekiq_unique_jobs/server/middleware.rb +0 -46
  109. data/lib/sidekiq_unique_jobs/timeout.rb +0 -8
  110. data/lib/sidekiq_unique_jobs/timeout/calculator.rb +0 -63
  111. data/lib/sidekiq_unique_jobs/unique_args.rb +0 -150
  112. data/lib/sidekiq_unique_jobs/util.rb +0 -103
  113. data/lib/sidekiq_unique_jobs/web/views/unique_digest.erb +0 -28
  114. data/lib/sidekiq_unique_jobs/web/views/unique_digests.erb +0 -46
  115. data/redis/acquire_lock.lua +0 -21
  116. data/redis/convert_legacy_lock.lua +0 -13
  117. data/redis/delete.lua +0 -14
  118. data/redis/delete_by_digest.lua +0 -23
  119. data/redis/delete_job_by_digest.lua +0 -60
  120. data/redis/lock.lua +0 -62
  121. data/redis/release_stale_locks.lua +0 -90
  122. data/redis/unlock.lua +0 -35
@@ -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,125 @@
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
+ do_call(file_name, new_conn, keys, argv)
49
+ end
50
+ end
51
+
52
+ # Only used to reduce a little bit of duplication
53
+ # @see call_script
54
+ def do_call(file_name, conn, keys, argv)
55
+ argv = argv.dup.concat([
56
+ now_f,
57
+ debug_lua,
58
+ max_history,
59
+ file_name,
60
+ redis_version,
61
+ ])
62
+ Script.execute(file_name, conn, keys: keys, argv: argv)
63
+ end
64
+
65
+ #
66
+ # Utility method to allow both symbol keys and arguments
67
+ #
68
+ # @overload call_script(file_name, keys, argv, conn)
69
+ # Call script without options hash
70
+ # @param [Symbol] file_name the name of the file
71
+ # @param [Array<String>] keys to pass to the the script
72
+ # @param [Array<String>] argv arguments to pass to the script
73
+ # @param [Redis] conn a redis connection
74
+ # @overload call_script(file_name, conn, keys:, argv:)
75
+ # Call script with options hash
76
+ # @param [Symbol] file_name the name of the file
77
+ # @param [Redis] conn a redis connection
78
+ # @param [Hash] options arguments to pass to the script file
79
+ # @option options [Array] :keys to pass to the script
80
+ # @option options [Array] :argv arguments to pass to the script
81
+ #
82
+ # @return [Array<Redis, Array, Array>] <description>
83
+ #
84
+ def extract_args(*args)
85
+ options = args.extract_options!
86
+ if options.length.positive?
87
+ [args.pop, options.fetch(:keys) { [] }, options.fetch(:argv) { [] }]
88
+ else
89
+ keys, argv = args.shift(2)
90
+ keys ||= []
91
+ argv ||= []
92
+ [args.pop, keys, argv]
93
+ end
94
+ end
95
+
96
+ #
97
+ # @see SidekiqUniqueJobs#now_f
98
+ #
99
+ def now_f
100
+ SidekiqUniqueJobs.now_f
101
+ end
102
+
103
+ #
104
+ # @see SidekiqUniqueJobs::Config#debug_lua
105
+ #
106
+ def debug_lua
107
+ SidekiqUniqueJobs.config.debug_lua
108
+ end
109
+
110
+ #
111
+ # @see SidekiqUniqueJobs::Config#max_history
112
+ #
113
+ def max_history
114
+ SidekiqUniqueJobs.config.max_history
115
+ end
116
+
117
+ #
118
+ # @see SidekiqUniqueJobs::Config#max_history
119
+ #
120
+ def redis_version
121
+ SidekiqUniqueJobs.config.redis_version
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,48 @@
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
+ def self.start
29
+ SidekiqUniqueJobs::UpdateVersion.call
30
+ SidekiqUniqueJobs::UpgradeLocks.call
31
+ SidekiqUniqueJobs::Orphans::Manager.start
32
+ end
33
+
34
+ def self.stop
35
+ SidekiqUniqueJobs::Orphans::Manager.stop
36
+ end
37
+
38
+ #
39
+ # A death handler for dead jobs
40
+ #
41
+ #
42
+ # @return [lambda]
43
+ #
44
+ def self.death_handler
45
+ DEATH_HANDLER
46
+ end
47
+ end
48
+ end
@@ -2,113 +2,140 @@
2
2
 
3
3
  require "sidekiq/api"
4
4
 
5
+ #
6
+ # @api private
7
+ #
5
8
  module Sidekiq
9
+ # See Sidekiq::Api
6
10
  class SortedEntry
11
+ #
12
+ # Provides extensions for unlocking jobs that are removed and deleted
13
+ #
14
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
15
+ #
7
16
  module UniqueExtension
8
- def self.included(base)
9
- base.class_eval do
10
- alias_method :delete_orig, :delete
11
- alias_method :delete, :delete_ext
12
- alias_method :remove_job_orig, :remove_job
13
- alias_method :remove_job, :remove_job_ext
14
- end
15
- end
16
-
17
- def delete_ext
18
- SidekiqUniqueJobs::Unlockable.unlock(item) if delete_orig
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
19
26
  end
20
27
 
21
28
  private
22
29
 
23
- def remove_job_ext
24
- remove_job_orig do |message|
25
- SidekiqUniqueJobs::Unlockable.unlock(Sidekiq.load_json(message))
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))
26
38
  yield message
27
39
  end
28
40
  end
29
41
  end
30
42
 
31
- include UniqueExtension
43
+ prepend UniqueExtension
32
44
  end
33
45
 
46
+ # See Sidekiq::Api
34
47
  class ScheduledSet
48
+ #
49
+ # Provides extensions for unlocking jobs that are removed and deleted
50
+ #
51
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
52
+ #
35
53
  module UniqueExtension
36
- def self.included(base)
37
- base.class_eval do
38
- alias_method :delete_orig, :delete
39
- alias_method :delete, :delete_ext
40
- end
41
- end
42
-
43
- def delete_ext
44
- SidekiqUniqueJobs::Unlockable.unlock(item) if delete_orig
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
45
65
  end
46
66
  end
47
- include UniqueExtension
67
+
68
+ prepend UniqueExtension
48
69
  end
49
70
 
71
+ # See Sidekiq::Api
50
72
  class Job
73
+ #
74
+ # Provides extensions for unlocking jobs that are removed and deleted
75
+ #
76
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
77
+ #
51
78
  module UniqueExtension
52
- def self.included(base)
53
- base.class_eval do
54
- alias_method :delete_orig, :delete
55
- alias_method :delete, :delete_ext
56
- end
57
- end
58
-
59
- def delete_ext
60
- delete_orig
61
- SidekiqUniqueJobs::Unlockable.unlock(item)
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
62
85
  end
63
86
  end
64
87
 
65
- include UniqueExtension
88
+ prepend UniqueExtension
66
89
  end
67
90
 
91
+ # See Sidekiq::Api
68
92
  class Queue
93
+ #
94
+ # Provides extensions for unlocking jobs that are removed and deleted
95
+ #
96
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
97
+ #
69
98
  module UniqueExtension
70
- def self.included(base)
71
- base.class_eval do
72
- alias_method :clear_orig, :clear
73
- alias_method :clear, :clear_ext
74
- end
75
- end
76
-
77
- def clear_ext
99
+ #
100
+ # Wraps the original method to ensure locks for the job are deleted
101
+ #
102
+ def clear
78
103
  each(&:delete)
79
- clear_orig
104
+ super
80
105
  end
81
106
  end
82
107
 
83
- include UniqueExtension
108
+ prepend UniqueExtension
84
109
  end
85
110
 
111
+ # See Sidekiq::Api
86
112
  class JobSet
113
+ #
114
+ # Provides extensions for unlocking jobs that are removed and deleted
115
+ #
116
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
117
+ #
87
118
  module UniqueExtension
88
- def self.included(base)
89
- base.class_eval do
90
- if base.method_defined?(:clear)
91
- alias_method :clear_orig, :clear
92
- alias_method :clear, :clear_ext
93
- end
94
-
95
- if base.method_defined?(:delete_by_value)
96
- alias_method :delete_by_value_orig, :delete_by_value
97
- alias_method :delete_by_value, :delete_by_value_ext
98
- end
99
- end
100
- end
101
-
102
- def clear_ext
119
+ #
120
+ # Wraps the original method to ensure locks for the job are deleted
121
+ #
122
+ def clear
103
123
  each(&:delete)
104
- clear_orig
124
+ super
105
125
  end
106
126
 
107
- def delete_by_value_ext(name, value)
108
- SidekiqUniqueJobs::Unlockable.unlock(Sidekiq.load_json(value)) if delete_by_value_orig(name, value)
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)
109
136
  end
110
137
  end
111
138
 
112
- include UniqueExtension
139
+ prepend UniqueExtension
113
140
  end
114
141
  end
@@ -3,73 +3,172 @@
3
3
  #
4
4
  # Contains configuration and utility methods that belongs top level
5
5
  #
6
- # @author Mikael Henriksson <mikael@zoolutions.se>
6
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
7
7
  module SidekiqUniqueJobs
8
8
  include SidekiqUniqueJobs::Connection
9
+ extend SidekiqUniqueJobs::JSON
9
10
 
10
11
  module_function
11
12
 
12
- Config = Concurrent::MutableStruct.new(
13
- :default_lock_timeout,
14
- :enabled,
15
- :unique_prefix,
16
- :logger,
17
- )
18
-
13
+ #
19
14
  # The current configuration (See: {.configure} on how to configure)
15
+ #
16
+ #
17
+ # @return [SidekiqUniqueJobs::Config] the gem configuration
18
+ #
20
19
  def config
21
- # Arguments here need to match the definition of the new class (see above)
22
- @config ||= Config.new(
23
- 0,
24
- true,
25
- "uniquejobs",
26
- Sidekiq.logger,
27
- )
20
+ @config ||= reset!
21
+ end
22
+
23
+ #
24
+ # The current strategies
25
+ #
26
+ #
27
+ # @return [Hash<Symbol, SidekiqUniqueJobs::Strategy>] the configured locks
28
+ #
29
+ def strategies
30
+ config.strategies
31
+ end
32
+
33
+ #
34
+ # The current locks
35
+ #
36
+ #
37
+ # @return [Hash<Symbol, SidekiqUniqueJobs::BaseLock>] the configured locks
38
+ #
39
+ def locks
40
+ config.locks
28
41
  end
29
42
 
43
+ #
30
44
  # The current logger
45
+ #
46
+ #
31
47
  # @return [Logger] the configured logger
48
+ #
32
49
  def logger
33
50
  config.logger
34
51
  end
35
52
 
36
- # :reek:ManualDispatch
37
- def with_context(context)
38
- if logger.respond_to?(:with_context)
39
- logger.with_context(context) { yield }
40
- elsif defined?(Sidekiq::Context)
41
- Sidekiq::Context.with(context) { yield }
42
- elsif defined?(Sidekiq::Logging)
43
- Sidekiq::Logging.with_context(context) { yield }
44
- else
45
- yield
46
- end
53
+ #
54
+ # The current gem version
55
+ #
56
+ #
57
+ # @return [String] the current gem version
58
+ #
59
+ def version
60
+ VERSION
47
61
  end
48
62
 
63
+ #
49
64
  # Set a new logger
50
- # @param [Logger] other a new logger
65
+ #
66
+ # @param [Logger] other another logger
67
+ #
68
+ # @return [Logger] the new logger
69
+ #
51
70
  def logger=(other)
52
71
  config.logger = other
53
72
  end
54
73
 
55
- # Change global configuration while yielding
74
+ #
75
+ # Temporarily use another configuration and reset to the old config after yielding
76
+ #
77
+ # @param [Hash] tmp_config the temporary configuration to use
78
+ #
79
+ # @return [void]
80
+ #
56
81
  # @yield control to the caller
57
- def use_config(tmp_config)
82
+ def use_config(tmp_config = {})
58
83
  raise ::ArgumentError, "#{name}.#{__method__} needs a block" unless block_given?
59
84
 
60
85
  old_config = config.to_h
86
+ reset!
61
87
  configure(tmp_config)
62
88
  yield
63
- configure(old_config)
89
+ ensure
90
+ reset!
91
+ configure(old_config.to_h)
92
+ end
93
+
94
+ #
95
+ # Resets configuration to deafult
96
+ #
97
+ #
98
+ # @return [SidekiqUniqueJobs::Config] a default gem configuration
99
+ #
100
+ def reset!
101
+ @config = SidekiqUniqueJobs::Config.default
102
+ end
103
+
104
+ #
105
+ # Enable SidekiqUniuqeJobs either temporarily in a block or for good
106
+ #
107
+ #
108
+ # @return [true] when not given a block
109
+ # @return [true, false] the previous value of enable when given a block
110
+ #
111
+ # @yieldreturn [void] temporarily enable sidekiq unique jobs while executing a block of code
112
+ def enable!(&block)
113
+ toggle(true, &block)
114
+ end
115
+
116
+ #
117
+ # Disable SidekiqUniuqeJobs either temporarily in a block or for good
118
+ #
119
+ #
120
+ # @return [false] when not given a block
121
+ # @return [true, false] the previous value of enable when given a block
122
+ #
123
+ # @yieldreturn [void] temporarily disable sidekiq unique jobs while executing a block of code
124
+ def disable!(&block)
125
+ toggle(false, &block)
126
+ end
127
+
128
+ #
129
+ # Checks if the gem has been disabled
130
+ #
131
+ # @return [true] when config.enabled is true
132
+ # @return [false] when config.enabled is false
133
+ #
134
+ def enabled?
135
+ config.enabled
136
+ end
137
+
138
+ #
139
+ # Checks if the gem has been disabled
140
+ #
141
+ # @return [true] when config.enabled is false
142
+ # @return [false] when config.enabled is true
143
+ #
144
+ def disabled?
145
+ !enabled?
146
+ end
147
+
148
+ #
149
+ # Toggles enabled on or off
150
+ #
151
+ # @api private
152
+ # :nodoc:
153
+ def toggle(enabled)
154
+ if block_given?
155
+ enabled_was = config.enabled
156
+ config.enabled = enabled
157
+ yield
158
+ config.enabled = enabled_was
159
+ else
160
+ config.enabled = enabled
161
+ end
64
162
  end
65
163
 
66
164
  # Configure the gem
67
165
  #
68
166
  # This is usually called once at startup of an application
69
167
  # @param [Hash] options global gem options
70
- # @option options [Integer] :default_lock_timeout (default is 0)
168
+ # @option options [Integer] :lock_timeout (default is 0)
169
+ # @option options [Integer] :lock_ttl (default is 0)
71
170
  # @option options [true,false] :enabled (default is true)
72
- # @option options [String] :unique_prefix (default is 'uniquejobs')
171
+ # @option options [String] :lock_prefix (default is 'uniquejobs')
73
172
  # @option options [Logger] :logger (default is Sidekiq.logger)
74
173
  # @yield control to the caller when given block
75
174
  def configure(options = {})
@@ -82,7 +181,59 @@ module SidekiqUniqueJobs
82
181
  end
83
182
  end
84
183
 
85
- def redis_version
86
- @redis_version ||= redis { |conn| conn.info("server")["redis_version"] }
184
+ #
185
+ # Returns the current redis version
186
+ #
187
+ #
188
+ # @return [String] a string like `5.0.2`
189
+ #
190
+ def fetch_redis_version
191
+ redis { |conn| conn.info("server")["redis_version"] }
192
+ end
193
+
194
+ #
195
+ # Current time as float
196
+ #
197
+ #
198
+ # @return [Float]
199
+ #
200
+ def now_f
201
+ now.to_f
202
+ end
203
+
204
+ #
205
+ # Current time
206
+ #
207
+ #
208
+ # @return [Time]
209
+ #
210
+ def now
211
+ Time.now
212
+ end
213
+
214
+ #
215
+ # Checks that the worker is valid with the given options
216
+ #
217
+ # @param [Hash] options the `sidekiq_options` to validate
218
+ #
219
+ # @return [Boolean]
220
+ #
221
+ def validate_worker(options)
222
+ raise NotUniqueWorker, options unless (lock_type = options[LOCK])
223
+
224
+ lock_class = locks[lock_type]
225
+ lock_class.validate_options(options)
226
+ end
227
+
228
+ #
229
+ # Checks that the worker is valid with the given options
230
+ #
231
+ # @param [Hash] options the `sidekiq_options` to validate
232
+ #
233
+ # @raise [InvalidWorker] when {#validate_worker} returns false or nil
234
+ #
235
+ def validate_worker!(options)
236
+ lock_config = validate_worker(options)
237
+ raise InvalidWorker, lock_config unless lock_config.errors.empty?
87
238
  end
88
239
  end