do not run post-sync jobs on termination

This commit is contained in:
Max Erenberg 2023-01-28 04:49:13 -05:00 committed by Mirror
parent 0509d030b9
commit f9fe0a115c
1 changed files with 11 additions and 6 deletions

View File

@ -48,8 +48,9 @@ func main() {
connChan := make(chan net.Conn)
// closed or receives a signal to stop listening to the unix socket
stopLisChan := make(chan struct{})
// acknowledgement that the goroutine listening on the unix socket has cleaned up
stopLisAckChan := make(chan struct{})
// Acknowledgement that the goroutine listening on the unix socket has cleaned up
// Use a buffer size of 1 so that we don't have to drain it when shutting down
stopLisAckChan := make(chan struct{}, 1)
// gets unblocked when SIGINT or SIGTERM is sent, will begin process of stopping the program
stopSig := make(chan os.Signal, 1)
@ -110,6 +111,7 @@ mainLoop:
timer := time.NewTimer(1 * time.Minute)
select {
case <-stopSig: // caught a SIGINT or SIGTERM
fmt.Println("Caught termination signal")
// kill all syncing repos and the socket listener
close(stopChan)
close(stopLisChan)
@ -118,6 +120,8 @@ mainLoop:
case <-reloadSig: // caught a SIGHUP
// temporary stop the socket listener and load the config again
stopLisChan <- struct{}{}
// make sure that the current listener has really stopped before starting a new one
<-stopLisAckChan
loadConfigAndStartListener()
case done := <-doneChan: // a sync is done and sends its exit status
@ -158,12 +162,13 @@ mainLoop:
// wait on repos to get terminated
for {
done := <-doneChan
if repo, check := config.RepoMap[done.Name]; check {
sync.SyncCompleted(repo, done.Exit)
}
fmt.Println(done.Name + " finished after the termination signal, post-sync jobs will not run")
// We do NOT want to call sync.SyncCompleted here for two reasons:
// 1. the post-sync jobs might take a long time, and systemd will SIGKILL us if we take too long
// 2. we want is_running to be saved as true in the state file so that we can resume this
// repo when we start back up
numJobsRunning--
if numJobsRunning <= 0 {
time.Sleep(1 * time.Second) // wait a sec for file writes to complete
os.Exit(0)
}
}