Class: Joblin::Uniqueness::LockContext
- Inherits:
-
Object
- Object
- Joblin::Uniqueness::LockContext
show all
- Defined in:
- lib/joblin/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/joblin/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/joblin/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/joblin/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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/joblin/uniqueness/lock_context.rb', line 107
def base_key(any_hash: false)
@base_key ||= begin
queue = @context_data[:queue] || "default"
base_key = [
Joblin::Uniqueness.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] || {}
hash = config[:hash]
if config[:hash].is_a?(Proc)
hash = config[:hash].call(*args, **kwargs)
elsif config[:hash].nil?
hash = [*args, kwargs]
end
hash = ":#{hash}" if hash.is_a?(Symbol)
if hash && !hash.is_a?(String)
hash = Array(hash)
hash = normalize_hash_chunk(hash)
normalized = ActiveJob::Arguments.serialize(hash)
hash = OpenSSL::Digest::MD5.hexdigest(JSON.dump(normalized))
end
base_key << hash if hash
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/joblin/uniqueness/lock_context.rb', line 35
def cache_data
{
lid: lock_id,
base_key: base_key,
job_score: job_score,
}
end
|
#config ⇒ Object
77
78
79
|
# File 'lib/joblin/uniqueness/lock_context.rb', line 77
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/joblin/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
65
66
67
68
|
# File 'lib/joblin/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)
nil
end
|
#job_class ⇒ Object
81
82
83
84
85
86
87
88
89
|
# File 'lib/joblin/uniqueness/lock_context.rb', line 81
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
91
92
93
|
# File 'lib/joblin/uniqueness/lock_context.rb', line 91
def job_id
@context_data[:jid]
end
|
#job_queue ⇒ Object
95
96
97
|
# File 'lib/joblin/uniqueness/lock_context.rb', line 95
def job_queue
@context_data[:queue]
end
|
#job_scheduled_at ⇒ Object
99
100
101
|
# File 'lib/joblin/uniqueness/lock_context.rb', line 99
def job_scheduled_at
nil
end
|
#job_score ⇒ Object
103
104
105
|
# File 'lib/joblin/uniqueness/lock_context.rb', line 103
def job_score
@context_data[:job_score] || job_scheduled_at.to_s
end
|
#lock_strategy ⇒ Object
70
71
72
73
74
75
|
# File 'lib/joblin/uniqueness/lock_context.rb', line 70
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
154
155
156
|
# File 'lib/joblin/uniqueness/lock_context.rb', line 154
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/joblin/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
|