I'm writing a Python script that will refresh a Git repository, then build it.
The script is to be run daily (e.g. running on a daily build server).
The issue is that git commands to our server sometimes never complete (ok, I terminate the processes after 10 minutes or more).
I would like to set a Timeout for the subprocess (and probably put into a retry loop).
How do I set up the Timeout for a subprocess.
Here's my code so far:
#------------------------------------------------------------------------------
# Script to daily build Voyant 3
#------------------------------------------------------------------------------
import subprocess
import os
#------------------------------------------------------------------------------
# Main program starts here.
#------------------------------------------------------------------------------
repo_dir = "c:/sandboxes/git/voyant-3"
#------------------------------------------------------------------------------
# Change to repo (repository) folder.
#------------------------------------------------------------------------------
os.chdir(repo_dir);
#------------------------------------------------------------------------------
# Refresh the repo (sandbox)
#------------------------------------------------------------------------------
subprocess.run(["git", "fetch", "--all]"]);
Notes:
- Using Python 27 (preferred) or Python 36
- Using Windows 10 (The script will be registered as a service on the PC)
- Research on using PyGit is not impressive; most Users say to use Python Subprocess.
- From the Python documentation: (https://docs.python.org/3/library/subprocess.html):
subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None, capture_output=False, shell=False, cwd=None, timeout=None, check=False, encoding=None, errors=None, text=None, env=None, universal_newlines=None, **other_popen_kwargs)
timeoutparameter and catch theTimeoutExpirederror?timeoutcommand, which can also do the "TERM, then KILL" sequence;subprocess.run(["timeout", "-k30", "600", "git", "fetch", "--all"])