stop timers if they have not expired

This commit is contained in:
Max Erenberg 2023-01-28 01:32:15 -05:00 committed by Mirror
parent 1cd945c4c0
commit 80ece34280
3 changed files with 20 additions and 20 deletions

View File

@ -66,7 +66,7 @@ func (logger *Logger) log(level int, v ...interface{}) {
args = append(args, v...)
logger.SetOutput(f)
logger.Println(args)
logger.Println(args...)
}
// write debug information to the logfile

View File

@ -17,7 +17,7 @@ import (
"git.csclub.uwaterloo.ca/public/merlin/sync"
)
var DEFAULT_CONFIG_PATH = "merlin-config.ini"
const DEFAULT_CONFIG_PATH = "merlin-config.ini"
func main() {
@ -96,18 +96,19 @@ func main() {
}
runAsManyAsPossible()
runLoop:
mainLoop:
for {
timer := time.NewTimer(1 * time.Minute)
select {
case <-stopSig: // caught a SIGINT or SIGTERM
// kill all syncing repos and the socket listener
close(stopChan)
close(stopLisChan)
break runLoop
break mainLoop
case <-reloadSig: // caught a SIGHUP
// temporary stop the socket listener and load the config again
stopLisChan <- struct{}{}
close(stopLisChan)
loadConfig()
case done := <-doneChan: // a sync is done and sends its exit status
@ -132,32 +133,28 @@ runLoop:
// close the received connection so that the message is sent
conn.Close()
case <-time.After(1 * time.Minute):
case <-timer.C:
}
timer.Stop()
runAsManyAsPossible()
}
// allow some time for jobs to terminate before force exiting the program
go func() {
<-time.After(time.Minute)
time.Sleep(1 * time.Minute)
logger.ErrLog("One minute has passed, forcefully exiting the program")
os.Exit(1)
}()
// wait on repos to get terminated
for {
select {
case done := <-doneChan:
if repo, check := config.RepoMap[done.Name]; check {
sync.SyncCompleted(repo, done.Exit)
}
numJobsRunning--
case <-time.After(1 * time.Second):
done := <-doneChan
if repo, check := config.RepoMap[done.Name]; check {
sync.SyncCompleted(repo, done.Exit)
}
numJobsRunning--
if numJobsRunning <= 0 {
<-time.After(1 * time.Second) // wait a sec for file writes to complete
time.Sleep(1 * time.Second) // wait a sec for file writes to complete
os.Exit(0)
}
}

View File

@ -52,9 +52,10 @@ func spawnProcess(repo *config.Repo, args []string) (ch <-chan *exec.Cmd) {
repo.Logger.Error("Could not send signal to process:", err)
return
}
timer := time.NewTimer(30 * time.Second)
defer timer.Stop()
select {
case <-time.After(30 * time.Second):
case <-timer.C:
repo.Logger.Warning("Process still hasn't stopped after 30 seconds; sending SIGKILL")
cmd.Process.Signal(syscall.SIGKILL)
@ -72,6 +73,8 @@ func spawnProcess(repo *config.Repo, args []string) (ch <-chan *exec.Cmd) {
defer func() {
cmdChan <- cmd
}()
timer := time.NewTimer(time.Duration(repo.MaxTime) * time.Second)
defer timer.Stop()
select {
case <-cmdDoneChan:
if !cmd.ProcessState.Success() {
@ -84,7 +87,7 @@ func spawnProcess(repo *config.Repo, args []string) (ch <-chan *exec.Cmd) {
repo.Logger.Debug("Received signal to stop, killing process...")
killProcess()
case <-time.After(time.Duration(repo.MaxTime) * time.Second):
case <-timer.C:
repo.Logger.Warning("Process has exceeded its max time; killing now")
killProcess()
}