feat(app_asset_service): implement asynchronous file deletion in asset management

- Added a threaded function to handle the deletion of storage files asynchronously after asset removal.
- Updated the asset removal logic to include a call to the new deletion function, improving performance and responsiveness during asset management operations.
This commit is contained in:
Harry
2026-01-23 17:27:04 +08:00
parent 8a6e1a695b
commit e1b0ab5c3f

View File

@@ -1,4 +1,5 @@
import logging
import threading
from uuid import uuid4
from sqlalchemy.orm import Session
@@ -288,18 +289,23 @@ class AppAssetService:
removed_ids = tree.remove(node_id)
except TreeNodeNotFoundError as e:
raise AppAssetNodeNotFoundError(str(e)) from e
for nid in removed_ids:
storage_key = AssetPaths.draft_file(app_model.tenant_id, app_model.id, nid)
try:
AppAssetService.assets_storage().delete(storage_key)
except Exception:
logger.warning("Failed to delete storage file %s", storage_key, exc_info=True)
assets.asset_tree = tree
assets.updated_by = account_id
session.commit()
# FIXME(Mairuis): sync deletion queue
def _delete_file_from_storage(tenant_id: str, app_id: str, node_ids: list[str]) -> None:
for nid in removed_ids:
storage_key = AssetPaths.draft_file(app_model.tenant_id, app_model.id, nid)
try:
AppAssetService.assets_storage().delete(storage_key)
except Exception:
logger.warning("Failed to delete storage file %s", storage_key, exc_info=True)
threading.Thread(
target=lambda: _delete_file_from_storage(app_model.tenant_id, app_model.id, removed_ids)
).start()
@staticmethod
def publish(session: Session, app_model: App, account_id: str, workflow_id: str) -> AppAssets:
tenant_id = app_model.tenant_id