Able to set timeout for process monitor

This commit is contained in:
Unknown 2014-07-06 17:32:36 -04:00
parent fd5412ec47
commit 097c8e05e6
7 changed files with 82 additions and 32 deletions

View file

@ -11,6 +11,7 @@ import (
"os"
"os/exec"
"strings"
"time"
"github.com/gogits/git"
@ -170,10 +171,6 @@ func ParsePatch(pid int64, cmd *exec.Cmd, reader io.Reader) (*Diff, error) {
}
}
// In case process became zombie.
if err := process.Kill(pid); err != nil {
log.Error("git_diff.ParsePatch(Kill): %v", err)
}
return diff, nil
}
@ -201,10 +198,30 @@ func GetDiff(repoPath, commitid string) (*Diff, error) {
cmd.Stdout = wr
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
done := make(chan error)
go func() {
cmd.Run()
cmd.Start()
done <- cmd.Wait()
wr.Close()
}()
defer rd.Close()
return ParsePatch(process.Add(fmt.Sprintf("GetDiff(%s)", repoPath), cmd), cmd, rd)
desc := fmt.Sprintf("GetDiff(%s)", repoPath)
pid := process.Add(desc, cmd)
go func() {
// In case process became zombie.
select {
case <-time.After(5 * time.Minute):
if errKill := process.Kill(pid); errKill != nil {
log.Error("git_diff.ParsePatch(Kill): %v", err)
}
<-done
// return "", ErrExecTimeout.Error(), ErrExecTimeout
case err = <-done:
process.Remove(pid)
}
}()
return ParsePatch(pid, cmd, rd)
}

View file

@ -89,7 +89,7 @@ func NewRepoContext() {
// Check if server has basic git setting.
stdout, stderr, err := process.Exec("NewRepoContext(get setting)", "git", "config", "--get", "user.name")
if strings.Contains(stderr, "fatal:") {
if err != nil {
log.Fatal("repo.NewRepoContext(fail to get git user.name): %s", stderr)
} else if err != nil || len(strings.TrimSpace(stdout)) == 0 {
if _, stderr, err = process.Exec("NewRepoContext(set email)", "git", "config", "--global", "user.email", "gogitservice@gmail.com"); err != nil {
@ -190,8 +190,8 @@ type Mirror struct {
// MirrorRepository creates a mirror repository from source.
func MirrorRepository(repoId int64, userName, repoName, repoPath, url string) error {
// TODO: need timeout.
_, stderr, err := process.Exec(fmt.Sprintf("MirrorRepository: %s/%s", userName, repoName),
_, stderr, err := process.ExecTimeout(10*time.Minute,
fmt.Sprintf("MirrorRepository: %s/%s", userName, repoName),
"git", "clone", "--mirror", url, repoPath)
if err != nil {
return errors.New("git clone --mirror: " + stderr)
@ -233,9 +233,8 @@ func MirrorUpdate() {
return nil
}
// TODO: need timeout.
repoPath := filepath.Join(setting.RepoRootPath, m.RepoName+".git")
if _, stderr, err := process.ExecDir(
if _, stderr, err := process.ExecDir(10*time.Minute,
repoPath, fmt.Sprintf("MirrorUpdate: %s", repoPath),
"git", "remote", "update"); err != nil {
return errors.New("git remote update: " + stderr)
@ -272,26 +271,23 @@ func MigrateRepository(u *User, name, desc string, private, mirror bool, url str
return repo, UpdateRepository(repo)
}
// TODO: need timeout.
// Clone from local repository.
_, stderr, err := process.Exec(
_, stderr, err := process.ExecTimeout(10*time.Minute,
fmt.Sprintf("MigrateRepository(git clone): %s", repoPath),
"git", "clone", repoPath, tmpDir)
if err != nil {
return repo, errors.New("git clone: " + stderr)
}
// TODO: need timeout.
// Pull data from source.
if _, stderr, err = process.ExecDir(
if _, stderr, err = process.ExecDir(3*time.Minute,
tmpDir, fmt.Sprintf("MigrateRepository(git pull): %s", repoPath),
"git", "pull", url); err != nil {
return repo, errors.New("git pull: " + stderr)
}
// TODO: need timeout.
// Push data to local repository.
if _, stderr, err = process.ExecDir(
if _, stderr, err = process.ExecDir(3*time.Minute,
tmpDir, fmt.Sprintf("MigrateRepository(git push): %s", repoPath),
"git", "push", "origin", "master"); err != nil {
return repo, errors.New("git push: " + stderr)
@ -314,20 +310,20 @@ func extractGitBareZip(repoPath string) error {
// initRepoCommit temporarily changes with work directory.
func initRepoCommit(tmpPath string, sig *git.Signature) (err error) {
var stderr string
if _, stderr, err = process.ExecDir(
if _, stderr, err = process.ExecDir(-1,
tmpPath, fmt.Sprintf("initRepoCommit(git add): %s", tmpPath),
"git", "add", "--all"); err != nil {
return errors.New("git add: " + stderr)
}
if _, stderr, err = process.ExecDir(
if _, stderr, err = process.ExecDir(-1,
tmpPath, fmt.Sprintf("initRepoCommit(git commit): %s", tmpPath),
"git", "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email),
"-m", "Init commit"); err != nil {
return errors.New("git commit: " + stderr)
}
if _, stderr, err = process.ExecDir(
if _, stderr, err = process.ExecDir(-1,
tmpPath, fmt.Sprintf("initRepoCommit(git push): %s", tmpPath),
"git", "push", "origin", "master"); err != nil {
return errors.New("git push: " + stderr)
@ -583,7 +579,7 @@ func CreateRepository(u *User, name, desc, lang, license string, private, mirror
return nil, err
}
_, stderr, err := process.ExecDir(
_, stderr, err := process.ExecDir(-1,
repoPath, fmt.Sprintf("CreateRepository(git update-server-info): %s", repoPath),
"git", "update-server-info")
if err != nil {