Class: Kaal::Internal::Sequel::MySQLBackend
Overview
MySQL named-lock engine backed by Sequel.
Constant Summary
collapse
- MAX_LOCK_NAME_LENGTH =
64
- UNSET_SKIP_LOCKED_SUPPORT =
Object.new.freeze
Class Method Summary
collapse
Instance Method Summary
collapse
#log_dispatch_attempt, #parse_lock_key, parse_lock_key
#with_lock
Constructor Details
#initialize(database, namespace: nil, use_skip_locked: UNSET_SKIP_LOCKED_SUPPORT) ⇒ MySQLBackend
Returns a new instance of MySQLBackend.
22
23
24
25
26
27
|
# File 'lib/kaal/internal/sequel/mysql_backend.rb', line 22
def initialize(database, namespace: nil, use_skip_locked: UNSET_SKIP_LOCKED_SUPPORT)
super()
@database = Kaal::Persistence::Database.new(database)
@namespace = namespace
@use_skip_locked = use_skip_locked
end
|
Class Method Details
.normalize_lock_name(key) ⇒ Object
55
56
57
58
59
60
61
|
# File 'lib/kaal/internal/sequel/mysql_backend.rb', line 55
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
41
42
43
44
45
46
47
|
# File 'lib/kaal/internal/sequel/mysql_backend.rb', line 41
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_registry ⇒ Object
33
34
35
|
# File 'lib/kaal/internal/sequel/mysql_backend.rb', line 33
def definition_registry
@definition_registry ||= Kaal::Definition::DatabaseEngine.new(database: @database.connection)
end
|
#delayed_store ⇒ Object
37
38
39
|
# File 'lib/kaal/internal/sequel/mysql_backend.rb', line 37
def delayed_store
@delayed_store ||= Kaal::DelayedJob::DatabaseEngine.new(database: @database.connection, use_skip_locked: supports_skip_locked?)
end
|
#dispatch_registry ⇒ Object
29
30
31
|
# File 'lib/kaal/internal/sequel/mysql_backend.rb', line 29
def dispatch_registry
@dispatch_registry ||= Kaal::Dispatch::DatabaseEngine.new(database: @database.connection, namespace: resolved_namespace)
end
|
#release(key) ⇒ Object
49
50
51
52
53
|
# File 'lib/kaal/internal/sequel/mysql_backend.rb', line 49
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
|