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

Git Backup Repository by configuration parameter backup-uri *use a backup git repository* #1220

Closed
wants to merge 7 commits into from

Conversation

matzegebbe
Copy link

Hi!

as already mentioned in the issue #617 we need a backup repository in worst case or if our bitbucket is not available and we do not already have a local copy of the remote repository.

We use the spring cloud Finchley SR2 release at the moment.

For that I created a new config param called backup-uri

image

That uri is used if the base uri is not working for the git checkout. After that a flag called backupRepoInUse is set. After a cool down timeout of 1 min the checkout will be retried by clone base dir.

the retry is implemented by

clone = this.gitFactory.getCloneCommandByCloneRepository()
                    .setURI(getBackupUri()).setDirectory(getBasedir());
            configureCommand(clone);

if an exception happened.

This is certainly not the best solution and you have to make sure that the data in the backup repository is up to date but it helps us a lot in our case of emergency

 private Git createGitClient() throws IOException, GitAPIException {
        File lock = new File(getWorkingDirectory(), ".git/index.lock");
        if (lock.exists()) {
            // The only way this can happen is if another JVM (e.g. one that
            // crashed earlier) created the lock. We can attempt to recover by
            // wiping the slate clean.
            logger.info("Deleting stale JGit lock file at " + lock);
            lock.delete();
        }

        if(backupRepoInUse){
            logger.info("We are running on the backup repo origin...");
        }

        // When we use the backup repo we try to check out the main repo after the cool down
        if (backupRepoInUse &&  (System.currentTimeMillis() - backupInUseTimestamp > mainRepoCheckCoolDown)) {
            logger.info("Retry to get the main repo running...");
            return copyRepository();
        } else if (new File(getWorkingDirectory(), ".git").exists()) {
            return openGitRepository();
        } else {
            return copyRepository();
        }
    }
    private Git cloneToBasedir() throws GitAPIException {
        CloneCommand clone = this.gitFactory.getCloneCommandByCloneRepository()
                .setURI(getUri()).setDirectory(getBasedir());
        configureCommand(clone);
        try {
            Git call = clone.call();
            backupRepoInUse = false;
            return call;
        } catch (GitAPIException e) {
            logger.warn("Error occured cloning to base directory.", e);
            // try with backup uri
            if (getBackupUri() == null || getBackupUri().isEmpty()) {
                deleteBaseDirIfExists();
                throw e;
            }
            clone = this.gitFactory.getCloneCommandByCloneRepository()
                    .setURI(getBackupUri()).setDirectory(getBasedir());
            configureCommand(clone);
            try {
                Git call = clone.call();
                backupRepoInUse = true;
                backupInUseTimestamp = System.currentTimeMillis();
                return call;
            } catch (GitAPIException e2) {
                logger.warn("Error occured cloning backup to base directory.", e2);
                deleteBaseDirIfExists();
                throw e2;
            }
        }
    }

Maybe we can see a similar feature in the future or this helps others.

@pivotal-issuemaster
Copy link

@matzegebbe Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

@ryanjbaxter
Copy link
Contributor

I think we would want to take this approach #617 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants