Skip to content

Commit

Permalink
Delete git repository and bundle directories on any errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kinow committed Jun 20, 2022
1 parent 27f69a9 commit 9516d2a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 16 deletions.
7 changes: 6 additions & 1 deletion src/main/java/org/commonwl/view/cwl/CWLToolRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.commonwl.view.git.GitSemaphore;
import org.commonwl.view.git.GitService;
import org.commonwl.view.researchobject.ROBundleFactory;
import org.commonwl.view.util.FileUtils;
import org.commonwl.view.workflow.QueuedWorkflow;
import org.commonwl.view.workflow.QueuedWorkflowRepository;
import org.commonwl.view.workflow.Workflow;
Expand Down Expand Up @@ -78,9 +79,10 @@ public void createWorkflowFromQueued(QueuedWorkflow queuedWorkflow)
GitDetails gitInfo = tempWorkflow.getRetrievedFrom();
final String repoUrl = gitInfo.getRepoUrl();
// Parse using cwltool and replace in database
Git repo = null;
try {
boolean safeToAccess = gitSemaphore.acquire(repoUrl);
Git repo = gitService.getRepository(gitInfo, safeToAccess);
repo = gitService.getRepository(gitInfo, safeToAccess);
Path localPath = repo.getRepository().getWorkTree().toPath();
Path workflowFile = localPath.resolve(gitInfo.getPath()).normalize().toAbsolutePath();
Workflow newWorkflow = cwlService.parseWorkflowWithCwltool(
Expand All @@ -106,16 +108,19 @@ public void createWorkflowFromQueued(QueuedWorkflow queuedWorkflow)
logger.error("Jena query exception ", ex);
queuedWorkflow.setCwltoolStatus(CWLToolStatus.ERROR);
queuedWorkflow.setMessage("An error occurred when executing a query on the SPARQL store");
FileUtils.deleteGitRepository(repo);
} catch (CWLValidationException ex) {
logger.error(ex.getMessage(), ex);
queuedWorkflow.setCwltoolStatus(CWLToolStatus.ERROR);
queuedWorkflow.setMessage(ex.getMessage());
FileUtils.deleteGitRepository(repo);
} catch (Exception ex) {
logger.error("Unexpected error", ex);
queuedWorkflow.setCwltoolStatus(CWLToolStatus.ERROR);
queuedWorkflow.setMessage("Whoops! Cwltool ran successfully, but an unexpected " +
"error occurred in CWLViewer!\n" +
"Help us by reporting it on Gitter or a Github issue\n");
FileUtils.deleteGitRepository(repo);
} finally {
gitSemaphore.release(repoUrl);
queuedWorkflowRepository.save(queuedWorkflow);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public Bundle createBundle(Workflow workflow, GitDetails gitInfo) throws IOExcep
Bundle bundle = Bundles.createBundle();
Manifest manifest = bundle.getManifest();

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

// Make a directory in the RO bundle to store the files
Path bundleRoot = bundle.getRoot();
Path bundlePath = bundleRoot.resolve("workflow");
bundlePath = bundleRoot.resolve("workflow");
Files.createDirectory(bundlePath);

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

boolean safeToAccess = gitSemaphore.acquire(gitInfo.getRepoUrl());
Git gitRepo = null;
try {
Git gitRepo = gitService.getRepository(workflow.getRetrievedFrom(), safeToAccess);
gitRepo = gitService.getRepository(workflow.getRetrievedFrom(), safeToAccess);
Path relativePath = Paths.get(FilenameUtils.getPath(gitInfo.getPath()));
Path gitPath = gitRepo.getRepository().getWorkTree().toPath().resolve(relativePath);
addFilesToBundle(gitInfo, bundle, bundlePath, gitRepo, gitPath, authors, workflow);
} catch (GitAPIException | IOException e) {
org.commonwl.view.util.FileUtils.deleteGitRepository(gitRepo);
FileUtils.forceDelete(bundlePath.toFile());
throw e;
} finally {
gitSemaphore.release(gitInfo.getRepoUrl());
}
Expand Down Expand Up @@ -217,6 +223,7 @@ public Bundle createBundle(Workflow workflow, GitDetails gitInfo) throws IOExcep
logger.error("Error creating URI for RO Bundle", ex);
} catch (GitAPIException ex) {
logger.error("Error getting repository to create RO Bundle", ex);
FileUtils.forceDelete(bundlePath.toFile());
}

// Return the completed bundle
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/org/commonwl/view/util/FileUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.commonwl.view.util;

import org.eclipse.jgit.api.Git;

import java.io.IOException;

/**
* File utilities for CWL Viewer.
*
* <p>Uses other utilities, such as Apache Commons IO's {@code FileUtils}, but
* with refinements specific for CWL Viewer (e.g. handling Git repositories).</p>
*/
public class FileUtils {

private FileUtils() {}

public static void deleteGitRepository(Git repo) throws IOException {
if (repo != null && repo.getRepository() != null && repo.getRepository().getDirectory().exists()) {
org.apache.commons.io.FileUtils.forceDelete(repo.getRepository().getDirectory());
}
}
}
31 changes: 18 additions & 13 deletions src/main/java/org/commonwl/view/workflow/WorkflowService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,6 @@

package org.commonwl.view.workflow;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import org.commonwl.view.cwl.CWLService;
import org.commonwl.view.cwl.CWLToolRunner;
import org.commonwl.view.cwl.CWLToolStatus;
Expand All @@ -40,6 +28,7 @@
import org.commonwl.view.graphviz.GraphVizService;
import org.commonwl.view.researchobject.ROBundleFactory;
import org.commonwl.view.researchobject.ROBundleNotFoundException;
import org.commonwl.view.util.FileUtils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.RefNotFoundException;
Expand All @@ -52,6 +41,18 @@
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Service
public class WorkflowService {

Expand Down Expand Up @@ -295,9 +296,9 @@ public QueuedWorkflow createQueuedWorkflow(GitDetails gitInfo)
throws GitAPIException, WorkflowNotFoundException, IOException {
QueuedWorkflow queuedWorkflow;

Git repo = null;
try {
boolean safeToAccess = gitSemaphore.acquire(gitInfo.getRepoUrl());
Git repo = null;
while (repo == null) {
try {
repo = gitService.getRepository(gitInfo, safeToAccess);
Expand Down Expand Up @@ -366,6 +367,10 @@ public QueuedWorkflow createQueuedWorkflow(GitDetails gitInfo)
logger.error("Could not update workflow with cwltool", e);
}

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

0 comments on commit 9516d2a

Please sign in to comment.