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 /modules | |
parent | fd5412ec47018a72c1f05aca487e10ea0a665e45 (diff) | |
download | gitea-097c8e05e6791efe3876eedf989e2b69190f7f8f.tar.gz gitea-097c8e05e6791efe3876eedf989e2b69190f7f8f.zip |
Able to set timeout for process monitor
Diffstat (limited to 'modules')
-rw-r--r-- | modules/middleware/repo.go | 2 | ||||
-rw-r--r-- | modules/process/manager.go | 49 |
2 files changed, 44 insertions, 7 deletions
diff --git a/modules/middleware/repo.go b/modules/middleware/repo.go index 8aa4a6a8a6..cf8ce412e5 100644 --- a/modules/middleware/repo.go +++ b/modules/middleware/repo.go @@ -82,7 +82,7 @@ func RepoAssignment(redirect bool, args ...bool) martini.Handler { ctx.Repo.Owner = user // Organization owner team members are true owners as well. - if ctx.Repo.Owner.IsOrganization() && ctx.Repo.Owner.IsOrgOwner(ctx.User.Id) { + if ctx.IsSigned && ctx.Repo.Owner.IsOrganization() && ctx.Repo.Owner.IsOrgOwner(ctx.User.Id) { ctx.Repo.IsTrueOwner = true } diff --git a/modules/process/manager.go b/modules/process/manager.go index 173b2aa4ee..ce8ab7b4b2 100644 --- a/modules/process/manager.go +++ b/modules/process/manager.go @@ -6,6 +6,7 @@ package process import ( "bytes" + "errors" "fmt" "os/exec" "time" @@ -13,6 +14,16 @@ import ( "github.com/gogits/gogs/modules/log" ) +var ( + ErrExecTimeout = errors.New("Process execution timeout") +) + +// Common timeout. +var ( + // NOTE: could be custom in config file for default. + DEFAULT = 60 * time.Second +) + // Process represents a working process inherit from Gogs. type Process struct { Pid int64 // Process ID, not system one. @@ -40,7 +51,12 @@ func Add(desc string, cmd *exec.Cmd) int64 { return pid } -func ExecDir(dir, desc, cmdName string, args ...string) (string, string, error) { +// Exec starts executing a command in given path, it records its process and timeout. +func ExecDir(timeout time.Duration, dir, desc, cmdName string, args ...string) (string, string, error) { + if timeout == -1 { + timeout = DEFAULT + } + bufOut := new(bytes.Buffer) bufErr := new(bytes.Buffer) @@ -48,18 +64,39 @@ func ExecDir(dir, desc, cmdName string, args ...string) (string, string, error) cmd.Dir = dir cmd.Stdout = bufOut cmd.Stderr = bufErr + if err := cmd.Start(); err != nil { + return "", err.Error(), err + } pid := Add(desc, cmd) - err := cmd.Run() - if errKill := Kill(pid); errKill != nil { - log.Error("Exec: %v", pid, desc, errKill) + done := make(chan error) + go func() { + done <- cmd.Wait() + }() + + var err error + select { + case <-time.After(timeout): + if errKill := Kill(pid); errKill != nil { + log.Error("Exec(%d:%s): %v", pid, desc, errKill) + } + <-done + return "", ErrExecTimeout.Error(), ErrExecTimeout + case err = <-done: } + + Remove(pid) return bufOut.String(), bufErr.String(), err } -// Exec starts executing a command and record its process. +// Exec starts executing a command, it records its process and timeout. +func ExecTimeout(timeout time.Duration, desc, cmdName string, args ...string) (string, string, error) { + return ExecDir(timeout, "", desc, cmdName, args...) +} + +// Exec starts executing a command, it records its process and has default timeout. func Exec(desc, cmdName string, args ...string) (string, string, error) { - return ExecDir("", desc, cmdName, args...) + return ExecDir(-1, "", desc, cmdName, args...) } // Remove removes a process from list. |