sidekiq-unique-jobs 8.0.3 → 8.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +148 -1
- data/README.md +2 -4
- data/lib/sidekiq_unique_jobs/batch_delete.rb +1 -1
- data/lib/sidekiq_unique_jobs/changelog.rb +1 -1
- data/lib/sidekiq_unique_jobs/config.rb +4 -4
- data/lib/sidekiq_unique_jobs/constants.rb +1 -0
- data/lib/sidekiq_unique_jobs/digests.rb +46 -13
- data/lib/sidekiq_unique_jobs/job.rb +4 -4
- data/lib/sidekiq_unique_jobs/lock/base_lock.rb +1 -1
- data/lib/sidekiq_unique_jobs/lock/while_executing.rb +3 -0
- data/lib/sidekiq_unique_jobs/lock.rb +14 -11
- data/lib/sidekiq_unique_jobs/lock_config.rb +10 -4
- data/lib/sidekiq_unique_jobs/locksmith.rb +3 -3
- data/lib/sidekiq_unique_jobs/lua/unlock.lua +16 -9
- data/lib/sidekiq_unique_jobs/middleware/client.rb +1 -1
- data/lib/sidekiq_unique_jobs/middleware/server.rb +1 -1
- data/lib/sidekiq_unique_jobs/middleware.rb +5 -5
- data/lib/sidekiq_unique_jobs/on_conflict/replace.rb +1 -1
- data/lib/sidekiq_unique_jobs/on_conflict/reschedule.rb +5 -1
- data/lib/sidekiq_unique_jobs/orphans/manager.rb +8 -7
- data/lib/sidekiq_unique_jobs/orphans/reaper.rb +2 -1
- data/lib/sidekiq_unique_jobs/orphans/ruby_reaper.rb +26 -7
- data/lib/sidekiq_unique_jobs/redis/sorted_set.rb +12 -3
- data/lib/sidekiq_unique_jobs/script/caller.rb +1 -1
- 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 +32 -1
- data/lib/sidekiq_unique_jobs/server.rb +2 -0
- data/lib/sidekiq_unique_jobs/sidekiq_unique_ext.rb +13 -35
- data/lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb +4 -4
- data/lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb +3 -3
- data/lib/sidekiq_unique_jobs/testing.rb +4 -4
- data/lib/sidekiq_unique_jobs/version.rb +1 -1
- data/lib/sidekiq_unique_jobs/web/helpers.rb +3 -3
- data/lib/sidekiq_unique_jobs/web/views/changelogs.erb +1 -1
- data/lib/sidekiq_unique_jobs/web/views/lock.erb +2 -2
- data/lib/sidekiq_unique_jobs/web/views/locks.erb +1 -1
- data/lib/sidekiq_unique_jobs/web.rb +18 -17
- data/lib/sidekiq_unique_jobs.rb +5 -4
- metadata +12 -23
@@ -22,11 +22,12 @@ module SidekiqUniqueJobs
|
|
22
22
|
#
|
23
23
|
# @return [Hash<Symbol, SidekiqUniqueJobs::Orphans::Reaper] the current implementation of reapers
|
24
24
|
REAPERS = {
|
25
|
-
lua: SidekiqUniqueJobs::Orphans::
|
25
|
+
lua: SidekiqUniqueJobs::Orphans::RubyReaper,
|
26
26
|
ruby: SidekiqUniqueJobs::Orphans::RubyReaper,
|
27
27
|
none: SidekiqUniqueJobs::Orphans::NullReaper,
|
28
28
|
nil => SidekiqUniqueJobs::Orphans::NullReaper,
|
29
29
|
false => SidekiqUniqueJobs::Orphans::NullReaper,
|
30
|
+
true => SidekiqUniqueJobs::Orphans::RubyReaper,
|
30
31
|
}.freeze
|
31
32
|
|
32
33
|
#
|
@@ -13,6 +13,9 @@ module SidekiqUniqueJobs
|
|
13
13
|
class RubyReaper < Reaper
|
14
14
|
include SidekiqUniqueJobs::Timing
|
15
15
|
|
16
|
+
#
|
17
|
+
# @return [Integer] a best guess of Sidekiq::Launcher::BEAT_PAUSE
|
18
|
+
SIDEKIQ_BEAT_PAUSE = 10
|
16
19
|
#
|
17
20
|
# @return [String] the suffix for :RUN locks
|
18
21
|
RUN_SUFFIX = ":RUN"
|
@@ -74,12 +77,28 @@ module SidekiqUniqueJobs
|
|
74
77
|
|
75
78
|
BatchDelete.call(expired_digests, conn)
|
76
79
|
BatchDelete.call(orphans, conn)
|
80
|
+
|
81
|
+
# orphans.each_slice(500) do |chunk|
|
82
|
+
# conn.pipelined do |pipeline|
|
83
|
+
# chunk.each do |digest|
|
84
|
+
# next if belongs_to_job?(digest)
|
85
|
+
|
86
|
+
# pipeline.zadd(ORPHANED_DIGESTS, now_f, digest)
|
87
|
+
# end
|
88
|
+
# end
|
89
|
+
# end
|
77
90
|
end
|
78
91
|
|
79
92
|
def expired_digests
|
80
|
-
|
93
|
+
conn.zrange(EXPIRING_DIGESTS, 0, max_score, "byscore")
|
94
|
+
end
|
95
|
+
|
96
|
+
def orphaned_digests
|
97
|
+
conn.zrange(ORPHANED_DIGESTS, 0, max_score, "byscore")
|
98
|
+
end
|
81
99
|
|
82
|
-
|
100
|
+
def max_score
|
101
|
+
(start_time - reaper_timeout - SIDEKIQ_BEAT_PAUSE).to_f
|
83
102
|
end
|
84
103
|
|
85
104
|
#
|
@@ -89,10 +108,10 @@ module SidekiqUniqueJobs
|
|
89
108
|
# @return [Array<String>] an array of orphaned digests
|
90
109
|
#
|
91
110
|
def orphans # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity
|
92
|
-
page = 0
|
93
|
-
per = reaper_count * 2
|
94
111
|
orphans = []
|
95
|
-
|
112
|
+
page = 0
|
113
|
+
per = reaper_count * 2
|
114
|
+
results = digests.byscore(0, max_score, offset: page * per, count: (page + 1) * per)
|
96
115
|
|
97
116
|
while results.size.positive?
|
98
117
|
results.each do |digest|
|
@@ -107,7 +126,7 @@ module SidekiqUniqueJobs
|
|
107
126
|
break if orphans.size >= reaper_count
|
108
127
|
|
109
128
|
page += 1
|
110
|
-
results =
|
129
|
+
results = digests.byscore(0, max_score, offset: page * per, count: (page + 1) * per)
|
111
130
|
end
|
112
131
|
|
113
132
|
orphans
|
@@ -218,7 +237,7 @@ module SidekiqUniqueJobs
|
|
218
237
|
end
|
219
238
|
|
220
239
|
def considered_active?(time_f)
|
221
|
-
|
240
|
+
max_score < time_f
|
222
241
|
end
|
223
242
|
|
224
243
|
#
|
@@ -24,10 +24,11 @@ module SidekiqUniqueJobs
|
|
24
24
|
# @return [Hash] when given with_scores: true
|
25
25
|
#
|
26
26
|
def entries(with_scores: true)
|
27
|
-
|
28
|
-
return entrys unless with_scores
|
27
|
+
return redis { |conn| conn.zrange(key, 0, -1) } unless with_scores
|
29
28
|
|
30
|
-
|
29
|
+
redis { |conn| conn.zrange(key, 0, -1, "withscores") }.each_with_object({}) do |pair, hash|
|
30
|
+
hash[pair[0]] = pair[1]
|
31
|
+
end
|
31
32
|
end
|
32
33
|
|
33
34
|
#
|
@@ -47,6 +48,14 @@ module SidekiqUniqueJobs
|
|
47
48
|
end
|
48
49
|
end
|
49
50
|
|
51
|
+
def byscore(min, max, offset: nil, count: nil)
|
52
|
+
redis do |conn|
|
53
|
+
return conn.zrange(key, min, max, "byscore") unless offset && count
|
54
|
+
|
55
|
+
conn.zrange(key, min, max, "byscore", "limit", offset, count)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
50
59
|
#
|
51
60
|
# Return the zrak of the member
|
52
61
|
#
|
@@ -56,7 +56,7 @@ module SidekiqUniqueJobs
|
|
56
56
|
def do_call(file_name, conn, keys, argv)
|
57
57
|
argv = argv.dup.push(now_f, debug_lua, max_history, file_name, redis_version)
|
58
58
|
|
59
|
-
Script.execute(file_name, conn, keys: keys, argv: normalize_argv(argv))
|
59
|
+
SidekiqUniqueJobs::Script.execute(file_name, conn, keys: keys, argv: normalize_argv(argv))
|
60
60
|
end
|
61
61
|
|
62
62
|
#
|
@@ -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
|
@@ -0,0 +1,96 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
module Script
|
5
|
+
#
|
6
|
+
# Misconfiguration is raised when gem is misconfigured
|
7
|
+
#
|
8
|
+
# @author Mikael Henriksson <mikael@zoolutions.se>
|
9
|
+
#
|
10
|
+
class Misconfiguration < RuntimeError
|
11
|
+
end
|
12
|
+
|
13
|
+
# LuaError raised on errors in Lua scripts
|
14
|
+
#
|
15
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
16
|
+
class LuaError < RuntimeError
|
17
|
+
# Reformats errors raised by redis representing failures while executing
|
18
|
+
# a lua script. The default errors have confusing messages and backtraces,
|
19
|
+
# and a type of +RuntimeError+. This class improves the message and
|
20
|
+
# modifies the backtrace to include the lua script itself in a reasonable
|
21
|
+
# way.
|
22
|
+
|
23
|
+
PATTERN = /ERR Error (compiling|running) script \(.*?\): .*?:(\d+): (.*)/.freeze
|
24
|
+
LIB_PATH = File.expand_path("..", __dir__).freeze
|
25
|
+
CONTEXT_LINE_NUMBER = 2
|
26
|
+
|
27
|
+
attr_reader :error, :file, :content
|
28
|
+
|
29
|
+
# Is this error one that should be reformatted?
|
30
|
+
#
|
31
|
+
# @param error [StandardError] the original error raised by redis
|
32
|
+
# @return [Boolean] is this an error that should be reformatted?
|
33
|
+
def self.intercepts?(error)
|
34
|
+
PATTERN.match?(error.message)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Initialize a new {LuaError} from an existing redis error, adjusting
|
38
|
+
# the message and backtrace in the process.
|
39
|
+
#
|
40
|
+
# @param error [StandardError] the original error raised by redis
|
41
|
+
# @param script [Script] a DTO with information about the script
|
42
|
+
#
|
43
|
+
def initialize(error, script)
|
44
|
+
@error = error
|
45
|
+
@file = script.path
|
46
|
+
@content = script.source
|
47
|
+
@backtrace = @error.backtrace
|
48
|
+
|
49
|
+
@error.message.match(PATTERN) do |regexp_match|
|
50
|
+
line_number = regexp_match[2].to_i
|
51
|
+
message = regexp_match[3]
|
52
|
+
error_context = generate_error_context(content, line_number)
|
53
|
+
|
54
|
+
super("#{message}\n\n#{error_context}\n\n")
|
55
|
+
set_backtrace(generate_backtrace(file, line_number))
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
# :nocov:
|
62
|
+
def generate_error_context(content, line_number)
|
63
|
+
lines = content.lines.to_a
|
64
|
+
beginning_line_number = [1, line_number - CONTEXT_LINE_NUMBER].max
|
65
|
+
ending_line_number = [lines.count, line_number + CONTEXT_LINE_NUMBER].min
|
66
|
+
line_number_width = ending_line_number.to_s.length
|
67
|
+
|
68
|
+
(beginning_line_number..ending_line_number).map do |number|
|
69
|
+
indicator = (number == line_number) ? "=>" : " "
|
70
|
+
formatted_number = format("%#{line_number_width}d", number)
|
71
|
+
" #{indicator} #{formatted_number}: #{lines[number - 1]}"
|
72
|
+
end.join.chomp
|
73
|
+
end
|
74
|
+
|
75
|
+
# :nocov:
|
76
|
+
def generate_backtrace(file, line_number)
|
77
|
+
pre_gem = backtrace_before_entering_gem(@backtrace)
|
78
|
+
index_of_first_gem_line = (@backtrace.size - pre_gem.size - 1)
|
79
|
+
|
80
|
+
pre_gem.unshift(@backtrace[index_of_first_gem_line])
|
81
|
+
pre_gem.unshift("#{file}:#{line_number}")
|
82
|
+
pre_gem
|
83
|
+
end
|
84
|
+
|
85
|
+
# :nocov:
|
86
|
+
def backtrace_before_entering_gem(backtrace)
|
87
|
+
backtrace.reverse.take_while { |line| !line_from_gem(line) }.reverse
|
88
|
+
end
|
89
|
+
|
90
|
+
# :nocov:
|
91
|
+
def line_from_gem(line)
|
92
|
+
line.split(":").first.include?(LIB_PATH)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SidekiqUniqueJobs
|
4
|
+
module Script
|
5
|
+
# Interface to dealing with .lua files
|
6
|
+
#
|
7
|
+
# @author Mikael Henriksson <mikael@mhenrixon.com>
|
8
|
+
class Script
|
9
|
+
def self.load(name, root_path, conn)
|
10
|
+
script = new(name: name, root_path: root_path)
|
11
|
+
script.load(conn)
|
12
|
+
end
|
13
|
+
|
14
|
+
#
|
15
|
+
# @!attribute [r] script_name
|
16
|
+
# @return [Symbol, String] the name of the script without extension
|
17
|
+
attr_reader :name
|
18
|
+
#
|
19
|
+
# @!attribute [r] script_path
|
20
|
+
# @return [String] the path to the script on disk
|
21
|
+
attr_reader :path
|
22
|
+
#
|
23
|
+
# @!attribute [r] root_path
|
24
|
+
# @return [Pathname]
|
25
|
+
attr_reader :root_path
|
26
|
+
#
|
27
|
+
# @!attribute [r] source
|
28
|
+
# @return [String] the source code of the lua script
|
29
|
+
attr_reader :source
|
30
|
+
#
|
31
|
+
# @!attribute [rw] sha
|
32
|
+
# @return [String] the sha of the script
|
33
|
+
attr_reader :sha
|
34
|
+
#
|
35
|
+
# @!attribute [rw] call_count
|
36
|
+
# @return [Integer] the number of times the script was called/executed
|
37
|
+
attr_reader :call_count
|
38
|
+
|
39
|
+
def initialize(name:, root_path:)
|
40
|
+
@name = name
|
41
|
+
@root_path = root_path
|
42
|
+
@path = root_path.join("#{name}.lua").to_s
|
43
|
+
@source = render_file
|
44
|
+
@sha = compiled_sha
|
45
|
+
@call_count = 0
|
46
|
+
end
|
47
|
+
|
48
|
+
def ==(other)
|
49
|
+
sha == compiled_sha && compiled_sha == other.sha
|
50
|
+
end
|
51
|
+
|
52
|
+
def increment_call_count
|
53
|
+
@call_count += 1
|
54
|
+
end
|
55
|
+
|
56
|
+
def changed?
|
57
|
+
compiled_sha != sha
|
58
|
+
end
|
59
|
+
|
60
|
+
def render_file
|
61
|
+
Template.new(root_path).render(path)
|
62
|
+
end
|
63
|
+
|
64
|
+
def compiled_sha
|
65
|
+
Digest::SHA1.hexdigest(source)
|
66
|
+
end
|
67
|
+
|
68
|
+
def load(conn)
|
69
|
+
@sha = conn.script(:load, source)
|
70
|
+
|
71
|
+
self
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|