summaryrefslogtreecommitdiffstats
path: root/modules/process/manager.go
diff options
context:
space:
mode:
Diffstat (limited to 'modules/process/manager.go')
-rw-r--r--modules/process/manager.go73
1 files changed, 47 insertions, 26 deletions
diff --git a/modules/process/manager.go b/modules/process/manager.go
index 3e77c0a6a9..af6ee9b81d 100644
--- a/modules/process/manager.go
+++ b/modules/process/manager.go
@@ -12,6 +12,7 @@ import (
"fmt"
"io"
"os/exec"
+ "sort"
"sync"
"time"
)
@@ -24,14 +25,17 @@ var (
// ErrExecTimeout represent a timeout error
ErrExecTimeout = errors.New("Process execution timeout")
manager *Manager
+
+ // DefaultContext is the default context to run processing commands in
+ DefaultContext = context.Background()
)
-// Process represents a working process inherit from Gogs.
+// Process represents a working process inheriting from Gitea.
type Process struct {
PID int64 // Process ID, not system one.
Description string
Start time.Time
- Cmd *exec.Cmd
+ Cancel context.CancelFunc
}
// Manager knows about all processes and counts PIDs.
@@ -39,28 +43,28 @@ type Manager struct {
mutex sync.Mutex
counter int64
- Processes map[int64]*Process
+ processes map[int64]*Process
}
// GetManager returns a Manager and initializes one as singleton if there's none yet
func GetManager() *Manager {
if manager == nil {
manager = &Manager{
- Processes: make(map[int64]*Process),
+ processes: make(map[int64]*Process),
}
}
return manager
}
// Add a process to the ProcessManager and returns its PID.
-func (pm *Manager) Add(description string, cmd *exec.Cmd) int64 {
+func (pm *Manager) Add(description string, cancel context.CancelFunc) int64 {
pm.mutex.Lock()
pid := pm.counter + 1
- pm.Processes[pid] = &Process{
+ pm.processes[pid] = &Process{
PID: pid,
Description: description,
Start: time.Now(),
- Cmd: cmd,
+ Cancel: cancel,
}
pm.counter = pid
pm.mutex.Unlock()
@@ -71,10 +75,32 @@ func (pm *Manager) Add(description string, cmd *exec.Cmd) int64 {
// Remove a process from the ProcessManager.
func (pm *Manager) Remove(pid int64) {
pm.mutex.Lock()
- delete(pm.Processes, pid)
+ delete(pm.processes, pid)
pm.mutex.Unlock()
}
+// Cancel a process in the ProcessManager.
+func (pm *Manager) Cancel(pid int64) {
+ pm.mutex.Lock()
+ process, ok := pm.processes[pid]
+ pm.mutex.Unlock()
+ if ok {
+ process.Cancel()
+ }
+}
+
+// Processes gets the processes in a thread safe manner
+func (pm *Manager) Processes() []*Process {
+ pm.mutex.Lock()
+ processes := make([]*Process, 0, len(pm.processes))
+ for _, process := range pm.processes {
+ processes = append(processes, process)
+ }
+ pm.mutex.Unlock()
+ sort.Sort(processList(processes))
+ return processes
+}
+
// Exec a command and use the default timeout.
func (pm *Manager) Exec(desc, cmdName string, args ...string) (string, string, error) {
return pm.ExecDir(-1, "", desc, cmdName, args...)
@@ -110,7 +136,7 @@ func (pm *Manager) ExecDirEnvStdIn(timeout time.Duration, dir, desc string, env
stdOut := new(bytes.Buffer)
stdErr := new(bytes.Buffer)
- ctx, cancel := context.WithTimeout(context.Background(), timeout)
+ ctx, cancel := context.WithTimeout(DefaultContext, timeout)
defer cancel()
cmd := exec.CommandContext(ctx, cmdName, args...)
@@ -126,7 +152,7 @@ func (pm *Manager) ExecDirEnvStdIn(timeout time.Duration, dir, desc string, env
return "", "", err
}
- pid := pm.Add(desc, cmd)
+ pid := pm.Add(desc, cancel)
err := cmd.Wait()
pm.Remove(pid)
@@ -137,21 +163,16 @@ func (pm *Manager) ExecDirEnvStdIn(timeout time.Duration, dir, desc string, env
return stdOut.String(), stdErr.String(), err
}
-// Kill and remove a process from list.
-func (pm *Manager) Kill(pid int64) error {
- if proc, exists := pm.Processes[pid]; exists {
- pm.mutex.Lock()
- if proc.Cmd != nil &&
- proc.Cmd.Process != nil &&
- proc.Cmd.ProcessState != nil &&
- !proc.Cmd.ProcessState.Exited() {
- if err := proc.Cmd.Process.Kill(); err != nil {
- return fmt.Errorf("failed to kill process(%d/%s): %v", pid, proc.Description, err)
- }
- }
- delete(pm.Processes, pid)
- pm.mutex.Unlock()
- }
+type processList []*Process
+
+func (l processList) Len() int {
+ return len(l)
+}
+
+func (l processList) Less(i, j int) bool {
+ return l[i].PID < l[j].PID
+}
- return nil
+func (l processList) Swap(i, j int) {
+ l[i], l[j] = l[j], l[i]
}