sidekiq-unique-jobs 6.0.25 → 7.0.0.beta2

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 (113) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +155 -20
  3. data/README.md +349 -112
  4. data/lib/sidekiq-unique-jobs.rb +2 -0
  5. data/lib/sidekiq_unique_jobs.rb +43 -6
  6. data/lib/sidekiq_unique_jobs/batch_delete.rb +121 -0
  7. data/lib/sidekiq_unique_jobs/changelog.rb +71 -0
  8. data/lib/sidekiq_unique_jobs/cli.rb +20 -29
  9. data/lib/sidekiq_unique_jobs/config.rb +193 -0
  10. data/lib/sidekiq_unique_jobs/connection.rb +5 -4
  11. data/lib/sidekiq_unique_jobs/constants.rb +36 -24
  12. data/lib/sidekiq_unique_jobs/core_ext.rb +38 -0
  13. data/lib/sidekiq_unique_jobs/digests.rb +78 -93
  14. data/lib/sidekiq_unique_jobs/exceptions.rb +152 -8
  15. data/lib/sidekiq_unique_jobs/job.rb +3 -3
  16. data/lib/sidekiq_unique_jobs/json.rb +34 -0
  17. data/lib/sidekiq_unique_jobs/key.rb +93 -0
  18. data/lib/sidekiq_unique_jobs/lock.rb +295 -0
  19. data/lib/sidekiq_unique_jobs/lock/base_lock.rb +49 -43
  20. data/lib/sidekiq_unique_jobs/lock/client_validator.rb +28 -0
  21. data/lib/sidekiq_unique_jobs/lock/server_validator.rb +27 -0
  22. data/lib/sidekiq_unique_jobs/lock/until_and_while_executing.rb +8 -17
  23. data/lib/sidekiq_unique_jobs/lock/until_executed.rb +5 -5
  24. data/lib/sidekiq_unique_jobs/lock/until_expired.rb +1 -23
  25. data/lib/sidekiq_unique_jobs/lock/validator.rb +65 -0
  26. data/lib/sidekiq_unique_jobs/lock/while_executing.rb +12 -8
  27. data/lib/sidekiq_unique_jobs/lock/while_executing_reject.rb +1 -1
  28. data/lib/sidekiq_unique_jobs/lock_config.rb +95 -0
  29. data/lib/sidekiq_unique_jobs/lock_info.rb +68 -0
  30. data/lib/sidekiq_unique_jobs/locksmith.rb +255 -99
  31. data/lib/sidekiq_unique_jobs/logging.rb +148 -22
  32. data/lib/sidekiq_unique_jobs/logging/middleware_context.rb +44 -0
  33. data/lib/sidekiq_unique_jobs/lua/delete.lua +51 -0
  34. data/lib/sidekiq_unique_jobs/lua/delete_by_digest.lua +46 -0
  35. data/lib/sidekiq_unique_jobs/lua/delete_job_by_digest.lua +38 -0
  36. data/lib/sidekiq_unique_jobs/lua/find_digest_in_queues.lua +26 -0
  37. data/lib/sidekiq_unique_jobs/lua/find_digest_in_sorted_set.lua +24 -0
  38. data/lib/sidekiq_unique_jobs/lua/lock.lua +91 -0
  39. data/lib/sidekiq_unique_jobs/lua/locked.lua +35 -0
  40. data/lib/sidekiq_unique_jobs/lua/queue.lua +83 -0
  41. data/lib/sidekiq_unique_jobs/lua/reap_orphans.lua +86 -0
  42. data/lib/sidekiq_unique_jobs/lua/shared/_common.lua +40 -0
  43. data/lib/sidekiq_unique_jobs/lua/shared/_current_time.lua +8 -0
  44. data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_queue.lua +19 -0
  45. data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_sorted_set.lua +18 -0
  46. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_queues.lua +46 -0
  47. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_sorted_set.lua +24 -0
  48. data/lib/sidekiq_unique_jobs/lua/shared/_hgetall.lua +13 -0
  49. data/lib/sidekiq_unique_jobs/lua/shared/_upgrades.lua +3 -0
  50. data/lib/sidekiq_unique_jobs/lua/shared/find_digest_in_sorted_set.lua +24 -0
  51. data/lib/sidekiq_unique_jobs/lua/unlock.lua +99 -0
  52. data/lib/sidekiq_unique_jobs/lua/update_version.lua +40 -0
  53. data/lib/sidekiq_unique_jobs/lua/upgrade.lua +68 -0
  54. data/lib/sidekiq_unique_jobs/middleware.rb +62 -31
  55. data/lib/sidekiq_unique_jobs/middleware/client.rb +42 -0
  56. data/lib/sidekiq_unique_jobs/middleware/server.rb +27 -0
  57. data/lib/sidekiq_unique_jobs/normalizer.rb +3 -3
  58. data/lib/sidekiq_unique_jobs/on_conflict.rb +22 -9
  59. data/lib/sidekiq_unique_jobs/on_conflict/log.rb +8 -4
  60. data/lib/sidekiq_unique_jobs/on_conflict/reject.rb +59 -13
  61. data/lib/sidekiq_unique_jobs/on_conflict/replace.rb +42 -13
  62. data/lib/sidekiq_unique_jobs/on_conflict/reschedule.rb +4 -4
  63. data/lib/sidekiq_unique_jobs/on_conflict/strategy.rb +24 -5
  64. data/lib/sidekiq_unique_jobs/options_with_fallback.rb +47 -23
  65. data/lib/sidekiq_unique_jobs/orphans/manager.rb +100 -0
  66. data/lib/sidekiq_unique_jobs/orphans/observer.rb +42 -0
  67. data/lib/sidekiq_unique_jobs/orphans/reaper.rb +201 -0
  68. data/lib/sidekiq_unique_jobs/profiler.rb +51 -0
  69. data/lib/sidekiq_unique_jobs/redis.rb +11 -0
  70. data/lib/sidekiq_unique_jobs/redis/entity.rb +94 -0
  71. data/lib/sidekiq_unique_jobs/redis/hash.rb +56 -0
  72. data/lib/sidekiq_unique_jobs/redis/list.rb +32 -0
  73. data/lib/sidekiq_unique_jobs/redis/set.rb +32 -0
  74. data/lib/sidekiq_unique_jobs/redis/sorted_set.rb +59 -0
  75. data/lib/sidekiq_unique_jobs/redis/string.rb +49 -0
  76. data/lib/sidekiq_unique_jobs/rspec/matchers.rb +19 -0
  77. data/lib/sidekiq_unique_jobs/rspec/matchers/have_valid_sidekiq_options.rb +43 -0
  78. data/lib/sidekiq_unique_jobs/{scripts.rb → script.rb} +43 -29
  79. data/lib/sidekiq_unique_jobs/script/caller.rb +125 -0
  80. data/lib/sidekiq_unique_jobs/script/template.rb +41 -0
  81. data/lib/sidekiq_unique_jobs/sidekiq_unique_ext.rb +92 -65
  82. data/lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb +166 -28
  83. data/lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb +10 -11
  84. data/lib/sidekiq_unique_jobs/testing.rb +47 -15
  85. data/lib/sidekiq_unique_jobs/time_calculator.rb +103 -0
  86. data/lib/sidekiq_unique_jobs/timing.rb +58 -0
  87. data/lib/sidekiq_unique_jobs/unique_args.rb +19 -21
  88. data/lib/sidekiq_unique_jobs/unlockable.rb +11 -2
  89. data/lib/sidekiq_unique_jobs/update_version.rb +25 -0
  90. data/lib/sidekiq_unique_jobs/upgrade_locks.rb +151 -0
  91. data/lib/sidekiq_unique_jobs/version.rb +3 -1
  92. data/lib/sidekiq_unique_jobs/version_check.rb +1 -1
  93. data/lib/sidekiq_unique_jobs/web.rb +25 -19
  94. data/lib/sidekiq_unique_jobs/web/helpers.rb +98 -6
  95. data/lib/sidekiq_unique_jobs/web/views/lock.erb +108 -0
  96. data/lib/sidekiq_unique_jobs/web/views/locks.erb +52 -0
  97. data/lib/tasks/changelog.rake +4 -3
  98. metadata +70 -35
  99. data/lib/sidekiq_unique_jobs/client/middleware.rb +0 -56
  100. data/lib/sidekiq_unique_jobs/server/middleware.rb +0 -46
  101. data/lib/sidekiq_unique_jobs/timeout.rb +0 -8
  102. data/lib/sidekiq_unique_jobs/timeout/calculator.rb +0 -63
  103. data/lib/sidekiq_unique_jobs/util.rb +0 -103
  104. data/lib/sidekiq_unique_jobs/web/views/unique_digest.erb +0 -28
  105. data/lib/sidekiq_unique_jobs/web/views/unique_digests.erb +0 -46
  106. data/redis/acquire_lock.lua +0 -21
  107. data/redis/convert_legacy_lock.lua +0 -13
  108. data/redis/delete.lua +0 -14
  109. data/redis/delete_by_digest.lua +0 -23
  110. data/redis/delete_job_by_digest.lua +0 -60
  111. data/redis/lock.lua +0 -62
  112. data/redis/release_stale_locks.lua +0 -90
  113. data/redis/unlock.lua +0 -35
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ module Redis
5
+ #
6
+ # Class Hash provides convenient access to redis hashes
7
+ #
8
+ # @author Mikael Henriksson <mikael@zoolutions.se>
9
+ #
10
+ class Hash < Entity
11
+ #
12
+ # Return entries for this hash
13
+ #
14
+ # @param [true,false] with_values false return hash
15
+ #
16
+ # @return [Array<Object>] when given with_values: false
17
+ # @return [Hash<String, String>] when given with_values: true
18
+ #
19
+ def entries(with_values: false)
20
+ if with_values
21
+ redis { |conn| conn.hgetall(key) }
22
+ else
23
+ redis { |conn| conn.hkeys(key) }
24
+ end
25
+ end
26
+
27
+ #
28
+ # Removes the key from redis
29
+ #
30
+ def del(*fields)
31
+ redis { |conn| conn.hdel(key, *fields) }
32
+ end
33
+
34
+ #
35
+ # Get a members value
36
+ #
37
+ # @param [String] member the member who's value to get
38
+ #
39
+ # @return [Object] whatever is stored on this hash member
40
+ #
41
+ def [](member)
42
+ redis { |conn| conn.hget(key, member) }
43
+ end
44
+
45
+ #
46
+ # Returns the count for this hash
47
+ #
48
+ #
49
+ # @return [Integer] the length of this hash
50
+ #
51
+ def count
52
+ redis { |conn| conn.hlen(key) }
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ module Redis
5
+ #
6
+ # Class List provides convenient access to redis hashes
7
+ #
8
+ # @author Mikael Henriksson <mikael@zoolutions.se>
9
+ #
10
+ class List < Entity
11
+ #
12
+ # Entries in this list
13
+ #
14
+ #
15
+ # @return [Array<Object>] the elements in this list
16
+ #
17
+ def entries
18
+ redis { |conn| conn.lrange(key, 0, -1) }
19
+ end
20
+
21
+ #
22
+ # The number of entries in this list
23
+ #
24
+ #
25
+ # @return [Integer] the total number of entries
26
+ #
27
+ def count
28
+ redis { |conn| conn.llen(key) }
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ module Redis
5
+ #
6
+ # Class Set provides convenient access to redis sets
7
+ #
8
+ # @author Mikael Henriksson <mikael@zoolutions.se>
9
+ #
10
+ class Set < Entity
11
+ #
12
+ # Return entries for this set
13
+ #
14
+ #
15
+ # @return [Array<String>]
16
+ #
17
+ def entries
18
+ redis { |conn| conn.smembers(key) }
19
+ end
20
+
21
+ #
22
+ # Returns the count for this sorted set
23
+ #
24
+ #
25
+ # @return [Integer] the number of entries
26
+ #
27
+ def count
28
+ redis { |conn| conn.scard(key) }
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,59 @@
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@zoolutions.se>
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
+ # Return the zrak of the member
28
+ #
29
+ # @param [String] member the member to pull rank on
30
+ #
31
+ # @return [Integer]
32
+ #
33
+ def rank(member)
34
+ redis { |conn| conn.zrank(key, member) }
35
+ end
36
+
37
+ #
38
+ # Return score for a member
39
+ #
40
+ # @param [String] member the member for which score
41
+ #
42
+ # @return [Integer, Float]
43
+ #
44
+ def score(member)
45
+ redis { |conn| conn.zscore(key, member) }
46
+ end
47
+
48
+ #
49
+ # Returns the count for this sorted set
50
+ #
51
+ #
52
+ # @return [Integer] the number of entries
53
+ #
54
+ def count
55
+ redis { |conn| conn.zcard(key) }
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,49 @@
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@zoolutions.se>
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)
29
+ redis { |conn| conn.set(key, obj) }
30
+ end
31
+
32
+ #
33
+ # Removes the key from redis
34
+ #
35
+ def del(*)
36
+ redis { |conn| conn.del(key) }
37
+ end
38
+
39
+ #
40
+ # Used only for compatibility with other keys
41
+ #
42
+ # @return [1] when key exists
43
+ # @return [0] when key does not exists
44
+ def count
45
+ exist? ? 1 : 0
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,19 @@
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@zoolutions.se>
9
+ #
10
+ module Matchers
11
+ end
12
+ end
13
+ end
14
+
15
+ require_relative "matchers/have_valid_sidekiq_options"
16
+
17
+ RSpec.configure do |config|
18
+ config.include SidekiqUniqueJobs::RSpec::Matchers
19
+ end
@@ -0,0 +1,43 @@
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@zoolutions.se>
9
+ #
10
+ module Matchers
11
+ #
12
+ # Class BeLockable validates the unique/lock configuration for your worker.
13
+ #
14
+ # @author Mikael Henriksson <mikael@zoolutions.se>
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
+ def failure_message
27
+ <<~FAILURE_MESSAGE
28
+ Expected #{worker} to have valid sidekiq options but found the following problems:
29
+ #{lock_config.errors_as_string}
30
+ FAILURE_MESSAGE
31
+ end
32
+
33
+ def description
34
+ "have valid sidekiq options"
35
+ end
36
+ end
37
+
38
+ def have_valid_sidekiq_options(*args) # rubocop:disable Naming/PredicateName
39
+ HaveValidSidekiqOptions.new(*args)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -1,18 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "pathname"
4
- require "digest/sha1"
5
- require "concurrent/map"
6
-
7
3
  module SidekiqUniqueJobs
