75 lines
2.0 KiB
Go
75 lines
2.0 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) {
|
|
// TODO change stdout and stderr to something else
|
|
cmd := exec.Command(args[0], args[1:]...)
|
|
cmd.Stdout = os.Stdout
|
|
cmd.Stderr = os.Stderr
|
|
startTime := time.Now().Unix()
|
|
repo.Logger.Debug("Starting process")
|
|
err := cmd.Start()
|
|
if 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; sending SIGKILL now")
|
|
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:
|
|
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
|
|
}
|