Skip to content

Commit 8fbb11b

Browse files
committed
Log orphaned attachments action for tests
1 parent f6e0efe commit 8fbb11b

4 files changed

Lines changed: 28 additions & 6 deletions

File tree

api/src/org/labkey/api/attachments/AttachmentService.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,10 @@ static AttachmentService get()
140140

141141
HttpView<?> getFindAttachmentParentsView();
142142

143-
void logOrphanedAttachments();
143+
/**
144+
* Logs the first 20 orphaned attachments it detects. Returns the total number of orphaned attachments detected.
145+
*/
146+
int logOrphanedAttachments();
144147

145148
void deleteOrphanedAttachments();
146149

api/src/org/labkey/api/data/ContainerManager.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1944,7 +1944,9 @@ private static boolean delete(final Container c, User user, @Nullable String com
19441944
setContainerTabDeleted(c.getParent(), c.getName(), c.getParent().getFolderType().getName());
19451945
}
19461946

1947-
AttachmentService.get().logOrphanedAttachments();
1947+
// Log orphaned attachments in this server, but only in dev mode, since this is for our testing
1948+
if (AppProps.getInstance().isDevMode())
1949+
AttachmentService.get().logOrphanedAttachments();
19481950

19491951
fireDeleteContainer(c, user);
19501952

core/src/org/labkey/core/admin/AdminController.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3710,6 +3710,20 @@ public void addNavTrail(NavTree root)
37103710
}
37113711
}
37123712

3713+
@AdminConsoleAction
3714+
public static class LogOrphanedAttachmentsAction extends ReadOnlyApiAction<Object>
3715+
{
3716+
@Override
3717+
public Object execute(Object o, BindException errors) throws Exception
3718+
{
3719+
int count = 0;
3720+
AttachmentService svc = AttachmentService.get();
3721+
if (svc != null)
3722+
count = svc.logOrphanedAttachments();
3723+
return Map.of("count", count);
3724+
}
3725+
}
3726+
37133727
public static ActionURL getMemTrackerURL(boolean clearCaches, boolean gc)
37143728
{
37153729
ActionURL url = new ActionURL(MemTrackerAction.class, ContainerManager.getRoot());

core/src/org/labkey/core/attachment/AttachmentServiceImpl.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,10 +1101,10 @@ public int available()
11011101
private record Orphan(String documentName, String parentType){}
11021102

11031103
@Override
1104-
public void logOrphanedAttachments()
1104+
public int logOrphanedAttachments()
11051105
{
1106-
// Log orphaned attachments in this server, but in dev mode only, since this is for our testing. Also, we
1107-
// don't yet offer a way to delete orphaned attachments via the UI, so it's not helpful to inform admins.
1106+
int ret = 0;
1107+
11081108
if (AppProps.getInstance().isDevMode())
11091109
{
11101110
User user = ElevatedUser.getElevatedUser(User.getSearchUser(), TroubleshooterRole.class);
@@ -1118,7 +1118,8 @@ public void logOrphanedAttachments()
11181118
List<Orphan> orphans = new TableSelector(documents, new CsvSet("DocumentName, ParentType"), filter, null).getArrayList(Orphan.class);
11191119
if (!orphans.isEmpty())
11201120
{
1121-
LOG.error("Found {}, which likely indicates a problem with a delete method or a container listener.", StringUtilsLabKey.pluralize(orphans.size(), "orphaned attachment"));
1121+
ret = orphans.size();
1122+
LOG.error("Found {}, which likely indicates a problem with a delete method or a container listener.", StringUtilsLabKey.pluralize(ret, "orphaned attachment"));
11221123

11231124
final String message;
11241125
if (orphans.size() > MAX_ORPHANS_TO_LOG)
@@ -1136,6 +1137,8 @@ public void logOrphanedAttachments()
11361137
}
11371138
}
11381139
}
1140+
1141+
return ret;
11391142
}
11401143

11411144
record OrphanedAttachment(String container, String parent, String parentType, String documentName)

0 commit comments

Comments
 (0)