mirror/merlin/sync/sync.go

129 lines
3.1 KiB
Go

package sync
import (
"fmt"
"os"
"os/exec"
"syscall"
"time"
"git.csclub.uwaterloo.ca/public/merlin/config"
)
// 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 spawnSyncProcess(repo *config.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:]...)
repo.Logger.Debug("Starting process")
if err := cmd.Start(); err != nil {
repo.Logger.Error(fmt.Errorf("could not start process for %s: %w", repo.Name, err).Error())
return
}
cmdChan := make(chan *exec.Cmd)
ch = cmdChan
cmdDoneChan := make(chan struct{})
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 <-cmdDoneChan:
repo.Logger.Debug("Process has been stopped.")
}
}
go func() {
cmd.Wait()
close(cmdDoneChan)
}()
go func() {
defer func() {
cmdChan <- cmd
}()
select {
case <-cmdDoneChan:
if !cmd.ProcessState.Success() {
repo.Logger.Warning("Process ended with status code", cmd.ProcessState.ExitCode())
}
case <-repo.StopChan:
repo.Logger.Debug("Received signal to stop, killing process...")
killProcess()
case <-time.After(time.Duration(repo.MaxTime) * time.Second):
repo.Logger.Warning("Process has exceeded its max time; killing now")
killProcess()
}
}()
return
}
func startSync(repo *config.Repo) {
status := config.FAILURE
defer func() {
repo.DoneChan <- config.SyncResult{
Name: repo.Name,
Exit: status,
}
}()
localDir := buildDownloadDir(repo)
err := os.MkdirAll(localDir, 0775)
if err != nil {
err = fmt.Errorf("Could not create directory %s: %w", localDir, err)
// I'm not sure if logger can handle error so just use the string?
repo.Logger.Error(err.Error())
return
}
args := getSyncCommand(repo)
if repo.PasswordFile != "" {
filename := config.Conf.PasswordDir + "/" + repo.PasswordFile
args = append(args, "--password-file", filename)
}
args = append(args, buildRsyncHost(repo), localDir)
ch := spawnSyncProcess(repo, args)
if ch == nil {
// SpawnProcess will have already logged error
return
}
cmd := <-ch
switch cmd.ProcessState.ExitCode() {
case 0:
status = config.SUCCESS
case -1:
status = config.TERMINATED
// default is already FAILURE
}
}
func zfsSync(repo *config.Repo) {
out, err := exec.Command("/home/mirror/bin/zfssync", repo.Name).CombinedOutput()
if err != nil {
repo.Logger.Error(err)
} else {
f, err := os.OpenFile(repo.ZfssyncLogFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
repo.Logger.Error(err.Error())
} else {
f.Write(out)
}
}
}