Skip to content

Commit 9516d2a

Browse files
committed
Delete git repository and bundle directories on any errors
1 parent 27f69a9 commit 9516d2a

File tree

4 files changed

+55
-16
lines changed

4 files changed

+55
-16
lines changed

src/main/java/org/commonwl/view/cwl/CWLToolRunner.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.commonwl.view.git.GitSemaphore;
2525
import org.commonwl.view.git.GitService;
2626
import org.commonwl.view.researchobject.ROBundleFactory;
27+
import org.commonwl.view.util.FileUtils;
2728
import org.commonwl.view.workflow.QueuedWorkflow;
2829
import org.commonwl.view.workflow.QueuedWorkflowRepository;
2930
import org.commonwl.view.workflow.Workflow;
@@ -78,9 +79,10 @@ public void createWorkflowFromQueued(QueuedWorkflow queuedWorkflow)
7879
GitDetails gitInfo = tempWorkflow.getRetrievedFrom();
7980
final String repoUrl = gitInfo.getRepoUrl();
8081
// Parse using cwltool and replace in database
82+
Git repo = null;
8183
try {
8284
boolean safeToAccess = gitSemaphore.acquire(repoUrl);
83-
Git repo = gitService.getRepository(gitInfo, safeToAccess);
85+
repo = gitService.getRepository(gitInfo, safeToAccess);
8486
Path localPath = repo.getRepository().getWorkTree().toPath();
8587
Path workflowFile = localPath.resolve(gitInfo.getPath()).normalize().toAbsolutePath();
8688
Workflow newWorkflow = cwlService.parseWorkflowWithCwltool(
@@ -106,16 +108,19 @@ public void createWorkflowFromQueued(QueuedWorkflow queuedWorkflow)
106108
logger.error("Jena query exception ", ex);
107109
queuedWorkflow.setCwltoolStatus(CWLToolStatus.ERROR);
108110
queuedWorkflow.setMessage("An error occurred when executing a query on the SPARQL store");
111+
FileUtils.deleteGitRepository(repo);
109112
} catch (CWLValidationException ex) {
110113
logger.error(ex.getMessage(), ex);
111114
queuedWorkflow.setCwltoolStatus(CWLToolStatus.ERROR);
112115
queuedWorkflow.setMessage(ex.getMessage());
116+
FileUtils.deleteGitRepository(repo);
113117
} catch (Exception ex) {
114118
logger.error("Unexpected error", ex);
115119
queuedWorkflow.setCwltoolStatus(CWLToolStatus.ERROR);
116120
queuedWorkflow.setMessage("Whoops! Cwltool ran successfully, but an unexpected " +
117121
"error occurred in CWLViewer!\n" +
118122
"Help us by reporting it on Gitter or a Github issue\n");
123+
FileUtils.deleteGitRepository(repo);
119124
} finally {
120125
gitSemaphore.release(repoUrl);
121126
queuedWorkflowRepository.save(queuedWorkflow);

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ public Bundle createBundle(Workflow workflow, GitDetails gitInfo) throws IOExcep
128128
Bundle bundle = Bundles.createBundle();
129129
Manifest manifest = bundle.getManifest();
130130

131+
Path bundlePath = null;
131132
// Simplified attribution for RO bundle
132133
try {
133134
manifest.setId(new URI(workflow.getPermalink()));
@@ -143,18 +144,23 @@ public Bundle createBundle(Workflow workflow, GitDetails gitInfo) throws IOExcep
143144

144145
// Make a directory in the RO bundle to store the files
145146
Path bundleRoot = bundle.getRoot();
146-
Path bundlePath = bundleRoot.resolve("workflow");
147+
bundlePath = bundleRoot.resolve("workflow");
147148
Files.createDirectory(bundlePath);
148149

149150
// Add the files from the repo to this workflow
150151
Set<HashableAgent> authors = new HashSet<>();
151152

152153
boolean safeToAccess = gitSemaphore.acquire(gitInfo.getRepoUrl());
154+
Git gitRepo = null;
153155
try {
154-
Git gitRepo = gitService.getRepository(workflow.getRetrievedFrom(), safeToAccess);
156+
gitRepo = gitService.getRepository(workflow.getRetrievedFrom(), safeToAccess);
155157
Path relativePath = Paths.get(FilenameUtils.getPath(gitInfo.getPath()));
156158
Path gitPath = gitRepo.getRepository().getWorkTree().toPath().resolve(relativePath);
157159
addFilesToBundle(gitInfo, bundle, bundlePath, gitRepo, gitPath, authors, workflow);
160+
} catch (GitAPIException | IOException e) {
161+
org.commonwl.view.util.FileUtils.deleteGitRepository(gitRepo);
162+
FileUtils.forceDelete(bundlePath.toFile());
163+
throw e;
158164
} finally {
159165
gitSemaphore.release(gitInfo.getRepoUrl());
160166
}
@@ -217,6 +223,7 @@ public Bundle createBundle(Workflow workflow, GitDetails gitInfo) throws IOExcep
217223
logger.error("Error creating URI for RO Bundle", ex);
218224
} catch (GitAPIException ex) {
219225
logger.error("Error getting repository to create RO Bundle", ex);
226+
FileUtils.forceDelete(bundlePath.toFile());
220227
}
221228

222229
// Return the completed bundle
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.commonwl.view.util;
2+
3+
import org.eclipse.jgit.api.Git;
4+
5+
import java.io.IOException;
6+
7+
/**
8+
* File utilities for CWL Viewer.
9+
*
10+
* <p>Uses other utilities, such as Apache Commons IO's {@code FileUtils}, but
11+
* with refinements specific for CWL Viewer (e.g. handling Git repositories).</p>
12+
*/
13+
public class FileUtils {
14+
15+
private FileUtils() {}
16+
17+
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());
20+
}
21+
}
22+
}

