mirror/merlin/common/process_manager.go

94 lines
2.5 KiB
Go

package common
import (
"fmt"
"os"
"os/exec"
"syscall"
"time"
)
// SpawnProcess spawns a child process for the given repo. The process will
// be stopped early if the repo receives a stop signal, or if the process
// runs for longer than the repo's MaxTime.
// It returns a channel through which a Cmd will be sent once it has finished,
// or nil if it was unable to start a process.
func SpawnProcess(repo *Repo, args []string) (ch <-chan *exec.Cmd) {
// startTime and time took will be handled in common.go by SyncExit
cmd := exec.Command(args[0], args[1:]...)
// TODO: change stdout and stderr to something else
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// I have no idea how to do this
// stdout, err := cmd.StdoutPipe()
// if err != nil {
// repo.Logger.Warning(err)
// }
// stderr, err := cmd.StderrPipe()
// if err != nil {
// repo.Logger.Warning(err)
// }
// multi := io.MultiReader(stdout, stderr)
// in := bufio.NewScanner(multi)
// repo.Logger.Debug(in.Text())
// repo.Logger.Warning(in.Err())
// startTime := time.Now().Unix()
repo.Logger.Debug("Starting process")
if err := cmd.Start(); err != nil {
err = fmt.Errorf("could not start process %s: %w", args[0], err)
repo.Logger.Error(err)
return
}
cmdChan := make(chan *exec.Cmd)
ch = cmdChan
procDoneChan := make(chan bool, 1)
killProcess := func() {
err := cmd.Process.Signal(syscall.SIGTERM)
if err != nil {
repo.Logger.Error("Could not send signal to process:", err)
return
}
select {
case <-time.After(30 * time.Second):
repo.Logger.Warning("Process still hasn't stopped after 30 seconds; sending SIGKILL")
cmd.Process.Signal(syscall.SIGKILL)
case <-procDoneChan:
repo.Logger.Debug("Process has stopped.")
}
}
go func() {
cmd.Wait()
procDoneChan <- true
}()
go func() {
defer func() {
cmdChan <- cmd
}()
select {
case <-repo.StopChan:
repo.Logger.Info("Received signal to stop, killing process...")
killProcess()
case <-procDoneChan:
// the following could be moved to SyncExit in common.go
if cmd.ProcessState.Success() {
repo.Logger.Debug("Process ended successfully")
} else {
repo.Logger.Warning("Process ended with status code", cmd.ProcessState.ExitCode())
}
// timeTook := time.Now().Unix() - startTime
// repo.Logger.Debug(fmt.Sprintf("Process took %d seconds", timeTook))
case <-time.After(time.Duration(repo.MaxTime) * time.Second):
repo.Logger.Warning("Process has exceeded its max time; killing now")
killProcess()
}
}()
return
}