Skip to content

Commit 6c63ad7

Browse files
kinowmr-c
authored andcommitted
Remove Bundle and Git directories when they are not necessary (e.g. process succeeed and they are not used, or process failed).
1 parent 3759a12 commit 6c63ad7

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

src/main/java/org/commonwl/view/Scheduler.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,10 @@
1919
import java.nio.file.Paths;
2020
import java.time.Duration;
2121
import java.time.Instant;
22-
import java.util.Arrays;
2322
import java.util.Calendar;
2423
import java.util.Collections;
2524
import java.util.Date;
26-
import java.util.HashSet;
2725
import java.util.List;
28-
import java.util.Set;
2926
import java.util.stream.Stream;
3027

3128
/**
@@ -43,8 +40,9 @@ public class Scheduler {
4340
@Value("${tmpDirAgeLimitDays}")
4441
private Integer TMP_DIR_AGE_LIMIT_DAYS;
4542

46-
@Value("${bundleStorage}")
47-
private String bundleStorage;
43+
// We do not want to remove the bundles, as we use the disk as a sort of
44+
// cache. Whenever a workflow page is displayed in the browser the UI
45+
// fires a request to re-generate it. We skip that by keeping files on disk.
4846
@Value("${graphvizStorage}")
4947
private String graphvizStorage;
5048
@Value("${gitStorage}")
@@ -102,7 +100,7 @@ public void removeOldQueuedWorkflowEntries() {
102100
public void clearTmpDir() {
103101
// Temporary files used for graphviz, RO, and git may be stored in different
104102
// locations, so we will collect all of them here.
105-
List<String> temporaryDirectories = Stream.of(bundleStorage, graphvizStorage, gitStorage)
103+
List<String> temporaryDirectories = Stream.of(graphvizStorage, gitStorage)
106104
.distinct()
107105
.toList();
108106
temporaryDirectories.forEach(this::clearDirectory);
@@ -130,7 +128,7 @@ private void clearDirectory(String temporaryDirectory) {
130128
// Really unexpected. walkFileTree should throw an IllegalArgumentException for negative maxDepth (clearly
131129
// not happening here), a SecurityException if the security manager denies access, or this IOException in
132130
// the cases where an I/O error happened (disk error, OS error, file not found, etc.). So just a warning.
133-
logger.warn(String.format("Unexpected I/O error was thrown walking directory [%s]: %s", dir.toString(), e.getMessage()), e);
131+
logger.warn(String.format("Unexpected I/O error was thrown walking directory [%s]: %s", dir, e.getMessage()), e);
134132
}
135133

136134
// Delete the directories accumulated by the visitor.

src/main/java/org/commonwl/view/researchobject/ROBundleFactory.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919

2020
package org.commonwl.view.researchobject;
2121

22+
import org.apache.commons.io.FileUtils;
2223
import org.apache.commons.io.FilenameUtils;
2324
import org.apache.taverna.robundle.Bundle;
25+
import org.apache.taverna.robundle.fs.BundleFileSystem;
2426
import org.commonwl.view.git.GitDetails;
2527
import org.commonwl.view.workflow.Workflow;
2628
import org.commonwl.view.workflow.WorkflowRepository;
@@ -31,7 +33,9 @@
3133
import org.springframework.scheduling.annotation.EnableAsync;
3234
import org.springframework.stereotype.Component;
3335

36+
import java.io.File;
3437
import java.io.IOException;
38+
import java.nio.file.FileSystem;
3539
import java.nio.file.Path;
3640

3741
/**
@@ -77,6 +81,26 @@ public void createWorkflowRO(Workflow workflow)
7781

7882
// Save the bundle to the storage location in properties
7983
Path bundleLocation = roBundleService.saveToFile(bundle);
84+
// The RoBundleService#saveToFile call above will delegate to Taverna's
85+
// Bundles.closeAndSaveBundle, which empties the bundle temporary
86+
// directory without deleting the directory itself. The following call is
87+
// just for cleaning it up.
88+
if (bundle != null) {
89+
try {
90+
BundleFileSystem fs = (BundleFileSystem) bundle.getFileSystem();
91+
File tmpDir = new File(System.getProperty("java.io.tmpdir"));
92+
// The file system source will be something like /tmp/bundles_dir/hash/bundle.zip,
93+
// and we want /tmp/hash to be deleted. N.B. Taverna uses the temporary directory
94+
// for the temporary bundles' directory, not the bundles file specified by Spring.
95+
String bundleTmpDirName = fs.getSource().toAbsolutePath().getParent().getFileName().toString();
96+
File bundleTmpDir = new File(tmpDir, bundleTmpDirName);
97+
if (bundleTmpDir.exists() && bundleTmpDir.isDirectory()) {
98+
FileUtils.forceDelete(bundleTmpDir);
99+
}
100+
} catch (IOException e) {
101+
logger.warn(String.format("Failed to delete temporary directory for bundle [%s]: %s", bundle.getSource(), e.getMessage()), e);
102+
}
103+
}
80104

81105
// Add RO Bundle to associated workflow model
82106
workflow.setRoBundlePath(bundleLocation.toString());

src/main/java/org/commonwl/view/researchobject/ROBundleService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ public Bundle createBundle(Workflow workflow, GitDetails gitInfo) throws IOExcep
163163
throw e;
164164
} finally {
165165
gitSemaphore.release(gitInfo.getRepoUrl());
166+
org.commonwl.view.util.FileUtils.deleteGitRepository(gitRepo);
166167
}
167168

168169
// Add combined authors
@@ -221,6 +222,9 @@ public Bundle createBundle(Workflow workflow, GitDetails gitInfo) throws IOExcep
221222

222223
} catch (URISyntaxException ex) {
223224
logger.error("Error creating URI for RO Bundle", ex);
225+
if (bundlePath != null) {
226+
FileUtils.forceDelete(bundlePath.toFile());
227+
}
224228
} catch (GitAPIException ex) {
225229
logger.error("Error getting repository to create RO Bundle", ex);
226230
FileUtils.forceDelete(bundlePath.toFile());

src/main/java/org/commonwl/view/util/FileUtils.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.eclipse.jgit.api.Git;
44

5+
import java.io.File;
56
import java.io.IOException;
67

78
/**
@@ -15,8 +16,16 @@ public class FileUtils {
1516
private FileUtils() {}
1617

1718
public static void deleteGitRepository(Git repo) throws IOException {
18-
if (repo != null && repo.getRepository() != null && repo.getRepository().getDirectory().exists()) {
19-
org.apache.commons.io.FileUtils.forceDelete(repo.getRepository().getDirectory());
19+
if (
20+
repo != null &&
21+
repo.getRepository() != null &&
22+
repo.getRepository().getDirectory() != null &&
23+
repo.getRepository().getDirectory().exists()
24+
) {
25+
// This is literally the git directory, i.e. /some/hierarchy/repository/.git,
26+
// but we want to delete its parent directory.
27+
File gitDirectory = repo.getRepository().getDirectory();
28+
org.apache.commons.io.FileUtils.forceDelete(gitDirectory.getParentFile());
2029
}
2130
}
2231
}

0 commit comments

Comments
 (0)