Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed an error that caused workdirs to become locked and unusable #314

Merged
merged 4 commits into from
Jan 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import io.reflectoring.coderadar.analyzer.port.driven.ResetAnalysisPort;
import io.reflectoring.coderadar.projectadministration.ModuleAlreadyExistsException;
import io.reflectoring.coderadar.projectadministration.ModulePathInvalidException;
import io.reflectoring.coderadar.projectadministration.ProjectNotFoundException;
import io.reflectoring.coderadar.projectadministration.domain.Commit;
import io.reflectoring.coderadar.projectadministration.domain.Module;
import io.reflectoring.coderadar.projectadministration.domain.Project;
Expand Down Expand Up @@ -35,6 +34,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.stereotype.Component;

Expand All @@ -56,6 +56,7 @@ public class ScanProjectScheduler {
private final SaveCommitPort saveCommitPort;
private final ResetAnalysisPort resetAnalysisPort;
private final UpdateProjectPort updateProjectPort;
private final TaskExecutor taskExecutor;

private final Logger logger = LoggerFactory.getLogger(ScanProjectScheduler.class);

Expand All @@ -76,7 +77,8 @@ public ScanProjectScheduler(
DeleteModulePort deleteModulePort,
SaveCommitPort saveCommitPort,
ResetAnalysisPort resetAnalysisPort,
UpdateProjectPort updateProjectPort) {
UpdateProjectPort updateProjectPort,
TaskExecutor taskExecutor) {
this.updateRepositoryUseCase = updateRepositoryUseCase;
this.coderadarConfigurationProperties = coderadarConfigurationProperties;
this.extractProjectCommitsUseCase = extractProjectCommitsUseCase;
Expand All @@ -92,6 +94,7 @@ public ScanProjectScheduler(
this.saveCommitPort = saveCommitPort;
this.resetAnalysisPort = resetAnalysisPort;
this.updateProjectPort = updateProjectPort;
this.taskExecutor = taskExecutor;
}

/** Starts the scheduleCheckTask tasks upon application start */
Expand Down Expand Up @@ -121,18 +124,24 @@ private void scheduleUpdateTask(Long projectId) {
tasks.put(
projectId,
taskScheduler.scheduleAtFixedRate(
() -> {
try {
Project currentProject = getProjectPort.get(projectId);
if (!projectStatusPort.isBeingProcessed(projectId)
&& !checkProjectDate(currentProject)) {
logger.info("Scanning project {} for new commits!", currentProject.getName());
checkForNewCommits(currentProject);
}
} catch (ProjectNotFoundException e) {
stopUpdateTask(projectId);
}
},
() ->
taskExecutor.execute(
() -> {
try {
Project currentProject = getProjectPort.get(projectId);
if (!projectStatusPort.isBeingProcessed(projectId)
&& !checkProjectDate(currentProject)) {
projectStatusPort.setBeingProcessed(projectId, true);
logger.info(
"Scanning project {} for new commits!", currentProject.getName());
checkForNewCommits(currentProject);
projectStatusPort.setBeingProcessed(projectId, false);
}
} catch (Exception e) {
projectStatusPort.setBeingProcessed(projectId, false);
stopUpdateTask(projectId);
}
}),
coderadarConfigurationProperties.getScanIntervalInSeconds() * 1000L));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package io.reflectoring.coderadar.vcs.adapter;

import io.reflectoring.coderadar.vcs.UnableToCloneRepositoryException;
import io.reflectoring.coderadar.vcs.UnableToUpdateRepositoryException;
import io.reflectoring.coderadar.vcs.port.driven.UpdateRepositoryPort;
import io.reflectoring.coderadar.vcs.port.driver.clone.CloneRepositoryCommand;
import io.reflectoring.coderadar.vcs.port.driver.update.UpdateRepositoryCommand;
import java.io.IOException;
import java.nio.file.Path;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.eclipse.jgit.api.FetchCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ResetCommand;
import org.eclipse.jgit.api.errors.CheckoutConflictException;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
Expand All @@ -21,6 +25,12 @@
@Service
public class UpdateRepositoryAdapter implements UpdateRepositoryPort {

private final CloneRepositoryAdapter cloneRepositoryAdapter;

public UpdateRepositoryAdapter(CloneRepositoryAdapter cloneRepositoryAdapter) {
this.cloneRepositoryAdapter = cloneRepositoryAdapter;
}

@Override
public boolean updateRepository(UpdateRepositoryCommand command)
throws UnableToUpdateRepositoryException {
Expand All @@ -32,10 +42,10 @@ public boolean updateRepository(UpdateRepositoryCommand command)
try {
resetRepository(command.getLocalDir().toPath());
return updateInternal(command);
} catch (IOException | GitAPIException ex) {
} catch (IOException | GitAPIException | UnableToCloneRepositoryException ex) {
throw createException(command.getLocalDir().toPath(), e.getMessage());
}
} catch (IOException | GitAPIException e) {
} catch (IOException | GitAPIException | UnableToCloneRepositoryException e) {
throw createException(command.getLocalDir().toPath(), e.getMessage());
}
}
Expand All @@ -47,11 +57,16 @@ private UnableToUpdateRepositoryException createException(Path repositoryRoot, S
}

private boolean updateInternal(UpdateRepositoryCommand command)
throws GitAPIException, IOException {
throws GitAPIException, IOException, UnableToCloneRepositoryException {
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repository = builder.setWorkTree(command.getLocalDir()).build();
Git git = new Git(repository);
ObjectId oldHead = git.getRepository().resolve(Constants.HEAD);
if (oldHead == null) {
git.close();
deleteAndCloneRepository(command);
return true;
}
StoredConfig config = git.getRepository().getConfig();
config.setString("remote", "origin", "url", command.getRemoteUrl());
config.save();
Expand All @@ -61,12 +76,29 @@ private boolean updateInternal(UpdateRepositoryCommand command)
new UsernamePasswordCredentialsProvider(command.getUsername(), command.getPassword()));
}
fetchCommand.call();
git.checkout().setName("origin/master").setForce(true).call();
try {
git.checkout().setName("origin/master").setForce(true).call();
} catch (JGitInternalException e) {
git.close();
deleteAndCloneRepository(command);
return false;
}
git.reset().setMode(ResetCommand.ResetType.HARD).setRef("origin/master").call();
ObjectId newHead = git.getRepository().resolve(Constants.HEAD);
git.getRepository().close();
git.close();
return (oldHead == null && newHead != null) || (oldHead != null && !oldHead.equals(newHead));
return !oldHead.equals(newHead);
}

private void deleteAndCloneRepository(UpdateRepositoryCommand command)
throws IOException, UnableToCloneRepositoryException {
FileUtils.forceDelete(command.getLocalDir());
cloneRepositoryAdapter.cloneRepository(
new CloneRepositoryCommand(
command.getRemoteUrl(),
command.getLocalDir(),
command.getUsername(),
command.getPassword()));
}

private void resetRepository(Path repositoryRoot) throws IOException, GitAPIException {
Expand Down