Class: Kaal::Internal::ActiveRecord::MySQLBackend

Inherits:
Backend::Adapter show all
Includes:
Backend::DispatchLogging
Defined in:
lib/kaal/internal/active_record/mysql_backend.rb

Overview

MySQL named-lock engine paired with Active Record registries.

Constant Summary collapse

MAX_LOCK_NAME_LENGTH =
64

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Backend::DispatchLogging

#log_dispatch_attempt, #parse_lock_key, parse_lock_key

Methods inherited from Backend::Adapter

#with_lock

Constructor Details

#initialize(connection = nil, dispatch_registry: nil, definition_registry: nil, namespace: nil) ⇒ MySQLBackend

Returns a new instance of MySQLBackend.



18
19
20
21
22
23
24
# File 'lib/kaal/internal/active_record/mysql_backend.rb', line 18

def initialize(connection = nil, dispatch_registry: nil, definition_registry: nil, namespace: nil)
  super()
  ConnectionSupport.configure!(connection)
  @dispatch_registry = dispatch_registry
  @definition_registry = definition_registry
  @namespace = namespace
end

Class Method Details

.normalize_lock_name(key) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/kaal/internal/active_record/mysql_backend.rb', line 48

def self.normalize_lock_name(key)
  return key if key.length <= MAX_LOCK_NAME_LENGTH

  digest = Digest::SHA256.hexdigest(key)
  prefix_length = MAX_LOCK_NAME_LENGTH - 17
  "#{key[0...prefix_length]}:#{digest[0...16]}"
end

Instance Method Details

#acquire(key, _ttl) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/kaal/internal/active_record/mysql_backend.rb', line 34

def acquire(key, _ttl)
  acquired = scalar('SELECT GET_LOCK(?, 0) AS lock_result', self.class.normalize_lock_name(key)) == 1
  log_dispatch_attempt(key) if acquired
  acquired
rescue StandardError => e
  raise Kaal::Backend::LockAdapterError, "MySQL acquire failed for #{key}: #{e.message}"
end

#definition_registryObject



30
31
32
# File 'lib/kaal/internal/active_record/mysql_backend.rb', line 30

def definition_registry
  @definition_registry ||= DefinitionRegistry.new
end

#dispatch_registryObject



26
27
28
# File 'lib/kaal/internal/active_record/mysql_backend.rb', line 26

def dispatch_registry
  @dispatch_registry ||= DispatchRegistry.new(namespace: resolved_namespace)
end

#release(key) ⇒ Object



42
43
44
45
46
# File 'lib/kaal/internal/active_record/mysql_backend.rb', line 42

def release(key)
  scalar('SELECT RELEASE_LOCK(?) AS lock_result', self.class.normalize_lock_name(key)) == 1
rescue StandardError => e
  raise Kaal::Backend::LockAdapterError, "MySQL release failed for #{key}: #{e.message}"
end