Class: SimpleMutex::BaseCleaner

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_mutex/base_cleaner.rb

Defined Under Namespace

Classes: SynchronizationAnomalyError

Constant Summary collapse

MAX_DEL_ATTEMPTS =
3

Instance Method Summary collapse

Instance Method Details

#unlockObject

rubocop:disable Metrics/MethodLength



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
# File 'lib/simple_mutex/base_cleaner.rb', line 10

def unlock
  ::SimpleMutex.redis_check!

  logger&.info(start_msg)

  redis.keys.select do |lock_key|
    attempt = 0

    begin
      redis.watch(lock_key) do
        raw_data = redis.get(lock_key)
        raw_data = raw_data.value if raw_data.is_a?(Redis::Future)

        next if raw_data.nil?

        parsed_data = safe_parse(raw_data)

        next unless parsed_data&.dig("payload", "type") == type

        entity_id = parsed_data&.dig(*path_to_entity_id)

        next if entity_id.nil? || active?(entity_id)

        return_value = redis.multi { |multi| multi.del(lock_key) }

        log_iteration(lock_key, raw_data, return_value) unless logger.nil?

        result = return_value&.first

        raise SynchronizationAnomalyError, "Sync anomaly." unless result.is_a?(Integer)

        result.positive?
      ensure
        redis.unwatch
      end
    rescue SynchronizationAnomalyError
      retry if (attempt += 1) < MAX_DEL_ATTEMPTS
    end
  end

  logger&.info(end_msg)
end