我正在使用PyGitHub通过python脚本从Ubuntu服务器内部更新我的GitHub.com代码库。
我注意到有些时候,我的脚本只是挂在那里,没有错误消息指出哪里出了问题。
这是我的脚本
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()我触发的方式是在bash脚本中
output=$(python /opt/github-add-deploy-keys.py --token="$token" --user-repo="$user_repo" --repo-name="$repo_name")它起作用了。但有时它只是挂在那里,没有任何输出。由于它是间歇性的,而且不是持续的,所以很难进行调试。
我不能确定这是类型问题、网络问题还是GitHub.com问题。什么都没有。
我希望它能快速而频繁地失败。我知道GitHub对象有一个超时和重试。
请参阅https://pygithub.readthedocs.io/en/latest/github.html?highlight=retry#github.MainClass.Github
我想知道我是否可以使用这两个参数做任何事情,所以至少我有一个知道正在做的事情的视觉。我可以添加很多typer.echo语句,但那会非常冗长。
我也不熟悉重试对象。我希望即使进行了重试,也会有一些echo语句告诉我正在尝试重试。
我可以尝试什么?
发布于 2021-07-17 21:22:18
超时应该会阻止github请求挂起,重试应该会让它工作,但根据文档,已经有一个默认的超时。因为这个问题是用于调试的,所以我建议使用日志记录python库来记录脚本在文件中运行的步骤。你可以找到一个很好的日志教程here。
至于日志记录风格,因为你的案例有很多未知因素,我会在脚本启动时,在"Create Key“步骤之前和"Create Key”之后记录,以防出现错误。当脚本挂起时,您可以查看日志文件。
您可以创建一个bash脚本,以便在超时时运行脚本,并在程序以非零退出代码退出时收到通知,并让它在夜间运行:
timeout 5 python <yourscript>.pyhttps://stackoverflow.com/questions/68222779
复制相似问题