WorkspaceImpl implements Workspace { @Override public void cleanup(final int maxAgeInSeconds) { if (maxAgeInSeconds < 0) { logger.debug("Canceling cleanup of workspace due to maxAge ({}) <= 0", maxAgeInSeconds); return; } if (maxAgeInSeconds < 60 * 60 * 24 * 2) { logger.warn("The max age for the workspace cleaner is dangerously low. Please consider increasing the value to " + "avoid deleting data in use by running workflows."); } final File workspaceDirectory = new File(wsRoot); logger.info("Starting cleanup of workspace at {}", workspaceDirectory); long now = new Date().getTime(); for (File file: FileUtils.listFiles(workspaceDirectory, null, true)) { long fileLastModified = file.lastModified(); long fileAgeInSeconds = (now - fileLastModified) / 1000; if (fileLastModified == 0 || fileAgeInSeconds < maxAgeInSeconds) { logger.debug("File age ({}) < max age ({}) or unknown: Skipping {} ", fileAgeInSeconds, maxAgeInSeconds, file); continue; } if (FileUtils.deleteQuietly(file)) { logger.info("Deleted {}", file); } else { logger.warn("Could not delete {}", file); } } logger.info("Finished cleanup of workspace"); } WorkspaceImpl();  WorkspaceImpl(String rootDirectory, boolean waitForResource); @Activate void activate(ComponentContext cc); @Deactivate void deactivate(); @Override File get(final URI uri); @Override File get(final URI uri, final boolean uniqueFilename); @Override InputStream read(final URI uri); @Override void delete(URI uri); @Override void delete(String mediaPackageID, String mediaPackageElementID); @Override URI put(String mediaPackageID, String mediaPackageElementID, String fileName, InputStream in); @Override URI putInCollection(String collectionId, String fileName, InputStream in); @Override URI getURI(String mediaPackageID, String mediaPackageElementID); @Override URI getCollectionURI(String collectionID, String fileName); @Override URI moveTo(URI collectionURI, String toMediaPackage, String toMediaPackageElement, String toFileName); @Override URI[] getCollectionContents(String collectionId); @Override void deleteFromCollection(String collectionId, String fileName); @Override Option<Long> getTotalSpace(); @Override Option<Long> getUsableSpace(); @Override Option<Long> getUsedSpace(); @Override URI getBaseUri(); @Reference(name = "REPO") void setRepository(WorkingFileRepository repo); @Reference(name = "trustedHttpClient") void setTrustedHttpClient(TrustedHttpClient trustedHttpClient); @Reference(name = "securityService") void setSecurityService(SecurityService securityService); @Override void cleanup(final int maxAgeInSeconds); @Override void cleanup(Id mediaPackageId); @Override void cleanup(Id mediaPackageId, boolean filesOnly); @Override String rootDirectory(); static final String WORKSPACE_DIR_KEY; static final String STORAGE_DIR_KEY; static final String WORKSPACE_CLEANUP_PERIOD_KEY; static final String WORKSPACE_CLEANUP_MAX_AGE_KEY; }
@Test public void testCleanup() throws Exception { workspace.cleanup(-1); Assert.assertEquals(0L, workspace.getUsedSpace().get().longValue()); File file = new File(PathSupport.concat(new String[] { workspaceRoot, "test", "c1", "bar.mov" })); FileUtils.write(file, "asdf", StandardCharsets.UTF_8); file.deleteOnExit(); Assert.assertEquals(4L, workspace.getUsedSpace().get().longValue()); workspace.cleanup(0); Assert.assertEquals(0L, workspace.getUsedSpace().get().longValue()); FileUtils.write(file, "asdf", StandardCharsets.UTF_8); Assert.assertEquals(4L, workspace.getUsedSpace().get().longValue()); workspace.cleanup(100); Assert.assertEquals(4L, workspace.getUsedSpace().get().longValue()); Prelude.sleep(1100L); workspace.cleanup(1); Assert.assertEquals(0L, workspace.getUsedSpace().get().longValue()); }