8
4
  # Interface to dealing with .lua files
9
5
  #
10
6
  # @author Mikael Henriksson <mikael@zoolutions.se>
11
- module Scripts
12
- LUA_PATHNAME ||= Pathname.new(__FILE__).dirname.join("../../redis").freeze
7
+ module Script
8
+ LUA_PATHNAME ||= Pathname.new(__FILE__).dirname.join("lua").freeze
13
9
  SCRIPT_SHAS ||= Concurrent::Map.new
14
10
 
15
- include SidekiqUniqueJobs::Connection
11
+ extend SidekiqUniqueJobs::Connection
12
+ extend SidekiqUniqueJobs::Logging
13
+ extend SidekiqUniqueJobs::Timing
16
14
 
17
15
  module_function
18
16
 
@@ -23,18 +21,22 @@ module SidekiqUniqueJobs
23
21
  # that wasn't previously loaded.
24
22
  #
25
23
  # @param [Symbol] file_name the name of the lua script
26
- # @param [Sidekiq::RedisConnection, ConnectionPool] redis_pool the redis connection
27
- # @param [Hash] options arguments to pass to the script file
28
- # @option options [Array] :keys the array of keys to pass to the script
29
- # @option options [Array] :argv the array of arguments to pass to the script
24
+ # @param [Array<String>] keys script keys
25
+ # @param [Array<Object>] argv script arguments
26
+ # @param [Redis] conn the redis connection to use
30
27
  #