src/main/java/org/commonwl/view/workflow/WorkflowService.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,6 @@
1919

2020
package org.commonwl.view.workflow;
2121

22-
import java.io.File;
23-
import java.io.IOException;
24-
import java.nio.file.Files;
25-
import java.nio.file.Path;
26-
import java.nio.file.Paths;
27-
import java.util.ArrayList;
28-
import java.util.Calendar;
29-
import java.util.Date;
30-
import java.util.List;
31-
import java.util.Objects;
32-
import java.util.Optional;
33-
3422
import org.commonwl.view.cwl.CWLService;
3523
import org.commonwl.view.cwl.CWLToolRunner;
3624
import org.commonwl.view.cwl.CWLToolStatus;
@@ -40,6 +28,7 @@
4028
import org.commonwl.view.graphviz.GraphVizService;
4129
import org.commonwl.view.researchobject.ROBundleFactory;
4230
import org.commonwl.view.researchobject.ROBundleNotFoundException;
31+
import org.commonwl.view.util.FileUtils;
4332
import org.eclipse.jgit.api.Git;
4433
import org.eclipse.jgit.api.errors.GitAPIException;
4534
import org.eclipse.jgit.api.errors.RefNotFoundException;
@@ -52,6 +41,18 @@
5241
import org.springframework.data.domain.Pageable;
5342
import org.springframework.stereotype.Service;
5443

44+
import java.io.File;
45+
import java.io.IOException;
46+
import java.nio.file.Files;
47+
import java.nio.file.Path;
48+
import java.nio.file.Paths;
49+
import java.util.ArrayList;
50+
import java.util.Calendar;
51+
import java.util.Date;
52+
import java.util.List;
53+
import java.util.Objects;
54+
import java.util.Optional;
55+
5556
@Service
5657
public class WorkflowService {
5758

@@ -295,9 +296,9 @@ public QueuedWorkflow createQueuedWorkflow(GitDetails gitInfo)
295296
throws GitAPIException, WorkflowNotFoundException, IOException {
296297
QueuedWorkflow queuedWorkflow;
297298

299+
Git repo = null;
298300
try {
299301
boolean safeToAccess = gitSemaphore.acquire(gitInfo.getRepoUrl());
300-
Git repo = null;
301302
while (repo == null) {
302303
try {
303304
repo = gitService.getRepository(gitInfo, safeToAccess);
@@ -366,6 +367,10 @@ public QueuedWorkflow createQueuedWorkflow(GitDetails gitInfo)
366367
logger.error("Could not update workflow with cwltool", e);
367368
}
368369

370+
} catch (GitAPIException | RuntimeException | IOException e) {
371+
logger.warn(String.format("Failed to create Queued Workflow: %s - Temporary files will be deleted", e.getMessage()), e);
372+
FileUtils.deleteGitRepository(repo);
373+
throw e;
369374
} finally {
370375
gitSemaphore.release(gitInfo.getRepoUrl());
371376
}

0 commit comments

Comments
 (0)