Code maintenance: updater loop waitgroup
This commit is contained in:
@@ -94,10 +94,13 @@ func (l *looper) Run(ctx context.Context, wg *sync.WaitGroup) {
|
|||||||
|
|
||||||
for ctx.Err() == nil {
|
for ctx.Err() == nil {
|
||||||
updateCtx, updateCancel := context.WithCancel(ctx)
|
updateCtx, updateCancel := context.WithCancel(ctx)
|
||||||
defer updateCancel()
|
|
||||||
serversCh := make(chan models.AllServers)
|
serversCh := make(chan models.AllServers)
|
||||||
errorCh := make(chan error)
|
errorCh := make(chan error)
|
||||||
|
runWg := &sync.WaitGroup{}
|
||||||
|
runWg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
|
defer runWg.Done()
|
||||||
servers, err := l.updater.UpdateServers(updateCtx)
|
servers, err := l.updater.UpdateServers(updateCtx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if updateCtx.Err() == nil {
|
if updateCtx.Err() == nil {
|
||||||
@@ -122,33 +125,37 @@ func (l *looper) Run(ctx context.Context, wg *sync.WaitGroup) {
|
|||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
l.logger.Warn("context canceled: exiting loop")
|
l.logger.Warn("context canceled: exiting loop")
|
||||||
updateCancel()
|
updateCancel()
|
||||||
|
runWg.Wait()
|
||||||
close(errorCh)
|
close(errorCh)
|
||||||
return
|
return
|
||||||
case <-l.start:
|
case <-l.start:
|
||||||
l.logger.Info("starting")
|
l.logger.Info("starting")
|
||||||
updateCancel()
|
updateCancel()
|
||||||
|
runWg.Wait()
|
||||||
stayHere = false
|
stayHere = false
|
||||||
case <-l.stop:
|
case <-l.stop:
|
||||||
l.logger.Info("stopping")
|
l.logger.Info("stopping")
|
||||||
updateCancel()
|
updateCancel()
|
||||||
|
runWg.Wait()
|
||||||
l.stopped <- struct{}{}
|
l.stopped <- struct{}{}
|
||||||
case servers := <-serversCh:
|
case servers := <-serversCh:
|
||||||
updateCancel()
|
|
||||||
l.setAllServers(servers)
|
l.setAllServers(servers)
|
||||||
if err := l.storage.FlushToFile(servers); err != nil {
|
if err := l.storage.FlushToFile(servers); err != nil {
|
||||||
l.logger.Error(err)
|
l.logger.Error(err)
|
||||||
}
|
}
|
||||||
|
runWg.Wait()
|
||||||
l.state.setStatusWithLock(constants.Completed)
|
l.state.setStatusWithLock(constants.Completed)
|
||||||
l.logger.Info("Updated servers information")
|
l.logger.Info("Updated servers information")
|
||||||
case err := <-errorCh:
|
case err := <-errorCh:
|
||||||
updateCancel()
|
|
||||||
close(serversCh)
|
close(serversCh)
|
||||||
|
runWg.Wait()
|
||||||
l.state.setStatusWithLock(constants.Crashed)
|
l.state.setStatusWithLock(constants.Crashed)
|
||||||
l.logAndWait(ctx, err)
|
l.logAndWait(ctx, err)
|
||||||
crashed = true
|
crashed = true
|
||||||
stayHere = false
|
stayHere = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
updateCancel()
|
||||||
close(errorCh)
|
close(errorCh)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user