Class: CanvasSync::JobUniqueness::LockContext
- Inherits:
-
Object
- Object
- CanvasSync::JobUniqueness::LockContext
show all
- Defined in:
- lib/canvas_sync/job_uniqueness/lock_context.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#base_key(any_hash: false) ⇒ Object
-
#cache_data ⇒ Object
Properties to cache on the serialized Job object to prevent issues arising from code changes between enqueue and perform.
-
#config ⇒ Object
-
#debug_data ⇒ Object
-
#handle_lifecycle!(stage, *args, **kwargs, &blk) ⇒ Object
-
#initialize(data, job_instance: nil, config: nil) ⇒ LockContext
constructor
{ job_clazz, jid, queue, args?, kwargs?, base_key? }.
-
#job_class ⇒ Object
-
#job_id ⇒ Object
-
#job_queue ⇒ Object
-
#job_scheduled_at ⇒ Object
-
#job_score ⇒ Object
-
#lock_strategy ⇒ Object
-
#reenqueue ⇒ Object
-
#serialize ⇒ Object
This is primarily for rehydrating in a Batch Callback, so it is unlikely that args and kwargs are needed.
Constructor Details
#initialize(data, job_instance: nil, config: nil) ⇒ LockContext
{ job_clazz, jid, queue, args?, kwargs?, base_key? }
12
13
14
15
16
17
18
19
20
|
# File 'lib/canvas_sync/job_uniqueness/lock_context.rb', line 12
def initialize(data, job_instance: nil, config: nil)
@base_key = data[:base_key]
@context_data = data
@job_instance = job_instance
@config = config || @context_data[:config]
@lock_id ||= data[:lid] || Thread.current[:unique_jobs_previous_context]&.lock_id || job_id
end
|
Instance Attribute Details
#lock_id ⇒ Object
Returns the value of attribute lock_id.
9
10
11
|
# File 'lib/canvas_sync/job_uniqueness/lock_context.rb', line 9
def lock_id
@lock_id
end
|
Class Method Details
.from_serialized(data, **kwargs) ⇒ Object
4
5
6
7
|
# File 'lib/canvas_sync/job_uniqueness/lock_context.rb', line 4
def self.from_serialized(data, **kwargs)
context_class = data[:clazz]&.constantize || self
context_class.new(data, **kwargs)
end
|
Instance Method Details
#base_key(any_hash: false) ⇒ Object
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/canvas_sync/job_uniqueness/lock_context.rb', line 103
def base_key(any_hash: false)
@base_key ||= begin
queue = @context_data[:queue] || "default"
base_key = [
CanvasSync::JobUniqueness.config.lock_prefix.presence,
].compact
scope = config[:scope]
if scope.is_a?(Proc)
base_key << scope.call(queue: queue)
elsif scope == :global
base_key << job_class.name
elsif scope == :per_queue
base_key << job_class.name
base_key << queue
else
base_key << scope
end
args = @context_data[:args] || []
kwargs = @context_data[:kwargs] || {}
if config[:hash].is_a?(Proc)
base_key << config[:hash].call(*args, **kwargs)
elsif config[:hash].nil?
base_key << OpenSSL::Digest::MD5.hexdigest(JSON.dump([ args, kwargs.sort]))
else
base_key << config[:hash]
end
base_key.join(":")
end
end
|
#cache_data ⇒ Object
Properties to cache on the serialized Job object to prevent issues arising from code changes between enqueue and perform
35
36
37
38
39
40
41
42
43
|
# File 'lib/canvas_sync/job_uniqueness/lock_context.rb', line 35
def cache_data
{
lid: lock_id,
base_key: base_key,
job_score: job_score,
}
end
|
#config ⇒ Object
73
74
75
|
# File 'lib/canvas_sync/job_uniqueness/lock_context.rb', line 73
def config
@config ||= job_class.unique_job_options
end
|
#debug_data ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/canvas_sync/job_uniqueness/lock_context.rb', line 45
def debug_data
{
lid: lock_id,
context_class: self.class.to_s,
job_class: job_class.to_s,
queue: job_queue,
limit: config[:limit],
timeout: config[:timeout],
ttl: config[:ttl],
strategy: config[:strategy],
time: Time.now.to_f,
at: job_scheduled_at,
}
end
|
#handle_lifecycle!(stage, *args, **kwargs, &blk) ⇒ Object
60
61
62
63
64
|
# File 'lib/canvas_sync/job_uniqueness/lock_context.rb', line 60
def handle_lifecycle!(stage, *args, **kwargs, &blk)
lock_strategy.send(:"on_#{stage}", *args, **kwargs, &blk)
rescue CouldNotLockError => e
call_conflict_strategy(stage)
end
|
#job_class ⇒ Object
77
78
79
80
81
82
83
84
85
|
# File 'lib/canvas_sync/job_uniqueness/lock_context.rb', line 77
def job_class
@job_class ||= begin
if (job_clazz = @context_data[:job_clazz]).is_a?(String)
job_clazz.constantize
else
job_clazz
end
end
end
|
#job_id ⇒ Object
87
88
89
|
# File 'lib/canvas_sync/job_uniqueness/lock_context.rb', line 87
def job_id
@context_data[:jid]
end
|
#job_queue ⇒ Object
91
92
93
|
# File 'lib/canvas_sync/job_uniqueness/lock_context.rb', line 91
def job_queue
@context_data[:queue]
end
|
#job_scheduled_at ⇒ Object
95
96
97
|
# File 'lib/canvas_sync/job_uniqueness/lock_context.rb', line 95
def job_scheduled_at
nil
end
|
#job_score ⇒ Object
99
100
101
|
# File 'lib/canvas_sync/job_uniqueness/lock_context.rb', line 99
def job_score
@context_data[:job_score] || job_scheduled_at.to_s
end
|
#lock_strategy ⇒ Object
66
67
68
69
70
71
|
# File 'lib/canvas_sync/job_uniqueness/lock_context.rb', line 66
def lock_strategy
return @lock_strategy if defined?(@lock_strategy)
strat_name = config[:strategy]
@lock_strategy = Strategy.lookup(strat_name).new(self)
end
|
#reenqueue ⇒ Object
137
138
139
|
# File 'lib/canvas_sync/job_uniqueness/lock_context.rb', line 137
def reenqueue
raise NotImplementedError, "needs to be implemented in child class"
end
|
#serialize ⇒ Object
This is primarily for rehydrating in a Batch Callback, so it is unlikely that args and kwargs are needed.
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/canvas_sync/job_uniqueness/lock_context.rb', line 23
def serialize
{
lid: lock_id,
clazz: self.class.to_s,
job_clazz: @context_data[:job_clazz].to_s,
jid: @context_data[:jid],
queue: @context_data[:queue],
**cache_data,
}
end
|