31
28
  # @return value from script
32
29
  #
33
- def call(file_name, redis_pool, options = {})
34
- execute_script(file_name, redis_pool, options)
35
- rescue Redis::CommandError => ex
36
- handle_error(ex, file_name) do
37
- call(file_name, redis_pool, options)
30
+ def call(file_name, conn, keys: [], argv: [])
31
+ result, elapsed = timed do
32
+ execute_script(file_name, conn, keys, argv)
33
+ end
34
+
35
+ log_debug("Executed #{file_name}.lua in #{elapsed}ms")
36
+ result
37
+ rescue ::Redis::CommandError => ex
38
+ handle_error(ex, file_name, conn) do
39
+ call(file_name, conn, keys: keys, argv: argv)
38
40
  end
39
41
  end
40
42
 
@@ -42,18 +44,18 @@ module SidekiqUniqueJobs
42
44
  # Execute the script file
43
45
  #
44
46
  # @param [Symbol] file_name the name of the lua script
45
- # @param [Sidekiq::RedisConnection, ConnectionPool] redis_pool the redis connection
46
- # @param [Hash] options arguments to pass to the script file
47
- # @option options [Array] :keys the array of keys to pass to the script
48
- # @option options [Array] :argv the array of arguments to pass to the script
47
+ # @param [Redis] conn the redis connection to use
48
+ # @param [Array] keys the array of keys to pass to the script
49
+ # @param [Array] argv the array of arguments to pass to the script
49
50
  #
