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.
- checksums.yaml +5 -5
- data/CHANGELOG.md +2163 -25
- data/LICENSE.txt +21 -0
- data/README.md +984 -47
- data/bin/uniquejobs +7 -0
- data/lib/sidekiq-unique-jobs.rb +2 -36
- data/lib/sidekiq_unique_jobs/batch_delete.rb +120 -0
- data/lib/sidekiq_unique_jobs/changelog.rb +68 -0
- data/lib/sidekiq_unique_jobs/cli.rb +95 -0
- data/lib/sidekiq_unique_jobs/config.rb +306 -33
- data/lib/sidekiq_unique_jobs/connection.rb +20 -0
- data/lib/sidekiq_unique_jobs/constants.rb +55 -0
- data/lib/sidekiq_unique_jobs/core_ext.rb +132 -0
- data/lib/sidekiq_unique_jobs/deprecation.rb +65 -0
- data/lib/sidekiq_unique_jobs/digests.rb +134 -0
- data/lib/sidekiq_unique_jobs/exceptions.rb +105 -0
- data/lib/sidekiq_unique_jobs/expiring_digests.rb +14 -0
- data/lib/sidekiq_unique_jobs/job.rb +63 -0
- data/lib/sidekiq_unique_jobs/json.rb +47 -0
- data/lib/sidekiq_unique_jobs/key.rb +98 -0
- data/lib/sidekiq_unique_jobs/lock/base_lock.rb +165 -0
- data/lib/sidekiq_unique_jobs/lock/client_validator.rb +28 -0
- data/lib/sidekiq_unique_jobs/lock/server_validator.rb +27 -0
- data/lib/sidekiq_unique_jobs/lock/until_and_while_executing.rb +71 -0
- data/lib/sidekiq_unique_jobs/lock/until_executed.rb +48 -0
- data/lib/sidekiq_unique_jobs/lock/until_executing.rb +43 -0
- data/lib/sidekiq_unique_jobs/lock/until_expired.rb +42 -0
- data/lib/sidekiq_unique_jobs/lock/validator.rb +96 -0
- data/lib/sidekiq_unique_jobs/lock/while_executing.rb +70 -0
- data/lib/sidekiq_unique_jobs/lock/while_executing_reject.rb +21 -0
- data/lib/sidekiq_unique_jobs/lock.rb +348 -0
- data/lib/sidekiq_unique_jobs/lock_args.rb +127 -0
- data/lib/sidekiq_unique_jobs/lock_config.rb +132 -0
- data/lib/sidekiq_unique_jobs/lock_digest.rb +79 -0
- data/lib/sidekiq_unique_jobs/lock_info.rb +68 -0
- data/lib/sidekiq_unique_jobs/lock_timeout.rb +62 -0
- data/lib/sidekiq_unique_jobs/lock_ttl.rb +77 -0
- data/lib/sidekiq_unique_jobs/lock_type.rb +37 -0
- data/lib/sidekiq_unique_jobs/locksmith.rb +390 -0
- data/lib/sidekiq_unique_jobs/logging/middleware_context.rb +44 -0
- data/lib/sidekiq_unique_jobs/logging.rb +236 -0
- data/lib/sidekiq_unique_jobs/lua/delete.lua +49 -0
- data/lib/sidekiq_unique_jobs/lua/delete_by_digest.lua +39 -0
- data/lib/sidekiq_unique_jobs/lua/delete_job_by_digest.lua +38 -0
- data/lib/sidekiq_unique_jobs/lua/find_digest_in_queues.lua +26 -0
- data/lib/sidekiq_unique_jobs/lua/lock.lua +108 -0
- data/lib/sidekiq_unique_jobs/lua/lock_until_expired.lua +92 -0
- data/lib/sidekiq_unique_jobs/lua/locked.lua +35 -0
- data/lib/sidekiq_unique_jobs/lua/queue.lua +88 -0
- data/lib/sidekiq_unique_jobs/lua/reap_orphans.lua +119 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_common.lua +35 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_current_time.lua +8 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_queue.lua +22 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_sorted_set.lua +29 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_process_set.lua +53 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_queues.lua +43 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_sorted_set.lua +24 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_hgetall.lua +13 -0
- data/lib/sidekiq_unique_jobs/lua/shared/_upgrades.lua +3 -0
- data/lib/sidekiq_unique_jobs/lua/unlock.lua +112 -0
- data/lib/sidekiq_unique_jobs/lua/update_version.lua +40 -0
- data/lib/sidekiq_unique_jobs/lua/upgrade.lua +66 -0
- data/lib/sidekiq_unique_jobs/middleware/client.rb +42 -0
- data/lib/sidekiq_unique_jobs/middleware/server.rb +31 -0
- data/lib/sidekiq_unique_jobs/middleware.rb +41 -15
- data/lib/sidekiq_unique_jobs/normalizer.rb +17 -0
- data/lib/sidekiq_unique_jobs/on_conflict/log.rb +24 -0
- data/lib/sidekiq_unique_jobs/on_conflict/null_strategy.rb +16 -0
- data/lib/sidekiq_unique_jobs/on_conflict/raise.rb +17 -0
- data/lib/sidekiq_unique_jobs/on_conflict/reject.rb +75 -0
- data/lib/sidekiq_unique_jobs/on_conflict/replace.rb +82 -0
- data/lib/sidekiq_unique_jobs/on_conflict/reschedule.rb +39 -0
- data/lib/sidekiq_unique_jobs/on_conflict/strategy.rb +51 -0
- data/lib/sidekiq_unique_jobs/on_conflict.rb +44 -0
- data/lib/sidekiq_unique_jobs/options_with_fallback.rb +78 -0
- data/lib/sidekiq_unique_jobs/orphans/lua_reaper.rb +29 -0
- data/lib/sidekiq_unique_jobs/orphans/manager.rb +242 -0
- data/lib/sidekiq_unique_jobs/orphans/null_reaper.rb +24 -0
- data/lib/sidekiq_unique_jobs/orphans/observer.rb +42 -0
- data/lib/sidekiq_unique_jobs/orphans/reaper.rb +115 -0
- data/lib/sidekiq_unique_jobs/orphans/reaper_resurrector.rb +170 -0
- data/lib/sidekiq_unique_jobs/orphans/ruby_reaper.rb +313 -0
- data/lib/sidekiq_unique_jobs/redis/entity.rb +112 -0
- data/lib/sidekiq_unique_jobs/redis/hash.rb +56 -0
- data/lib/sidekiq_unique_jobs/redis/list.rb +32 -0
- data/lib/sidekiq_unique_jobs/redis/set.rb +32 -0
- data/lib/sidekiq_unique_jobs/redis/sorted_set.rb +102 -0
- data/lib/sidekiq_unique_jobs/redis/string.rb +51 -0
- data/lib/sidekiq_unique_jobs/redis.rb +11 -0
- data/lib/sidekiq_unique_jobs/reflectable.rb +26 -0
- data/lib/sidekiq_unique_jobs/reflections.rb +79 -0
- data/lib/sidekiq_unique_jobs/rspec/matchers/have_valid_sidekiq_options.rb +51 -0
- data/lib/sidekiq_unique_jobs/rspec/matchers.rb +26 -0
- data/lib/sidekiq_unique_jobs/script/caller.rb +133 -0
- data/lib/sidekiq_unique_jobs/script/client.rb +94 -0
- data/lib/sidekiq_unique_jobs/script/config.rb +68 -0
- data/lib/sidekiq_unique_jobs/script/dsl.rb +60 -0
- data/lib/sidekiq_unique_jobs/script/logging.rb +95 -0
- data/lib/sidekiq_unique_jobs/script/lua_error.rb +96 -0
- data/lib/sidekiq_unique_jobs/script/script.rb +75 -0
- data/lib/sidekiq_unique_jobs/script/scripts.rb +123 -0
- data/lib/sidekiq_unique_jobs/script/template.rb +41 -0
- data/lib/sidekiq_unique_jobs/script/timing.rb +35 -0
- data/lib/sidekiq_unique_jobs/script.rb +46 -0
- data/lib/sidekiq_unique_jobs/server.rb +62 -0
- data/lib/sidekiq_unique_jobs/sidekiq_unique_ext.rb +110 -37
- data/lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb +304 -0
- data/lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb +84 -0
- data/lib/sidekiq_unique_jobs/testing.rb +132 -9
- data/lib/sidekiq_unique_jobs/timer_task.rb +299 -0
- data/lib/sidekiq_unique_jobs/timing.rb +58 -0
- data/lib/sidekiq_unique_jobs/unlockable.rb +43 -0
- data/lib/sidekiq_unique_jobs/update_version.rb +25 -0
- data/lib/sidekiq_unique_jobs/upgrade_locks.rb +152 -0
- data/lib/sidekiq_unique_jobs/version.rb +5 -1
- data/lib/sidekiq_unique_jobs/version_check.rb +114 -0
- data/lib/sidekiq_unique_jobs/web/helpers.rb +175 -0
- data/lib/sidekiq_unique_jobs/web/views/_paging.erb +10 -0
- data/lib/sidekiq_unique_jobs/web/views/changelogs.erb +60 -0
- data/lib/sidekiq_unique_jobs/web/views/lock.erb +110 -0
- data/lib/sidekiq_unique_jobs/web/views/locks.erb +59 -0
- data/lib/sidekiq_unique_jobs/web.rb +109 -0
- data/lib/sidekiq_unique_jobs.rb +83 -0
- data/lib/tasks/changelog.rake +23 -0
- metadata +157 -126
- data/.gitignore +0 -10
- data/.rspec +0 -3
- data/.rubocop.yml +0 -36
- data/.travis.yml +0 -25
- data/Appraisals +0 -20
- data/Gemfile +0 -5
- data/LICENSE +0 -22
- data/Rakefile +0 -11
- data/gemfiles/sidekiq_2.15.gemfile +0 -9
- data/gemfiles/sidekiq_2.16.gemfile +0 -9
- data/gemfiles/sidekiq_2.17.gemfile +0 -9
- data/gemfiles/sidekiq_3.0.gemfile +0 -9
- data/gemfiles/sidekiq_develop.gemfile +0 -9
- data/lib/sidekiq_unique_jobs/connectors/redis_pool.rb +0 -11
- data/lib/sidekiq_unique_jobs/connectors/sidekiq_redis.rb +0 -9
- data/lib/sidekiq_unique_jobs/connectors/testing.rb +0 -11
- data/lib/sidekiq_unique_jobs/connectors.rb +0 -16
- data/lib/sidekiq_unique_jobs/middleware/client/strategies/testing_inline.rb +0 -25
- data/lib/sidekiq_unique_jobs/middleware/client/strategies/unique.rb +0 -76
- data/lib/sidekiq_unique_jobs/middleware/client/unique_jobs.rb +0 -39
- data/lib/sidekiq_unique_jobs/middleware/server/unique_jobs.rb +0 -69
- data/lib/sidekiq_unique_jobs/payload_helper.rb +0 -42
- data/sidekiq-unique-jobs.gemspec +0 -27
- data/spec/lib/.sidekiq_testing_enabled_spec.rb.swp +0 -0
- data/spec/lib/client_spec.rb +0 -173
- data/spec/lib/middleware/server/unique_jobs_spec.rb +0 -81
- data/spec/lib/sidekiq_testing_enabled_spec.rb +0 -123
- data/spec/lib/sidekiq_unique_ext_spec.rb +0 -70
- data/spec/lib/unlock_order_spec.rb +0 -64
- data/spec/spec_helper.rb +0 -37
- data/spec/support/my_worker.rb +0 -13
- data/spec/support/sidekiq_meta.rb +0 -17
- data/spec/support/unique_worker.rb +0 -13
@@ -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) # rubocop:disable Metrics/MethodLength
|
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,133 @@
|
|
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(now_f, debug_lua, max_history, file_name, redis_version)
|
58
|
+
|
59
|
+
SidekiqUniqueJobs::Script.execute(file_name, conn, keys: keys, argv: normalize_argv(argv))
|
60
|
+
end
|
61
|
+
|
62
|
+
#
|
63
|
+
# Utility method to allow both symbol keys and arguments
|
64
|
+
#
|
65
|
+
# @overload call_script(file_name, keys, argv, conn)
|
66
|
+
# Call script without options hash
|
67
|
+
# @param [Symbol] file_name the name of the file
|
68
|
+
# @param [Array<String>] keys to pass to the the script
|
69
|
+
# @param [Array<String>] argv arguments to pass to the script
|
70
|
+
# @param [Redis] conn a redis connection
|
71
|
+
# @overload call_script(file_name, conn, keys:, argv:)
|
72
|
+
# Call script with options hash
|
73
|
+
# @param [Symbol] file_name the name of the file
|
74
|
+
# @param [Redis] conn a redis connection
|
75
|
+
# @param [Hash] options arguments to pass to the script file
|
76
|
+
# @option options [Array] :keys to pass to the script
|
77
|
+
# @option options [Array] :argv arguments to pass to the script
|
78
|
+
#
|
79
|
+
# @return [Array<Redis, Array, Array>] <description>
|
80
|
+
#
|
81
|
+
def extract_args(*args)
|
82
|
+
options = args.extract_options!
|
83
|
+
if options.length.positive?
|
84
|
+
[args.pop, options.fetch(:keys) { [] }, options.fetch(:argv) { [] }]
|
85
|
+
else
|
86
|
+
keys, argv = args.shift(2)
|
87
|
+
keys ||= []
|
88
|
+
argv ||= []
|
89
|
+
[args.pop, keys, argv]
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
#
|
94
|
+
# @see SidekiqUniqueJobs#now_f
|
95
|
+
#
|
96
|
+
def now_f
|
97
|
+
SidekiqUniqueJobs.now_f
|
98
|
+
end
|
99
|
+
|
100
|
+
#
|
101
|
+
# @see SidekiqUniqueJobs::Config#debug_lua
|
102
|
+
#
|
103
|
+
def debug_lua
|
104
|
+
SidekiqUniqueJobs.config.debug_lua
|
105
|
+
end
|
106
|
+
|
107
|
+
#
|
108
|
+
# @see SidekiqUniqueJobs::Config#max_history
|
109
|
+
#
|
110
|
+
def max_history
|
111
|
+
SidekiqUniqueJobs.config.max_history
|
112
|
+
end
|
113
|
+
|
114
|
+
#
|
115
|
+
# @see SidekiqUniqueJobs::Config#max_history
|
116
|
+
#
|
117
|
+
def redis_version
|
118
|
+
SidekiqUniqueJobs.config.redis_version
|
119
|
+
end
|
120
|
+
|
121
|
+
def normalize_argv(argv)
|
122
|
+
argv.each_with_index do |item, index|
|
123
|
+
case item
|
124
|
+
when FalseClass, NilClass
|
125
|
+
argv[index] = 0
|
126
|
+
when TrueClass
|
127
|
+
argv[index] = 1
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
@@ -0,0 +1,94 @@
|
|
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 Client
|
9
|
+
include SidekiqUniqueJobs::Script::Timing
|
10
|
+
|
11
|
+
#
|
12
|
+
# @!attribute [r] logger
|
13
|
+
# @return [Logger] an instance of a logger
|
14
|
+
attr_reader :logger
|
15
|
+
#
|
16
|
+
# @!attribute [r] file_name
|
17
|
+
# @return [String] The name of the file to execute
|
18
|
+
attr_reader :config
|
19
|
+
#
|
20
|
+
# @!attribute [r] scripts
|
21
|
+
# @return [Scripts] the collection with loaded scripts
|
22
|
+
attr_reader :scripts
|
23
|
+
|
24
|
+
def initialize(config)
|
25
|
+
@config = config
|
26
|
+
@logger = config.logger
|
27
|
+
@scripts = Scripts.fetch(config.scripts_path)
|
28
|
+
end
|
29
|
+
|
30
|
+
#
|
31
|
+
# Execute a lua script with the provided script_name
|
32
|
+
#
|
33
|
+
# @note this method is recursive if we need to load a lua script
|
34
|
+
# that wasn't previously loaded.
|
35
|
+
#
|
36
|
+
# @param [Symbol] script_name the name of the script to execute
|
37
|
+
# @param [Redis] conn the redis connection to use for execution
|
38
|
+
# @param [Array<String>] keys script keys
|
39
|
+
# @param [Array<Object>] argv script arguments
|
40
|
+
#
|
41
|
+
# @return value from script
|
42
|
+
#
|
43
|
+
def execute(script_name, conn, keys: [], argv: [])
|
44
|
+
result, elapsed = timed do
|
45
|
+
scripts.execute(script_name, conn, keys: keys, argv: argv)
|
46
|
+
end
|
47
|
+
|
48
|
+
logger.debug("Executed #{script_name}.lua in #{elapsed}ms")
|
49
|
+
result
|
50
|
+
rescue ::RedisClient::CommandError => ex
|
51
|
+
handle_error(script_name, conn, ex) do
|
52
|
+
execute(script_name, conn, keys: keys, argv: argv)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
#
|
59
|
+
# Handle errors to allow retrying errors that need retrying
|
60
|
+
#
|
61
|
+
# @param [RedisClient::CommandError] ex exception to handle
|
62
|
+
#
|
63
|
+
# @return [void]
|
64
|
+
#
|
65
|
+
# @yieldreturn [void] yields back to the caller when NOSCRIPT is raised
|
66
|
+
def handle_error(script_name, conn, ex)
|
67
|
+
case ex.message
|
68
|
+
when /NOSCRIPT/
|
69
|
+
handle_noscript(script_name) { return yield }
|
70
|
+
when /BUSY/
|
71
|
+
handle_busy(conn) { return yield }
|
72
|
+
end
|
73
|
+
|
74
|
+
raise unless LuaError.intercepts?(ex)
|
75
|
+
|
76
|
+
script = scripts.fetch(script_name, conn)
|
77
|
+
raise LuaError.new(ex, script)
|
78
|
+
end
|
79
|
+
|
80
|
+
def handle_noscript(script_name)
|
81
|
+
scripts.delete(script_name)
|
82
|
+
yield
|
83
|
+
end
|
84
|
+
|
85
|
+
def handle_busy(conn)
|
86
|
+
scripts.kill(conn)
|
87
|
+
rescue ::RedisClient::CommandError => ex
|
88
|
+
logger.warn(ex)
|
89
|
+
ensure
|
90
|
+
yield
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
module Script
|
5
|
+
#
|
6
|
+
# Class holding gem configuration
|
7
|
+
#
|
8
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
9
|
+
class Config
|
10
|
+
#
|
11
|
+
# @!attribute [r] logger
|
12
|
+
# @return [Logger] a logger to use for debugging
|
13
|
+
attr_reader :logger
|
14
|
+
#
|
15
|
+
# @!attribute [r] scripts_path
|
16
|
+
# @return [Pathname] a directory with lua scripts
|
17
|
+
attr_reader :scripts_path
|
18
|
+
|
19
|
+
#
|
20
|
+
# Initialize a new instance of {Config}
|
21
|
+
#
|
22
|
+
#
|
23
|
+
def initialize
|
24
|
+
@conn = RedisClient.new
|
25
|
+
@logger = Logger.new($stdout)
|
26
|
+
@scripts_path = nil
|
27
|
+
end
|
28
|
+
|
29
|
+
#
|
30
|
+
# Sets a value for scripts_path
|
31
|
+
#
|
32
|
+
# @param [String, Pathname] obj <description>
|
33
|
+
#
|
34
|
+
# @raise [ArgumentError] when directory does not exist
|
35
|
+
# @raise [ArgumentError] when argument isn't supported
|
36
|
+
#
|
37
|
+
# @return [Pathname]
|
38
|
+
#
|
39
|
+
def scripts_path=(obj)
|
40
|
+
raise ArgumentError, "#{obj} should be a Pathname or String" unless obj.is_a?(Pathname) || obj.is_a?(String)
|
41
|
+
raise ArgumentError, "#{obj} does not exist" unless Dir.exist?(obj.to_s)
|
42
|
+
|
43
|
+
@scripts_path =
|
44
|
+
case obj
|
45
|
+
when String
|
46
|
+
Pathname.new(obj)
|
47
|
+
else
|
48
|
+
obj
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
#
|
53
|
+
# Sets a value for logger
|
54
|
+
#
|
55
|
+
# @param [Logger] obj a logger to use
|
56
|
+
#
|
57
|
+
# @raise [ArgumentError] when given argument isn't a Logger
|
58
|
+
#
|
59
|
+
# @return [Logger]
|
60
|
+
#
|
61
|
+
def logger=(obj)
|
62
|
+
raise ArgumentError, "#{obj} should be a Logger" unless obj.is_a?(Logger)
|
63
|
+
|
64
|
+
@logger = obj
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,60 @@
|
|
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
|
+
module DSL
|
9
|
+
MUTEX = Mutex.new
|
10
|
+
|
11
|
+
def self.included(base)
|
12
|
+
base.class_eval do
|
13
|
+
extend ClassMethods
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
#
|
18
|
+
# Module ClassMethods extends the base class with necessary methods
|
19
|
+
#
|
20
|
+
# @author Mikael Henriksson <mikael@zoolutions.se>
|
21
|
+
#
|
22
|
+
module ClassMethods
|
23
|
+
def execute(file_name, conn, keys: [], argv: [])
|
24
|
+
SidekiqUniqueJobs::Script::Client
|
25
|
+
.new(config)
|
26
|
+
.execute(file_name, conn, keys: keys, argv: argv)
|
27
|
+
end
|
28
|
+
|
29
|
+
# Configure the gem
|
30
|
+
#
|
31
|
+
# This is usually called once at startup of an application
|
32
|
+
# @param [Hash] options global gem options
|
33
|
+
# @option options [String, Pathname] :path
|
34
|
+
# @option options [Logger] :logger (default is Logger.new(STDOUT))
|
35
|
+
# @yield control to the caller when given block
|
36
|
+
def configure(options = {})
|
37
|
+
if block_given?
|
38
|
+
yield config
|
39
|
+
else
|
40
|
+
options.each do |key, val|
|
41
|
+
config.send(:"#{key}=", val)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
#
|
47
|
+
# The current configuration (See: {.configure} on how to configure)
|
48
|
+
#
|
49
|
+
#
|
50
|
+
# @return [Script::Config] the gem configuration
|
51
|
+
#
|
52
|
+
def config
|
53
|
+
MUTEX.synchronize do
|
54
|
+
@config ||= Config.new
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
module Script
|
5
|
+
# Utility module for reducing the number of uses of logger.
|
6
|
+
#
|
7
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
8
|
+
module Logging
|
9
|
+
def self.included(base)
|
10
|
+
base.send(:extend, self)
|
11
|
+
end
|
12
|
+
|
13
|
+
#
|
14
|
+
# A convenience method for using the configured gem logger
|
15
|
+
#
|
16
|
+
# @see Script#.logger
|
17
|
+
#
|
18
|
+
# @return [Logger]
|
19
|
+
#
|
20
|
+
def logger
|
21
|
+
SidekiqUniqueJobs::Script.logger
|
22
|
+
end
|
23
|
+
|
24
|
+
#
|
25
|
+
# Logs a message at debug level
|
26
|
+
#
|
27
|
+
# @param [String, Exception] message_or_exception the message or exception to log
|
28
|
+
#
|
29
|
+
# @return [void]
|
30
|
+
#
|
31
|
+
# @yield [String, Exception] the message or exception to use for log message
|
32
|
+
#
|
33
|
+
def log_debug(message_or_exception = nil, &block)
|
34
|
+
logger.debug(message_or_exception, &block)
|
35
|
+
nil
|
36
|
+
end
|
37
|
+
|
38
|
+
#
|
39
|
+
# Logs a message at info level
|
40
|
+
#
|
41
|
+
# @param [String, Exception] message_or_exception the message or exception to log
|
42
|
+
#
|
43
|
+
# @return [void]
|
44
|
+
#
|
45
|
+
# @yield [String, Exception] the message or exception to use for log message
|
46
|
+
#
|
47
|
+
def log_info(message_or_exception = nil, &block)
|
48
|
+
logger.info(message_or_exception, &block)
|
49
|
+
nil
|
50
|
+
end
|
51
|
+
|
52
|
+
#
|
53
|
+
# Logs a message at warn level
|
54
|
+
#
|
55
|
+
# @param [String, Exception] message_or_exception the message or exception to log
|
56
|
+
#
|
57
|
+
# @return [void]
|
58
|
+
#
|
59
|
+
# @yield [String, Exception] the message or exception to use for log message
|
60
|
+
#
|
61
|
+
def log_warn(message_or_exception = nil, &block)
|
62
|
+
logger.warn(message_or_exception, &block)
|
63
|
+
nil
|
64
|
+
end
|
65
|
+
|
66
|
+
#
|
67
|
+
# Logs a message at error level
|
68
|
+
#
|
69
|
+
# @param [String, Exception] message_or_exception the message or exception to log
|
70
|
+
#
|
71
|
+
# @return [void]
|
72
|
+
#
|
73
|
+
# @yield [String, Exception] the message or exception to use for log message
|
74
|
+
#
|
75
|
+
def log_error(message_or_exception = nil, &block)
|
76
|
+
logger.error(message_or_exception, &block)
|
77
|
+
nil
|
78
|
+
end
|
79
|
+
|
80
|
+
#
|
81
|
+
# Logs a message at fatal level
|
82
|
+
#
|
83
|
+
# @param [String, Exception] message_or_exception the message or exception to log
|
84
|
+
#
|
85
|
+
# @return [void]
|
86
|
+
#
|
87
|
+
# @yield [String, Exception] the message or exception to use for log message
|
88
|
+
#
|
89
|
+
def log_fatal(message_or_exception = nil, &block)
|
90
|
+
logger.fatal(message_or_exception, &block)
|
91
|
+
nil
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|