mirror of
https://github.com/langgenius/dify.git
synced 2026-05-06 02:18:08 +08:00
refactor(api): enhance DbMigrationAutoRenewLock acquisition logic
- Added a check to prevent double acquisition of the DB migration lock, raising an error if an attempt is made to acquire it while already held. - Implemented logic to reuse the lock object if it has already been created, improving efficiency and clarity in lock management. - Reset the lock object to None upon release to ensure proper state management. (cherry picked from commit d4b102d3c8a473c4fd6409dba7c198289bb5f921)
This commit is contained in:
@ -86,11 +86,17 @@ class DbMigrationAutoRenewLock:
|
|||||||
|
|
||||||
Accepts the same args/kwargs as redis-py `Lock.acquire()`.
|
Accepts the same args/kwargs as redis-py `Lock.acquire()`.
|
||||||
"""
|
"""
|
||||||
self._lock = self._redis_client.lock(
|
# Prevent accidental double-acquire which could leave the previous heartbeat thread running.
|
||||||
name=self._name,
|
if self._acquired:
|
||||||
timeout=self._ttl_seconds,
|
raise RuntimeError("DB migration lock is already acquired; call release_safely() before acquiring again.")
|
||||||
thread_local=False,
|
|
||||||
)
|
# Reuse the lock object if we already created one.
|
||||||
|
if self._lock is None:
|
||||||
|
self._lock = self._redis_client.lock(
|
||||||
|
name=self._name,
|
||||||
|
timeout=self._ttl_seconds,
|
||||||
|
thread_local=False,
|
||||||
|
)
|
||||||
acquired = bool(self._lock.acquire(*args, **kwargs))
|
acquired = bool(self._lock.acquire(*args, **kwargs))
|
||||||
self._acquired = acquired
|
self._acquired = acquired
|
||||||
if acquired:
|
if acquired:
|
||||||
@ -184,6 +190,7 @@ class DbMigrationAutoRenewLock:
|
|||||||
)
|
)
|
||||||
finally:
|
finally:
|
||||||
self._acquired = False
|
self._acquired = False
|
||||||
|
self._lock = None
|
||||||
|
|
||||||
def _stop_heartbeat(self) -> None:
|
def _stop_heartbeat(self) -> None:
|
||||||
if self._stop_event is None:
|
if self._stop_event is None:
|
||||||
|
|||||||
Reference in New Issue
Block a user