Skip to content

Commit 58f3205

Browse files
authored
Log orphaned attachments action for tests (#7556)
1 parent 9a63dde commit 58f3205

4 files changed

Lines changed: 47 additions & 28 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: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,41 +1101,41 @@ 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.
1108-
if (AppProps.getInstance().isDevMode())
1106+
int ret = 0;
1107+
User user = ElevatedUser.getElevatedUser(User.getSearchUser(), TroubleshooterRole.class);
1108+
UserSchema core = DefaultSchema.get(user, ContainerManager.getRoot()).getUserSchema(CoreQuerySchema.NAME);
1109+
1110+
if (core != null)
11091111
{
1110-
User user = ElevatedUser.getElevatedUser(User.getSearchUser(), TroubleshooterRole.class);
1111-
UserSchema core = DefaultSchema.get(user, ContainerManager.getRoot()).getUserSchema(CoreQuerySchema.NAME);
1112-
if (core != null)
1112+
TableInfo documents = core.getTable(CoreQuerySchema.DOCUMENTS_TABLE_NAME, new AllFolders(user));
1113+
if (null != documents)
11131114
{
1114-
TableInfo documents = core.getTable(CoreQuerySchema.DOCUMENTS_TABLE_NAME, new AllFolders(user));
1115-
if (null != documents)
1115+
SimpleFilter filter = new SimpleFilter(FieldKey.fromParts("Orphaned"), true);
1116+
List<Orphan> orphans = new TableSelector(documents, new CsvSet("DocumentName, ParentType"), filter, null).getArrayList(Orphan.class);
1117+
if (!orphans.isEmpty())
11161118
{
1117-
SimpleFilter filter = new SimpleFilter(FieldKey.fromParts("Orphaned"), true);
1118-
List<Orphan> orphans = new TableSelector(documents, new CsvSet("DocumentName, ParentType"), filter, null).getArrayList(Orphan.class);
1119-
if (!orphans.isEmpty())
1119+
ret = orphans.size();
1120+
LOG.error("Found {}, which likely indicates a problem with a delete method or a container listener.", StringUtilsLabKey.pluralize(ret, "orphaned attachment"));
1121+
1122+
final String message;
1123+
if (orphans.size() > MAX_ORPHANS_TO_LOG)
1124+
{
1125+
orphans = orphans.subList(0, MAX_ORPHANS_TO_LOG);
1126+
message = "The first " + MAX_ORPHANS_TO_LOG;
1127+
}
1128+
else
11201129
{
1121-
LOG.error("Found {}, which likely indicates a problem with a delete method or a container listener.", StringUtilsLabKey.pluralize(orphans.size(), "orphaned attachment"));
1122-
1123-
final String message;
1124-
if (orphans.size() > MAX_ORPHANS_TO_LOG)
1125-
{
1126-
orphans = orphans.subList(0, MAX_ORPHANS_TO_LOG);
1127-
message = "The first " + MAX_ORPHANS_TO_LOG;
1128-
}
1129-
else
1130-
{
1131-
message = "All";
1132-
}
1133-
1134-
LOG.error("{} detected orphans are listed below:\n{}", message, orphans.stream().map(Record::toString).collect(Collectors.joining("\n")));
1130+
message = "All";
11351131
}
1132+
1133+
LOG.error("{} detected orphans are listed below:\n{}", message, orphans.stream().map(Record::toString).collect(Collectors.joining("\n")));
11361134
}
11371135
}
11381136
}
1137+
1138+
return ret;
11391139
}
11401140

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

0 commit comments

Comments
 (0)