Class: CanvasSync::JobUniqueness::LockContext

Inherits:
Object
  • Object
show all
Defined in:
lib/canvas_sync/job_uniqueness/lock_context.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, job_instance: nil, config: nil) ⇒ LockContext

{ job_clazz, jid, queue, args?, kwargs?, base_key? }



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/canvas_sync/job_uniqueness/lock_context.rb', line 10

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]

  # TODO Consider (somewhere) updating the lock_id to the BID of the wrapping Batch (when applicable)
  @lock_id ||= data[:lid] || Thread.current[:unique_jobs_previous_context]&.lock_id
  @lock_id_locked = @lock_id.present?
  @lock_id ||= job_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



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
153
154
155
156
157
158
159
160
# File 'lib/canvas_sync/job_uniqueness/lock_context.rb', line 115

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] || {}
    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)

      # Normalize the hash to ensure that the order of any Hash keys don't matter
      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_dataObject

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,
    # TODO Should config also be cached on the Job at time of enqueue?
    # config: config,
  }
end

#configObject



73
74
75
# File 'lib/canvas_sync/job_uniqueness/lock_context.rb', line 73

def config
  @config ||= job_class.unique_job_options
end

#debug_dataObject



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_classObject



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_idObject



99
100
101
# File 'lib/canvas_sync/job_uniqueness/lock_context.rb', line 99

def job_id
  @context_data[:jid]
end

#job_queueObject



103
104
105
# File 'lib/canvas_sync/job_uniqueness/lock_context.rb', line 103

def job_queue
  @context_data[:queue]
end

#job_scheduled_atObject



107
108
109
# File 'lib/canvas_sync/job_uniqueness/lock_context.rb', line 107

def job_scheduled_at
  nil
end

#job_scoreObject



111
112
113
# File 'lib/canvas_sync/job_uniqueness/lock_context.rb', line 111

def job_score
  @context_data[:job_score] || job_scheduled_at.to_s
end

#lock_idObject



87
88
89
90
# File 'lib/canvas_sync/job_uniqueness/lock_context.rb', line 87

def lock_id
  @lock_id_locked = true
  @lock_id
end

#lock_id=(new_id) ⇒ Object



92
93
94
95
96
97
# File 'lib/canvas_sync/job_uniqueness/lock_context.rb', line 92

def lock_id=(new_id)
  raise "Lock ID already set" if @lock_id_locked
  return unless new_id.present?
  @lock_id = new_id
  @lock_id_locked = true
end

#lock_strategyObject



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

#reenqueueObject

Raises:

  • (NotImplementedError)


162
163
164
# File 'lib/canvas_sync/job_uniqueness/lock_context.rb', line 162

def reenqueue
  raise NotImplementedError, "needs to be implemented in child class"
end

#serializeObject

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