Class: SimpleMutex::Helper

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

Constant Summary collapse

LIST_MODES =
%i[job batch default all].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get(lock_key) ⇒ Object



10
11
12
# File 'lib/simple_mutex/helper.rb', line 10

def get(lock_key)
  new.get(lock_key)
end

.list(**options) ⇒ Object



14
15
16
# File 'lib/simple_mutex/helper.rb', line 14

def list(**options)
  new.list(**options)
end

Instance Method Details

#get(lock_key) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/simple_mutex/helper.rb', line 19

def get(lock_key)
  raw_data = redis.get(lock_key)

  return if raw_data.nil?

  parsed_data = safe_parse(raw_data)

  {
    key: lock_key,
    value: parsed_data.nil? ? raw_data : parsed_data,
  }
end

#list(mode: :default) ⇒ Object

rubocop:disable Metrics/MethodLength, Style/HashEachMethods, Performance/CollectionLiteralInLoop



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/simple_mutex/helper.rb', line 33

def list(mode: :default)
  check_mode(mode)

  result = []

  redis.keys.each do |lock_key|
    redis.watch(lock_key) do
      raw_data = redis.get(lock_key)

      unless raw_data.nil?
        parsed_data = safe_parse(raw_data)

        if parsed_data.nil?
          result << { key: lock_key, value: raw_data } if mode == :all
        else
          lock_type = parsed_data&.dig("payload", "type")

          if (mode == :all) ||
             (lock_type == "Job" && %i[job default].include?(mode)) ||
             (lock_type == "Batch" && %i[batch default].include?(mode))
            result << { key: lock_key, value: parsed_data }
          end
        end
      end

      redis.unwatch
    end
  end

  result
end