Repository: langgenius/dify
File: api/libs/db_migration_lock.py (Python)

Code before:
                self._logger.warning(
                    "Unexpected error while renewing DB migration lock%s; will retry.",
                    f" ({self._log_context})" if self._log_context else "",
                    exc_info=True,
                )

    def release_safely(self, *, status: str | None = None) -> None:
        """
        Stop heartbeat and release lock. Never raises.

        Args:
            status: Optional caller-provided status (e.g. 'successful'/'failed') to add context to logs.
        """
        lock = self._lock
        if lock is None:
            return

        self._stop_heartbeat()

        # Lock release errors should never mask the real error/exit code.
        try:
            lock.release()
        except LockNotOwnedError:
            self._logger.warning(
                "DB migration lock not owned on release%s%s; ignoring.",
                f" after {status} operation" if status else "",
                f" ({self._log_context})" if self._log_context else "",
                exc_info=True,
            )
        except RedisError:
            self._logger.warning(
                "Failed to release DB migration lock due to Redis error%s%s; ignoring.",
                f" after {status} operation" if status else "",
                f" ({self._log_context})" if self._log_context else "",
                exc_info=True,
            )
        except Exception:
            self._logger.warning(
                "Unexpected error while releasing DB migration lock%s%s; ignoring.",
                f" after {status} operation" if status else "",
                f" ({self._log_context})" if self._log_context else "",
                exc_info=True,
            )
        finally:
            self._acquired = False

    def _stop_heartbeat(self) -> None:
        if self._stop_event is None:
            return
        self._stop_event.set()
        if self._thread is not None:

Review comment (suggestion):
avoiding use f-string for logging, use log string with [printf-style format](https://docs.python.org/3/library/stdtypes.html#old-string-formatting) instead.


Code after:
            except LockNotOwnedError:
                self._logger.warning(
                    "DB migration lock is no longer owned during heartbeat; stop renewing. log_context=%s",
                    self._log_context,
                    exc_info=True,
                )
                return
            except RedisError:
                self._logger.warning(
                    "Failed to renew DB migration lock due to Redis error; will retry. log_context=%s",
                    self._log_context,
                    exc_info=True,
                )
            except Exception:
                self._logger.warning(
                    "Unexpected error while renewing DB migration lock; will retry. log_context=%s",
                    self._log_context,
                    exc_info=True,
                )

    def release_safely(self, *, status: str | None = None) -> None:
        """
        Stop heartbeat and release lock. Never raises.

        Args:
            status: Optional caller-provided status (e.g. 'successful'/'failed') to add context to logs.
        """
        lock = self._lock
        if lock is None:
            return

        self._stop_heartbeat()

        # Lock release errors should never mask the real error/exit code.
        try:
            lock.release()
        except LockNotOwnedError:
            self._logger.warning(
                "DB migration lock not owned on release; ignoring. status=%s log_context=%s",
                status,
                self._log_context,
                exc_info=True,
            )
        except RedisError:
            self._logger.warning(
                "Failed to release DB migration lock due to Redis error; ignoring. status=%s log_context=%s",
                status,
                self._log_context,
                exc_info=True,
            )
        except Exception: