2

I'm using PyGitHub to update my GitHub.com repos from inside a Ubuntu server using a python script.

I have noticed there are times, when my script just hangs there and there's no error message indicating what went wrong.

This is my script

from pathlib import Path
from typing import Optional

import typer
from github import Github, GithubException

app = typer.Typer()


@app.command()
def add_deploy_key(
    token: Optional[str] = None, user_repo: Optional[str] = None, repo_name: Optional[str] = None
):
    typer.echo("Starting to access GitHub.com... ")
    try:
        # using an access token
        g = Github(token)

        # I skipped a bunch of code to save space
        
        for key in repo.get_keys():
            if str(key.key) == str(pure_public_key):
                typer.echo(
                    "We found an existing public key in " + user_repo + ", so we're NOT adding it"
                )
                return
        rep_key = repo.create_key(
            "DigitalOcean for " + repo_name, current_public_key, read_only=True
        )
        if rep_key:
            typer.echo("Success with adding public key to repo in GitHub.com!")
            typer.echo("")
            typer.echo("The url to the deposited key is: " + rep_key.url)
        else:
            typer.echo("There's some issue when adding public key to repo in GitHub.com")
    except GithubException as e:
        typer.echo("There's some issue")
        typer.echo(str(e))
        return


if __name__ == "__main__":
    app()

The way I trigger is inside a bash script

output=$(python /opt/github-add-deploy-keys.py --token="$token" --user-repo="$user_repo" --repo-name="$repo_name")

It works. But sometimes it just hangs there without any output. And since it happens intermittently and not consistently, it's hard to debug.

I cannot be sure if it's a typer issue or a network issue or a GitHub.com issue. There's just nothing.

I want it to fail fast and often. I know there's a timeout and a retry for the GitHub object.

See https://pygithub.readthedocs.io/en/latest/github.html?highlight=retry#github.MainClass.Github

I wonder if I can do anything with these two parameters so at least I have a visual of knowing something is being done. I can add a lot of typer.echo statements but that would be extremely verbose.

I'm also unfamiliar with the retry object. I wish even if a retry is made, there will be some echo statements to tell me a retry is being attempted.

What can I try?

Kim Stacks
  • 10,202
  • 35
  • 151
  • 282
  • 2
    Rate limiting may be the issue. https://docs.github.com/en/rest/overview/resources-in-the-rest-api#rate-limiting – Nizam Mohamed Jul 15 '21 at 08:12
  • Yeah, rate limiting could be a problem; GitHub allows up to 60 reqs per hour unauthenticated, although I don't see why that would cause a hang unless your 3rd-party libs are badly written. With a PAT (personal access token), you can make up to 5,000 requests per hour, so I'd definitely do that if you're doing real work with the API. **Generate a token [here](https://github.com/settings/tokens/new?scopes=repo&description=Token%20for%20PyGitHub).** You can pass it to your `Github` instance like this: `github.Github(login_or_token="........")`. –  Jul 15 '21 at 21:36

1 Answers1

0

The timeout should prevent the github request to hang and retries should make it work but based on the documentation there is a default timeout already. Since this question is for debugging I would suggest using the logging python library to just the steps your script runs in a file. You can find a good logging tutorial here.

As for the logging style, since your case has a lot of unknowns I would log when the script starts, before "Create Key" step and after "Create Key" an maybe in case of errors. You can go over the log file when the script hangs.

You can create a bash script to run your script with timeout and get a notification if the program exits with non zero exit code and leave it to run over night:

timeout 5 python <yourscript>.py
Mihai
  • 831
  • 6
  • 13