mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-05 07:27:15 +08:00
feat(plugin): 建立可信来源准入与安装恢复 (#6462)
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import copy
|
||||
import threading
|
||||
from typing import Any, Optional, Union
|
||||
from collections.abc import Callable
|
||||
from typing import Any, Optional, TypeVar, Union
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db.base import DbOper
|
||||
@@ -9,6 +11,8 @@ from app.db.models.systemconfig import SystemConfig
|
||||
from app.schemas.types import SystemConfigKey
|
||||
from app.foundation.singleton import Singleton
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
class SystemConfigOper(DbOper, metaclass=Singleton):
|
||||
"""
|
||||
@@ -80,6 +84,37 @@ class SystemConfigOper(DbOper, metaclass=Singleton):
|
||||
self._publish_value(key, value)
|
||||
return result
|
||||
|
||||
def update_atomically(
|
||||
self,
|
||||
key: Union[str, SystemConfigKey],
|
||||
mutation: Callable[[Session, Any], tuple[T, Any]],
|
||||
) -> T:
|
||||
"""在配置写锁内提交关联记录,并在事务成功后发布最终配置值。"""
|
||||
if isinstance(key, SystemConfigKey):
|
||||
key = key.value
|
||||
self._require_loaded()
|
||||
with self._write_lock:
|
||||
|
||||
def write(db: Session) -> tuple[T, Any]:
|
||||
"""锁定配置行,把关联写入与最终配置值放入同一事务。"""
|
||||
conf = db.execute(
|
||||
select(SystemConfig)
|
||||
.where(SystemConfig.key == key)
|
||||
.with_for_update()
|
||||
).scalar_one_or_none()
|
||||
current = copy.deepcopy(conf.value if conf else None)
|
||||
result, value = mutation(db, current)
|
||||
committed_value = copy.deepcopy(value)
|
||||
if conf:
|
||||
conf.value = committed_value
|
||||
else:
|
||||
db.add(SystemConfig(key=key, value=committed_value))
|
||||
return result, committed_value
|
||||
|
||||
result, committed_value = self._execute_sync_write(write)
|
||||
self._publish_value(key, committed_value)
|
||||
return result
|
||||
|
||||
def get(self, key: Optional[Union[str, SystemConfigKey]] = None) -> Any:
|
||||
"""
|
||||
获取系统设置
|
||||
|
||||
Reference in New Issue
Block a user