Monday 12 July 2021

How to debug PyGitHub not being responsive?

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?



from How to debug PyGitHub not being responsive?

No comments:

Post a Comment