Skip to content

Commit

Permalink
[24.x] Delete TenantMediaSet in Batch and safeguard loading detached …
Browse files Browse the repository at this point in the history
…media (#1995)

<!-- Thank you for submitting a Pull Request. If you're new to
contributing to BCApps please read our pull request guideline below
* https://github.com/microsoft/BCApps/Contributing.md
-->
#### Summary <!-- Provide a general summary of your changes -->

port from #1990

#### Work Item(s) <!-- Add the issue number here after the #. The issue
needs to be open and approved. Submitting PRs with no linked issues or
unapproved issues is highly discouraged. -->
Fixes
[AB#548337](https://dynamicssmb2.visualstudio.com/1fcb79e7-ab07-432a-a3c6-6cf5a88ba4a5/_workitems/edit/548337)
  • Loading branch information
haoranpb authored Sep 10, 2024
1 parent 12f1388 commit d096669
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ codeunit 1927 "Media Cleanup"

/// <summary>
/// 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.
/// </summary>
procedure DeleteDetachedTenantMediaSet()
begin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
Expand Down

0 comments on commit d096669

Please sign in to comment.