diff options
author | Unknown <joe2010xtmf@163.com> | 2014-07-06 17:32:36 -0400 |
---|---|---|
committer | Unknown <joe2010xtmf@163.com> | 2014-07-06 17:32:36 -0400 |
commit | 097c8e05e6791efe3876eedf989e2b69190f7f8f (patch) | |
tree | dcf67952ffbfbfd6cd056006073eda7393b723bc /models | |
parent | fd5412ec47018a72c1f05aca487e10ea0a665e45 (diff) | |
download | gitea-097c8e05e6791efe3876eedf989e2b69190f7f8f.tar.gz gitea-097c8e05e6791efe3876eedf989e2b69190f7f8f.zip |
Able to set timeout for process monitor
Diffstat (limited to 'models')
-rw-r--r-- | models/git_diff.go | 29 | ||||
-rw-r--r-- | models/repo.go | 26 |
2 files changed, 34 insertions, 21 deletions
diff --git a/models/git_diff.go b/models/git_diff.go index ed114b7504..303d61d5de 100644 --- a/models/git_diff.go +++ b/models/git_diff.go @@ -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) } diff --git a/models/repo.go b/models/repo.go index 54177bb6fb..35e4427577 100644 --- a/models/repo.go +++ b/models/repo.go @@ -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 { |