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.
18
19
20
21
22
23
24
25
|
# File 'lib/kaal/internal/active_record/database_backend.rb', line 18
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/kaal/internal/active_record/database_backend.rb', line 39
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
66
67
68
|
# File 'lib/kaal/internal/active_record/database_backend.rb', line 66
def cleanup_expired_locks
@lock_model.where(expires_at: ...Time.now.utc).delete_all
end
|
#definition_registry ⇒ Object
31
32
33
|
# File 'lib/kaal/internal/active_record/database_backend.rb', line 31
def definition_registry
@definition_registry ||= DefinitionRegistry.new
end
|
#delayed_store ⇒ Object
35
36
37
|
# File 'lib/kaal/internal/active_record/database_backend.rb', line 35
def delayed_store
@delayed_store ||= DelayedJobRegistry.new
end
|
#dispatch_registry ⇒ Object
27
28
29
|
# File 'lib/kaal/internal/active_record/database_backend.rb', line 27
def dispatch_registry
@dispatch_registry ||= DispatchRegistry.new(namespace: resolved_namespace)
end
|
#release(key) ⇒ Object
60
61
62
63
64
|
# File 'lib/kaal/internal/active_record/database_backend.rb', line 60
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
|