50
51
  # @return value from script (evalsha)
51
52
  #
52
- def execute_script(file_name, redis_pool, options = {})
53
- redis(redis_pool) do |conn|
54
- sha = script_sha(conn, file_name)
55
- conn.evalsha(sha, options)
56
- end
53
+ def execute_script(file_name, conn, keys, argv)
54
+ conn.evalsha(
55
+ script_sha(conn, file_name),
56
+ keys,
57
+ argv,
58
+ )
57
59
  end
58
60
 
59
61
  #
@@ -80,17 +82,29 @@ module SidekiqUniqueJobs
80
82
  #
81
83
  # @param [Redis::CommandError] ex exception to handle
82
84
  # @param [Symbol] file_name the name of the lua script
85
+ # @param [Redis] conn the redis connection to use
83
86
  #
84
87
  # @return [void]
85
88
  #
86
89
  # @yieldreturn [void] yields back to the caller when NOSCRIPT is raised
87
- def handle_error(ex, file_name)
88
- if ex.message == "NOSCRIPT No matching script. Please use EVAL."
90
+ def handle_error(ex, file_name, conn) # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity
91
+ case ex.message
92
+ when "NOSCRIPT No matching script. Please use EVAL."
89
93
  SCRIPT_SHAS.delete(file_name)
90
94
  return yield if block_given?
95
+ when "BUSY Redis is busy running a script. You can only call SCRIPT KILL or SHUTDOWN NOSAVE."
96
+ begin
97
+ conn.script(:kill)
98
+ return yield if block_given?
99
+ rescue ::Redis::CommandError => ex
100
+ log_warn(ex)
101
+ return yield if block_given?
102
+ end
91
103
  end
92
104
 
93
- raise ScriptError, file_name: file_name, source_exception: ex
105
+ raise unless ScriptError.intercepts?(ex)
106
+
107
+ raise ScriptError.new(ex, script_path(file_name).to_s, script_source(file_name))
94
108
  end
95
109
 
96
110
  #
@@ -101,7 +115,7 @@ module SidekiqUniqueJobs
101
115
  # @return [String] the content of the lua file
102
116
  #
103
117
  def script_source(file_name)
104
- script_path(file_name).read
118
+ Template.new(LUA_PATHNAME).render(script_path(file_name))
105
119
  end
106
120
 
107
121
  #
@@ -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@zoolutions.se>
7
+ module Script
8
+ #
9
+ # Module Caller provides the convenience method #call_script
10
+ #
11
+ # @author Mikael Henriksson <mikael@zoolutions.se>
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.call(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