Class: TouchWorker

Inherits:
ApplicationWorker show all
Defined in:
app/workers/touch_worker.rb

Overview

to refresh user record cache we use updated_at column as cache_key context: hungryhubgroup.slack.com/archives/C04BJKAJXS5/p1713243615214859

Constant Summary collapse

MAX_RETRY =
30
MAX_RETRY_VALUE_FOR_RELOAD =

we use multiple DB, so we need to make sure the data is already synced

3
LOCK_TIMEOUT =

seconds

10

Instance Method Summary collapse

Methods inherited from ApplicationWorker

unlimited_retry

Instance Method Details

#perform(klass, resource_id, column = :updated_at, retry_times = 0) ⇒ Object

An exception will be raised if saving the data fails, triggering a retry there is unlimited retry to retry the job



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/workers/touch_worker.rb', line 21

def perform(klass, resource_id, column = :updated_at, retry_times = 0)
  instance = klass.constantize.find_by(id: resource_id)
  if instance.nil? && retry_times < MAX_RETRY_VALUE_FOR_RELOAD
    # re run after x seconds, use exponential backoff
    # x = retry_times * 2
    retry_times = retry_times.to_i + 1
    ::TouchWorker.perform_at(perform_at_time(retry_times), klass, resource_id, column, retry_times)
    return
  end

  column = column.to_sym
  old_value = instance.send column

  # #touch method uses transaction
  instance.touch column.to_sym

  return if instance.send(column) != old_value # success

  retry_times = retry_times.to_i + 1
  raise "Failed to update #{column}" if retry_times > MAX_RETRY

  ::TouchWorker.perform_at(perform_at_time(retry_times), klass, resource_id, column, retry_times)
end