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 2f7fd80f8e
commit 3d979440b8
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...) args = append(args, v...)
logger.SetOutput(f) logger.SetOutput(f)
logger.Println(args) logger.Println(args...)
} }
// write debug information to the logfile // write debug information to the logfile

View File

@ -17,7 +17,7 @@ import (
"git.csclub.uwaterloo.ca/public/merlin/sync" "git.csclub.uwaterloo.ca/public/merlin/sync"
) )
var DEFAULT_CONFIG_PATH = "merlin-config.ini" const DEFAULT_CONFIG_PATH = "merlin-config.ini"
func main() { func main() {
@ -96,18 +96,19 @@ func main() {
} }
runAsManyAsPossible() runAsManyAsPossible()
runLoop: mainLoop:
for { for {
timer := time.NewTimer(1 * time.Minute)
select { select {
case <-stopSig: // caught a SIGINT or SIGTERM case <-stopSig: // caught a SIGINT or SIGTERM
// kill all syncing repos and the socket listener // kill all syncing repos and the socket listener
close(stopChan) close(stopChan)
close(stopLisChan) close(stopLisChan)
break runLoop break mainLoop
case <-reloadSig: // caught a SIGHUP case <-reloadSig: // caught a SIGHUP
// temporary stop the socket listener and load the config again // temporary stop the socket listener and load the config again
stopLisChan <- struct{}{} close(stopLisChan)
loadConfig() loadConfig()
case done := <-doneChan: // a sync is done and sends its exit status 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 // close the received connection so that the message is sent
conn.Close() conn.Close()
case <-time.After(1 * time.Minute): case <-timer.C:
} }
timer.Stop()
runAsManyAsPossible() runAsManyAsPossible()
} }
// allow some time for jobs to terminate before force exiting the program // allow some time for jobs to terminate before force exiting the program
go func() { go func() {
<-time.After(time.Minute) time.Sleep(1 * time.Minute)
logger.ErrLog("One minute has passed, forcefully exiting the program") logger.ErrLog("One minute has passed, forcefully exiting the program")
os.Exit(1) os.Exit(1)
}() }()
// wait on repos to get terminated // wait on repos to get terminated
for { for {
select { done := <-doneChan
case done := <-doneChan:
if repo, check := config.RepoMap[done.Name]; check { if repo, check := config.RepoMap[done.Name]; check {
sync.SyncCompleted(repo, done.Exit) sync.SyncCompleted(repo, done.Exit)
} }
numJobsRunning-- numJobsRunning--
case <-time.After(1 * time.Second):
}
if numJobsRunning <= 0 { 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) 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) repo.Logger.Error("Could not send signal to process:", err)
return return
} }
timer := time.NewTimer(30 * time.Second)
defer timer.Stop()
select { select {
case <-time.After(30 * time.Second): case <-timer.C:
repo.Logger.Warning("Process still hasn't stopped after 30 seconds; sending SIGKILL") repo.Logger.Warning("Process still hasn't stopped after 30 seconds; sending SIGKILL")
cmd.Process.Signal(syscall.SIGKILL) cmd.Process.Signal(syscall.SIGKILL)
@ -72,6 +73,8 @@ func spawnProcess(repo *config.Repo, args []string) (ch <-chan *exec.Cmd) {
defer func() { defer func() {
cmdChan <- cmd cmdChan <- cmd
}() }()
timer := time.NewTimer(time.Duration(repo.MaxTime) * time.Second)
defer timer.Stop()
select { select {
case <-cmdDoneChan: case <-cmdDoneChan:
if !cmd.ProcessState.Success() { 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...") repo.Logger.Debug("Received signal to stop, killing process...")
killProcess() killProcess()
case <-time.After(time.Duration(repo.MaxTime) * time.Second): case <-timer.C:
repo.Logger.Warning("Process has exceeded its max time; killing now") repo.Logger.Warning("Process has exceeded its max time; killing now")
killProcess() killProcess()
} }