canvas_sync 0.21.1 → 0.22.0.beta2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/canvas_sync/concerns/auto_relations.rb +11 -0
- data/lib/canvas_sync/config.rb +3 -5
- data/lib/canvas_sync/generators/templates/models/rubric.rb +2 -1
- data/lib/canvas_sync/job_batches/batch.rb +432 -402
- data/lib/canvas_sync/job_batches/callback.rb +100 -114
- data/lib/canvas_sync/job_batches/chain_builder.rb +194 -196
- data/lib/canvas_sync/job_batches/{active_job.rb → compat/active_job.rb} +2 -2
- data/lib/canvas_sync/job_batches/{sidekiq → compat/sidekiq}/web/helpers.rb +1 -1
- data/lib/canvas_sync/job_batches/{sidekiq → compat/sidekiq}/web.rb +3 -3
- data/lib/canvas_sync/job_batches/{sidekiq.rb → compat/sidekiq.rb} +35 -22
- data/lib/canvas_sync/job_batches/compat.rb +20 -0
- data/lib/canvas_sync/job_batches/context_hash.rb +124 -126
- data/lib/canvas_sync/job_batches/jobs/base_job.rb +2 -4
- data/lib/canvas_sync/job_batches/jobs/concurrent_batch_job.rb +14 -16
- data/lib/canvas_sync/job_batches/jobs/managed_batch_job.rb +125 -127
- data/lib/canvas_sync/job_batches/jobs/serial_batch_job.rb +14 -16
- data/lib/canvas_sync/job_batches/pool.rb +193 -195
- data/lib/canvas_sync/job_batches/redis_model.rb +50 -52
- data/lib/canvas_sync/job_batches/redis_script.rb +129 -131
- data/lib/canvas_sync/job_batches/status.rb +85 -87
- data/lib/canvas_sync/job_uniqueness/compat/active_job.rb +75 -0
- data/lib/canvas_sync/job_uniqueness/compat/sidekiq.rb +135 -0
- data/lib/canvas_sync/job_uniqueness/compat.rb +20 -0
- data/lib/canvas_sync/job_uniqueness/configuration.rb +25 -0
- data/lib/canvas_sync/job_uniqueness/job_uniqueness.rb +47 -0
- data/lib/canvas_sync/job_uniqueness/lock_context.rb +171 -0
- data/lib/canvas_sync/job_uniqueness/locksmith.rb +92 -0
- data/lib/canvas_sync/job_uniqueness/on_conflict/base.rb +32 -0
- data/lib/canvas_sync/job_uniqueness/on_conflict/log.rb +13 -0
- data/lib/canvas_sync/job_uniqueness/on_conflict/null_strategy.rb +9 -0
- data/lib/canvas_sync/job_uniqueness/on_conflict/raise.rb +11 -0
- data/lib/canvas_sync/job_uniqueness/on_conflict/reject.rb +21 -0
- data/lib/canvas_sync/job_uniqueness/on_conflict/reschedule.rb +20 -0
- data/lib/canvas_sync/job_uniqueness/on_conflict.rb +62 -0
- data/lib/canvas_sync/job_uniqueness/strategy/base.rb +104 -0
- data/lib/canvas_sync/job_uniqueness/strategy/until_and_while_executing.rb +35 -0
- data/lib/canvas_sync/job_uniqueness/strategy/until_executed.rb +20 -0
- data/lib/canvas_sync/job_uniqueness/strategy/until_executing.rb +20 -0
- data/lib/canvas_sync/job_uniqueness/strategy/until_expired.rb +16 -0
- data/lib/canvas_sync/job_uniqueness/strategy/while_executing.rb +26 -0
- data/lib/canvas_sync/job_uniqueness/strategy.rb +27 -0
- data/lib/canvas_sync/job_uniqueness/unique_job_common.rb +79 -0
- data/lib/canvas_sync/misc_helper.rb +1 -1
- data/lib/canvas_sync/version.rb +1 -1
- data/lib/canvas_sync.rb +4 -3
- data/spec/dummy/app/models/rubric.rb +2 -1
- data/spec/dummy/config/environments/test.rb +1 -1
- data/spec/job_batching/batch_spec.rb +49 -7
- data/spec/job_batching/{active_job_spec.rb → compat/active_job_spec.rb} +2 -2
- data/spec/job_batching/{sidekiq_spec.rb → compat/sidekiq_spec.rb} +14 -12
- data/spec/job_batching/flow_spec.rb +1 -1
- data/spec/job_batching/integration_helper.rb +1 -1
- data/spec/job_batching/status_spec.rb +2 -2
- data/spec/job_uniqueness/compat/active_job_spec.rb +49 -0
- data/spec/job_uniqueness/compat/sidekiq_spec.rb +68 -0
- data/spec/job_uniqueness/lock_context_spec.rb +95 -0
- data/spec/job_uniqueness/on_conflict/log_spec.rb +11 -0
- data/spec/job_uniqueness/on_conflict/raise_spec.rb +10 -0
- data/spec/job_uniqueness/on_conflict/reschedule_spec.rb +24 -0
- data/spec/job_uniqueness/on_conflict_spec.rb +16 -0
- data/spec/job_uniqueness/spec_helper.rb +14 -0
- data/spec/job_uniqueness/strategy/base_spec.rb +100 -0
- data/spec/job_uniqueness/strategy/until_and_while_executing_spec.rb +48 -0
- data/spec/job_uniqueness/strategy/until_executed_spec.rb +23 -0
- data/spec/job_uniqueness/strategy/until_executing_spec.rb +23 -0
- data/spec/job_uniqueness/strategy/until_expired_spec.rb +23 -0
- data/spec/job_uniqueness/strategy/while_executing_spec.rb +33 -0
- data/spec/job_uniqueness/support/lock_strategy.rb +28 -0
- data/spec/job_uniqueness/support/on_conflict.rb +24 -0
- data/spec/job_uniqueness/support/test_worker.rb +19 -0
- data/spec/job_uniqueness/unique_job_common_spec.rb +45 -0
- data/spec/spec_helper.rb +1 -1
- metadata +278 -204
- /data/lib/canvas_sync/job_batches/{sidekiq → compat/sidekiq}/web/batches_assets/css/styles.less +0 -0
- /data/lib/canvas_sync/job_batches/{sidekiq → compat/sidekiq}/web/batches_assets/js/batch_tree.js +0 -0
- /data/lib/canvas_sync/job_batches/{sidekiq → compat/sidekiq}/web/batches_assets/js/util.js +0 -0
- /data/lib/canvas_sync/job_batches/{sidekiq → compat/sidekiq}/web/views/_batch_tree.erb +0 -0
- /data/lib/canvas_sync/job_batches/{sidekiq → compat/sidekiq}/web/views/_batches_table.erb +0 -0
- /data/lib/canvas_sync/job_batches/{sidekiq → compat/sidekiq}/web/views/_common.erb +0 -0
- /data/lib/canvas_sync/job_batches/{sidekiq → compat/sidekiq}/web/views/_jobs_table.erb +0 -0
- /data/lib/canvas_sync/job_batches/{sidekiq → compat/sidekiq}/web/views/_pagination.erb +0 -0
- /data/lib/canvas_sync/job_batches/{sidekiq → compat/sidekiq}/web/views/batch.erb +0 -0
- /data/lib/canvas_sync/job_batches/{sidekiq → compat/sidekiq}/web/views/batches.erb +0 -0
- /data/lib/canvas_sync/job_batches/{sidekiq → compat/sidekiq}/web/views/pool.erb +0 -0
- /data/lib/canvas_sync/job_batches/{sidekiq → compat/sidekiq}/web/views/pools.erb +0 -0
@@ -4,159 +4,157 @@ require 'erb'
|
|
4
4
|
|
5
5
|
# Modified from https://github.com/Shopify/wolverine/blob/master/lib/wolverine/script.rb
|
6
6
|
|
7
|
-
module CanvasSync
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
end
|
7
|
+
module CanvasSync::JobBatches
|
8
|
+
# {RedisScript} represents a lua script in the filesystem. It loads the script
|
9
|
+
# from disk and handles talking to redis to execute it. Error handling
|
10
|
+
# is handled by {LuaError}.
|
11
|
+
class RedisScript
|
12
|
+
|
13
|
+
# Loads the script file from disk and calculates its +SHA1+ sum.
|
14
|
+
#
|
15
|
+
# @param file [Pathname] the full path to the indicated file
|
16
|
+
def initialize(file)
|
17
|
+
@file = Pathname.new(file)
|
18
|
+
end
|
20
19
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
rescue => e
|
37
|
-
e.message =~ /NOSCRIPT/ ? redis.eval(content, *args) : raise
|
38
|
-
end
|
20
|
+
# Passes the script and supplied arguments to redis for evaulation.
|
21
|
+
# It first attempts to use a script redis has already cached by using
|
22
|
+
# the +EVALSHA+ command, but falls back to providing the full script
|
23
|
+
# text via +EVAL+ if redis has not seen this script before. Future
|
24
|
+
# invocations will then use +EVALSHA+ without erroring.
|
25
|
+
#
|
26
|
+
# @param redis [Redis] the redis connection to run against
|
27
|
+
# @param args [*Objects] the arguments to the script
|
28
|
+
# @return [Object] the value passed back by redis after script execution
|
29
|
+
# @raise [LuaError] if the script failed to compile of encountered a
|
30
|
+
# runtime error
|
31
|
+
def call(redis, *args)
|
32
|
+
t = Time.now
|
33
|
+
begin
|
34
|
+
redis.evalsha(digest, *args)
|
39
35
|
rescue => e
|
40
|
-
|
41
|
-
raise LuaError.new(e, @file, content)
|
42
|
-
else
|
43
|
-
raise
|
44
|
-
end
|
36
|
+
e.message =~ /NOSCRIPT/ ? redis.eval(content, *args) : raise
|
45
37
|
end
|
46
|
-
|
47
|
-
|
48
|
-
|
38
|
+
rescue => e
|
39
|
+
if LuaError.intercepts?(e)
|
40
|
+
raise LuaError.new(e, @file, content)
|
41
|
+
else
|
42
|
+
raise
|
49
43
|
end
|
44
|
+
end
|
50
45
|
|
51
|
-
|
52
|
-
|
53
|
-
|
46
|
+
def content
|
47
|
+
@content ||= load_lua(@file)
|
48
|
+
end
|
54
49
|
|
55
|
-
|
50
|
+
def digest
|
51
|
+
@digest ||= Digest::SHA1.hexdigest content
|
52
|
+
end
|
56
53
|
|
57
|
-
|
58
|
-
Rails.root + 'app/redis_lua'
|
59
|
-
end
|
54
|
+
private
|
60
55
|
|
61
|
-
|
62
|
-
|
63
|
-
|
56
|
+
def script_path
|
57
|
+
Rails.root + 'app/redis_lua'
|
58
|
+
end
|
64
59
|
|
65
|
-
|
66
|
-
|
67
|
-
|
60
|
+
def relative_path
|
61
|
+
@path ||= @file.relative_path_from(script_path)
|
62
|
+
end
|
68
63
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
end
|
64
|
+
def load_lua(file)
|
65
|
+
TemplateContext.new(script_path).template(script_path + file)
|
66
|
+
end
|
73
67
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
68
|
+
class TemplateContext
|
69
|
+
def initialize(script_path)
|
70
|
+
@script_path = script_path
|
71
|
+
end
|
78
72
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
# `script_path`
|
83
|
-
def include_partial(relative_path)
|
84
|
-
unless @partial_templates.has_key? relative_path
|
85
|
-
@partial_templates[relative_path] = nil
|
86
|
-
template( Pathname.new("#{@script_path}/#{relative_path}") )
|
87
|
-
end
|
88
|
-
end
|
73
|
+
def template(pathname)
|
74
|
+
@partial_templates ||= {}
|
75
|
+
ERB.new(File.read(pathname)).result binding
|
89
76
|
end
|
90
77
|
|
91
|
-
#
|
92
|
-
#
|
93
|
-
#
|
94
|
-
#
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
CONTEXT_LINE_NUMBER = 2
|
100
|
-
|
101
|
-
attr_reader :error, :file, :content
|
102
|
-
|
103
|
-
# Is this error one that should be reformatted?
|
104
|
-
#
|
105
|
-
# @param error [StandardError] the original error raised by redis
|
106
|
-
# @return [Boolean] is this an error that should be reformatted?
|
107
|
-
def self.intercepts? error
|
108
|
-
error.message =~ PATTERN
|
78
|
+
# helper method to include a lua partial within another lua script
|
79
|
+
#
|
80
|
+
# @param relative_path [String] the relative path to the script from
|
81
|
+
# `script_path`
|
82
|
+
def include_partial(relative_path)
|
83
|
+
unless @partial_templates.has_key? relative_path
|
84
|
+
@partial_templates[relative_path] = nil
|
85
|
+
template( Pathname.new("#{@script_path}/#{relative_path}") )
|
109
86
|
end
|
87
|
+
end
|
88
|
+
end
|
110
89
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
@content = content
|
121
|
-
|
122
|
-
@error.message =~ PATTERN
|
123
|
-
_stage, line_number, message = $1, $2, $3
|
124
|
-
error_context = generate_error_context(content, line_number.to_i)
|
125
|
-
|
126
|
-
super "#{message}\n\n#{error_context}\n\n"
|
127
|
-
set_backtrace generate_backtrace file, line_number
|
128
|
-
end
|
90
|
+
# Reformats errors raised by redis representing failures while executing
|
91
|
+
# a lua script. The default errors have confusing messages and backtraces,
|
92
|
+
# and a type of +RuntimeError+. This class improves the message and
|
93
|
+
# modifies the backtrace to include the lua script itself in a reasonable
|
94
|
+
# way.
|
95
|
+
class LuaError < StandardError
|
96
|
+
PATTERN = /ERR Error (compiling|running) script \(.*?\): .*?:(\d+): (.*)/
|
97
|
+
WOLVERINE_LIB_PATH = File.expand_path('../../', __FILE__)
|
98
|
+
CONTEXT_LINE_NUMBER = 2
|
129
99
|
|
130
|
-
|
100
|
+
attr_reader :error, :file, :content
|
131
101
|
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
102
|
+
# Is this error one that should be reformatted?
|
103
|
+
#
|
104
|
+
# @param error [StandardError] the original error raised by redis
|
105
|
+
# @return [Boolean] is this an error that should be reformatted?
|
106
|
+
def self.intercepts? error
|
107
|
+
error.message =~ PATTERN
|
108
|
+
end
|
137
109
|
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
110
|
+
# Initialize a new {LuaError} from an existing redis error, adjusting
|
111
|
+
# the message and backtrace in the process.
|
112
|
+
#
|
113
|
+
# @param error [StandardError] the original error raised by redis
|
114
|
+
# @param file [Pathname] full path to the lua file the error ocurred in
|
115
|
+
# @param content [String] lua file content the error ocurred in
|
116
|
+
def initialize error, file, content
|
117
|
+
@error = error
|
118
|
+
@file = file
|
119
|
+
@content = content
|
120
|
+
|
121
|
+
@error.message =~ PATTERN
|
122
|
+
_stage, line_number, message = $1, $2, $3
|
123
|
+
error_context = generate_error_context(content, line_number.to_i)
|
124
|
+
|
125
|
+
super "#{message}\n\n#{error_context}\n\n"
|
126
|
+
set_backtrace generate_backtrace file, line_number
|
127
|
+
end
|
144
128
|
|
145
|
-
|
146
|
-
pre_wolverine = backtrace_before_entering_wolverine(@error.backtrace)
|
147
|
-
index_of_first_wolverine_line = (@error.backtrace.size - pre_wolverine.size - 1)
|
148
|
-
pre_wolverine.unshift(@error.backtrace[index_of_first_wolverine_line])
|
149
|
-
pre_wolverine.unshift("#{file}:#{line_number}")
|
150
|
-
pre_wolverine
|
151
|
-
end
|
129
|
+
private
|
152
130
|
|
153
|
-
|
154
|
-
|
155
|
-
|
131
|
+
def generate_error_context(content, line_number)
|
132
|
+
lines = content.lines.to_a
|
133
|
+
beginning_line_number = [1, line_number - CONTEXT_LINE_NUMBER].max
|
134
|
+
ending_line_number = [lines.count, line_number + CONTEXT_LINE_NUMBER].min
|
135
|
+
line_number_width = ending_line_number.to_s.length
|
136
|
+
|
137
|
+
(beginning_line_number..ending_line_number).map do |number|
|
138
|
+
indicator = number == line_number ? '=>' : ' '
|
139
|
+
formatted_number = "%#{line_number_width}d" % number
|
140
|
+
" #{indicator} #{formatted_number}: #{lines[number - 1]}"
|
141
|
+
end.join.chomp
|
142
|
+
end
|
156
143
|
|
157
|
-
|
158
|
-
|
159
|
-
|
144
|
+
def generate_backtrace(file, line_number)
|
145
|
+
pre_wolverine = backtrace_before_entering_wolverine(@error.backtrace)
|
146
|
+
index_of_first_wolverine_line = (@error.backtrace.size - pre_wolverine.size - 1)
|
147
|
+
pre_wolverine.unshift(@error.backtrace[index_of_first_wolverine_line])
|
148
|
+
pre_wolverine.unshift("#{file}:#{line_number}")
|
149
|
+
pre_wolverine
|
150
|
+
end
|
151
|
+
|
152
|
+
def backtrace_before_entering_wolverine(backtrace)
|
153
|
+
backtrace.reverse.take_while { |line| ! line_from_wolverine(line) }.reverse
|
154
|
+
end
|
155
|
+
|
156
|
+
def line_from_wolverine(line)
|
157
|
+
line.split(':').first.include?(WOLVERINE_LIB_PATH)
|
160
158
|
end
|
161
159
|
end
|
162
160
|
end
|
@@ -1,90 +1,88 @@
|
|
1
|
-
module CanvasSync
|
2
|
-
|
3
|
-
class
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
}
|
87
|
-
end
|
1
|
+
module CanvasSync::JobBatches
|
2
|
+
class Batch
|
3
|
+
class Status
|
4
|
+
attr_reader :bid
|
5
|
+
|
6
|
+
def initialize(bid)
|
7
|
+
bid = bid.bid if bid.is_a?(Batch)
|
8
|
+
@bid = bid
|
9
|
+
end
|
10
|
+
|
11
|
+
def join
|
12
|
+
raise "Not supported"
|
13
|
+
end
|
14
|
+
|
15
|
+
def pending
|
16
|
+
Batch.redis { |r| r.hget("BID-#{bid}", 'pending') }.to_i
|
17
|
+
end
|
18
|
+
|
19
|
+
def failures
|
20
|
+
Batch.redis { |r| r.scard("BID-#{bid}-failed") }.to_i
|
21
|
+
end
|
22
|
+
|
23
|
+
def dead
|
24
|
+
Batch.redis { |r| r.scard("BID-#{bid}-dead") }.to_i
|
25
|
+
end
|
26
|
+
|
27
|
+
def completed_count
|
28
|
+
job_count - pending
|
29
|
+
end
|
30
|
+
|
31
|
+
def job_count
|
32
|
+
Batch.redis { |r| r.hget("BID-#{bid}", "job_count") }.to_i
|
33
|
+
end
|
34
|
+
|
35
|
+
def created_at
|
36
|
+
Batch.redis { |r| r.hget("BID-#{bid}", 'created_at') }
|
37
|
+
end
|
38
|
+
|
39
|
+
def parent_bid
|
40
|
+
Batch.redis { |r| r.hget("BID-#{bid}", "parent_bid") }
|
41
|
+
end
|
42
|
+
|
43
|
+
def failure_info
|
44
|
+
Batch.redis { |r| r.smembers("BID-#{bid}-failed") } || []
|
45
|
+
end
|
46
|
+
|
47
|
+
def complete?
|
48
|
+
'true' == Batch.redis { |r| r.hget("BID-#{bid}", 'complete') }
|
49
|
+
end
|
50
|
+
|
51
|
+
def success?
|
52
|
+
'true' == Batch.redis { |r| r.hget("BID-#{bid}", 'success') }
|
53
|
+
end
|
54
|
+
|
55
|
+
def child_count
|
56
|
+
Batch.redis { |r| r.hget("BID-#{bid}", 'children') }.to_i
|
57
|
+
end
|
58
|
+
|
59
|
+
def completed_children_count
|
60
|
+
Batch.redis { |r| r.scard("BID-#{bid}-batches-complete") }.to_i
|
61
|
+
end
|
62
|
+
|
63
|
+
def successful_children_count
|
64
|
+
Batch.redis { |r| r.scard("BID-#{bid}-batches-success") }.to_i
|
65
|
+
end
|
66
|
+
|
67
|
+
def failed_children_count
|
68
|
+
Batch.redis { |r| r.scard("BID-#{bid}-batches-failed") }.to_i
|
69
|
+
end
|
70
|
+
|
71
|
+
def data
|
72
|
+
{
|
73
|
+
bid: bid,
|
74
|
+
failures: failures,
|
75
|
+
pending: pending,
|
76
|
+
created_at: created_at,
|
77
|
+
complete: complete?,
|
78
|
+
success: success?,
|
79
|
+
failure_info: failure_info,
|
80
|
+
parent_bid: parent_bid,
|
81
|
+
child_count: child_count,
|
82
|
+
completed_children_count: completed_children_count,
|
83
|
+
successful_children_count: successful_children_count,
|
84
|
+
failed_children_count: failed_children_count,
|
85
|
+
}
|
88
86
|
end
|
89
87
|
end
|
90
88
|
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
|
2
|
+
module CanvasSync::JobUniqueness
|
3
|
+
module Compat
|
4
|
+
module ActiveJob
|
5
|
+
|
6
|
+
class ActiveJobLockContext < LockContext
|
7
|
+
def job_scheduled_at
|
8
|
+
job_instance&.scheduled_at
|
9
|
+
end
|
10
|
+
|
11
|
+
def reenqueue(schedule_in:)
|
12
|
+
job_class.set(
|
13
|
+
queue: job_queue.to_sym,
|
14
|
+
wait: schedule_in,
|
15
|
+
priortity: job_instance.priority,
|
16
|
+
).perform_later(*job_instance.arguments)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module UniqueJobExtension
|
21
|
+
extend ActiveSupport::Concern
|
22
|
+
|
23
|
+
included do
|
24
|
+
set_callback(:enqueue, :around, prepend: true) do |job, block|
|
25
|
+
ctx = uniqueness_lock_context
|
26
|
+
@uniqueness_cache_data = ctx.cache_data
|
27
|
+
ctx.handle_lifecycle!(:enqueue, &block)
|
28
|
+
end
|
29
|
+
|
30
|
+
around_perform do |job, block|
|
31
|
+
ctx = uniqueness_lock_context
|
32
|
+
ctx.handle_lifecycle!(:perform, &block)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def serialize
|
37
|
+
super.tap do |data|
|
38
|
+
data['uniqueness_cache_data'] = @uniqueness_cache_data.stringify_keys
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def deserialize(data)
|
43
|
+
super
|
44
|
+
@uniqueness_cache_data = data['uniqueness_cache_data'].symbolize_keys
|
45
|
+
end
|
46
|
+
|
47
|
+
def uniqueness_lock_context
|
48
|
+
ActiveJobLockContext.new({
|
49
|
+
job_clazz: self.class,
|
50
|
+
jid: self.job_id,
|
51
|
+
args: self.arguments,
|
52
|
+
queue: self.queue_name,
|
53
|
+
**(@uniqueness_cache_data || {})
|
54
|
+
}, job_instance: self)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
module JobExtension
|
59
|
+
extend ActiveSupport::Concern
|
60
|
+
include UniqueJobCommon
|
61
|
+
|
62
|
+
class_methods do
|
63
|
+
def ensure_uniqueness(**kwargs)
|
64
|
+
super(**kwargs)
|
65
|
+
include UniqueJobExtension
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.configure
|
71
|
+
::ActiveJob::Base.include JobExtension
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|