Class: Kaal::Internal::ActiveRecord::DatabaseBackend
Overview
Table-backed lock engine used for SQLite-style Active Record storage.
Instance Method Summary
collapse
#log_dispatch_attempt, #parse_lock_key, parse_lock_key
#with_lock
Constructor Details
#initialize(connection = nil, lock_model: LockRecord, dispatch_registry: nil, definition_registry: nil, namespace: nil) ⇒ DatabaseBackend
Returns a new instance of DatabaseBackend.
17
18
19
20
21
22
23
24
|
# File 'lib/kaal/internal/active_record/database_backend.rb', line 17
def initialize(connection = nil, lock_model: LockRecord, dispatch_registry: nil, definition_registry: nil, namespace: nil)
super()
ConnectionSupport.configure!(connection)
@lock_model = lock_model
@dispatch_registry = dispatch_registry
@definition_registry = definition_registry
@namespace = namespace
end
|
Instance Method Details
#acquire(key, ttl) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/kaal/internal/active_record/database_backend.rb', line 34
def acquire(key, ttl)
now = Time.now.utc
expires_at = now + ttl
2.times do |attempt|
cleanup_expired_locks if attempt.positive?
begin
@lock_model.create!(key: key, acquired_at: now, expires_at: expires_at)
log_dispatch_attempt(key)
return true
rescue ::ActiveRecord::RecordNotUnique
next
end
end
false
rescue StandardError => e
raise Kaal::Backend::LockAdapterError, "Database acquire failed for #{key}: #{e.message}"
end
|
#cleanup_expired_locks ⇒ Object
61
62
63
|
# File 'lib/kaal/internal/active_record/database_backend.rb', line 61
def cleanup_expired_locks
@lock_model.where(expires_at: ...Time.now.utc).delete_all
end
|
#definition_registry ⇒ Object
30
31
32
|
# File 'lib/kaal/internal/active_record/database_backend.rb', line 30
def definition_registry
@definition_registry ||= DefinitionRegistry.new
end
|
#dispatch_registry ⇒ Object
26
27
28
|
# File 'lib/kaal/internal/active_record/database_backend.rb', line 26
def dispatch_registry
@dispatch_registry ||= DispatchRegistry.new(namespace: resolved_namespace)
end
|
#release(key) ⇒ Object
55
56
57
58
59
|
# File 'lib/kaal/internal/active_record/database_backend.rb', line 55
def release(key)
@lock_model.where(key: key).delete_all.positive?
rescue StandardError => e
raise Kaal::Backend::LockAdapterError, "Database release failed for #{key}: #{e.message}"
end
|