From fb558b980fbc486bc459ffff8e31faff051b0954 Mon Sep 17 00:00:00 2001 From: Sun Haoran Date: Tue, 10 Sep 2024 14:04:01 +0200 Subject: [PATCH] [24.4] Delete TenantMediaSet in Batch and safeguard loading detached media (#1998) #### Summary port from #1990 #### Work Item(s) Fixes [AB#548335](https://dynamicssmb2.visualstudio.com/1fcb79e7-ab07-432a-a3c6-6cf5a88ba4a5/_workitems/edit/548335) --- .../src/MediaCleanup.Codeunit.al | 2 ++ .../src/MediaCleanupImpl.Codeunit.al | 15 ++++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/System Application/App/Data Administration/src/MediaCleanup.Codeunit.al b/src/System Application/App/Data Administration/src/MediaCleanup.Codeunit.al index 426030b3a0..e57fcb2b50 100644 --- a/src/System Application/App/Data Administration/src/MediaCleanup.Codeunit.al +++ b/src/System Application/App/Data Administration/src/MediaCleanup.Codeunit.al @@ -83,6 +83,8 @@ codeunit 1927 "Media Cleanup" /// /// Deletes all detached tenant media sets. + /// Note: This function will delete detached media set in batches of 10 and commit in between each batch. + /// This is to ensure we don't get stuck always trying to delete the same media and time out. /// procedure DeleteDetachedTenantMediaSet() begin diff --git a/src/System Application/App/Data Administration/src/MediaCleanupImpl.Codeunit.al b/src/System Application/App/Data Administration/src/MediaCleanupImpl.Codeunit.al index 615adbc589..f3a2cbb45d 100644 --- a/src/System Application/App/Data Administration/src/MediaCleanupImpl.Codeunit.al +++ b/src/System Application/App/Data Administration/src/MediaCleanupImpl.Codeunit.al @@ -74,8 +74,8 @@ codeunit 1928 "Media Cleanup Impl." repeat if TenantMedia.Get(TenantMediaSet."Media ID".MediaId()) then begin TempTenantMediaVar := TenantMedia; - TempTenantMediaVar.Insert(); - MediaLoaded += 1; + if TempTenantMediaVar.Insert() then + MediaLoaded += 1; if MediaLoaded >= RecordLimit then exit(false); end; @@ -160,22 +160,27 @@ codeunit 1928 "Media Cleanup Impl." procedure DeleteDetachedTenantMediaSet() var + [SecurityFiltering(SecurityFilter::Ignored)] TenantMediaSet: Record "Tenant Media Set"; + SplitList: List of [List of [Guid]]; MediaSetOrphans: List of [Guid]; - Orphan: Guid; + MediaOrphanSubList: List of [Guid]; begin if not TenantMediaSet.WritePermission() then exit; MediaSetOrphans := MediaSet.FindOrphans(); - foreach Orphan in MediaSetOrphans do begin - TenantMediaSet.SetRange(ID, Orphan); + SplitListIntoSubLists(MediaSetOrphans, 10, SplitList); + foreach MediaOrphanSubList in SplitList do begin + TenantMediaSet.SetFilter(ID, CreateOrFilter(MediaOrphanSubList)); TenantMediaSet.DeleteAll(); + Commit(); // Ensure we keep the progress even on timeout (in case of large amounts of detached media). end; end; procedure DeleteDetachedTenantMedia() var + [SecurityFiltering(SecurityFilter::Ignored)] TenantMedia: Record "Tenant Media"; SplitList: List of [List of [Guid]]; MediaOrphans: List of [Guid];