Skip to content

Commit 782de91

Browse files
authored
Delete TenantMediaSet in Batch and safeguard loading detached media (#1990)
#### Summary The PR consists of two parts: 1. Safeguard loading detached media. Loading detached media to temp record could fail (observed in Telemetry) with duplicated ID, using `if ... then ...` so safeguard the insert. 2. Delete Tenant Media Set in batch and Commit between each batch. Just like what we did with Tenant Media, providing the ability to clean up even when there are large number of records. The code change should be covered by test case: `EnsureDetachedMediaAndMediaSetAreCleanedUp` #### Work Item(s) Fixes [AB#548323](https://dynamicssmb2.visualstudio.com/1fcb79e7-ab07-432a-a3c6-6cf5a88ba4a5/_workitems/edit/548323)
1 parent 69d45e8 commit 782de91

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/System Application/App/Data Administration/src/MediaCleanup.Codeunit.al

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ codeunit 1927 "Media Cleanup"
8383

8484
/// <summary>
8585
/// Deletes all detached tenant media sets.
86+
/// Note: This function will delete detached media set in batches of 10 and commit in between each batch.
87+
/// This is to ensure we don't get stuck always trying to delete the same media and time out.
8688
/// </summary>
8789
procedure DeleteDetachedTenantMediaSet()
8890
begin

src/System Application/App/Data Administration/src/MediaCleanupImpl.Codeunit.al

+8-5
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ codeunit 1928 "Media Cleanup Impl."
7474
repeat
7575
if TenantMedia.Get(TenantMediaSet."Media ID".MediaId()) then begin
7676
TempTenantMediaVar := TenantMedia;
77-
TempTenantMediaVar.Insert();
78-
MediaLoaded += 1;
77+
if TempTenantMediaVar.Insert() then
78+
MediaLoaded += 1;
7979
if MediaLoaded >= RecordLimit then
8080
exit(false);
8181
end;
@@ -162,16 +162,19 @@ codeunit 1928 "Media Cleanup Impl."
162162
var
163163
[SecurityFiltering(SecurityFilter::Ignored)]
164164
TenantMediaSet: Record "Tenant Media Set";
165+
SplitList: List of [List of [Guid]];
165166
MediaSetOrphans: List of [Guid];
166-
Orphan: Guid;
167+
MediaOrphanSubList: List of [Guid];
167168
begin
168169
if not TenantMediaSet.WritePermission() then
169170
exit;
170171

171172
MediaSetOrphans := MediaSet.FindOrphans();
172-
foreach Orphan in MediaSetOrphans do begin
173-
TenantMediaSet.SetRange(ID, Orphan);
173+
SplitListIntoSubLists(MediaSetOrphans, 10, SplitList);
174+
foreach MediaOrphanSubList in SplitList do begin
175+
TenantMediaSet.SetFilter(ID, CreateOrFilter(MediaOrphanSubList));
174176
TenantMediaSet.DeleteAll();
177+
Commit(); // Ensure we keep the progress even on timeout (in case of large amounts of detached media).
175178
end;
176179
end;
177180

0 commit comments

Comments
 (0)