0

Server: Atlassian Bitbucket v4.14.3 (aka Stash) Client: git version 2.17.1 (on Linux Ubuntu 18LTS)

What is the best way to detect whether the GIT server is alive and providing service?

Use case1: On a daily basis, the Bitbucket server goes down to perform a full backup. During the backup period, it doesn't respond to requests, and any 'git clone and git push' command fails (we issue those commands from scripts). Before issuing a 'clone' or a 'push' , I'd like to know whether the GIT server is in production to avoid an error.

Use case2: Once in a while, the Bitbucket server is brought down (for upgrade, some maintenance, etc). Similar as the situation above, I'd like to know whether it is in production before issuing a 'clone' or ' push' to avoid an error.

Does Bitbucket server have some API to check that? Does the git client have a command to check that? (I'd prefer this option , to avoid dependence on the server)

I've tried 'git ls-remote -h ssh:/reponame --exit-code' , but I'd like to know whether there is a better option where I don't have to provide a repository name.

freeAR
  • 943
  • 3
  • 18
  • 32

2 Answers2

1

You can issue just about any API command you like to check if the service is up. For example, you could get info for the current user. That doesn't mean the API will still be up a moment later.

"Check then do" doesn't work. Consider the following scenario.

  1. Check if Bitbucket is up.
  2. It's up!
  3. Bitbucket goes down.
  4. Issue Bitbucket command.
  5. Command fails.

This is a common theme in computer science. Other examples include...

  1. Check if a file exists.
  2. It exists!
  3. File gets deleted.
  4. Try to open file.
  5. Open fails.

You want to check if it's up and issue the command in one go, known as "atomic" because the two operations cannot be separated. In the case of a file, you just try to open the file and look for an error. For Git, issue the git command and check whether it succeeded.

For an entire process which might require multiple commands, safest thing to do would be to write a robust system that can deal with errors at any point in the process. This means being able to automatically retry failed network calls such as git pull, and to be able to restart a process which failed in the middle.

Schwern
  • 153,029
  • 25
  • 195
  • 336
-1

Here's how I ended up handling it (note that as per comment bellow, the best way is checking for these errors after issuing the desired git command in order to make it an atomic operation):

1- Execute 'git ls-remote' with a repository name , and detect if it returns a 0 (no issue), or something else. The 'something else' value I've got is 128.

2a- If 'ls-remote' returned something other than zero, make sure that the server is reachable. Look for 'Git server not found:'

3b- elif, check if the GIT server is in down in maintenance mode. Since my server is Bitbucket, I look for 'Bitbucket is currently unavailable'.

3c- elif, check if GIT server is rebooting. I get 'Connection refused' during the reboot.

3d- elif, confirm that the repository exists. An stderr with 'Repository not found' gives a hint.

3- Execute the desired git command (and check for errors afterwards).

freeAR
  • 943
  • 3
  • 18
  • 32