From 3d979440b81b0ec902d43b6e89edfb63bc8094ff Mon Sep 17 00:00:00 2001 From: Max Erenberg Date: Sat, 28 Jan 2023 01:32:15 -0500 Subject: [PATCH] stop timers if they have not expired --- merlin/logger/logger.go | 2 +- merlin/merlin.go | 29 +++++++++++++---------------- merlin/sync/utils.go | 9 ++++++--- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/merlin/logger/logger.go b/merlin/logger/logger.go index 84f05b1..4d0cb9b 100644 --- a/merlin/logger/logger.go +++ b/merlin/logger/logger.go @@ -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 diff --git a/merlin/merlin.go b/merlin/merlin.go index 4122402..390c3fb 100644 --- a/merlin/merlin.go +++ b/merlin/merlin.go @@ -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) } } diff --git a/merlin/sync/utils.go b/merlin/sync/utils.go index 4f05c00..62ae493 100644 --- a/merlin/sync/utils.go +++ b/merlin/sync/utils.go @@ -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() }