github.com/gramework/gramework@v1.8.1-0.20231027140105-82555c9057f5/app_shutdown.go (about)

     1  package gramework
     2  
     3  // Shutdown gracefully shuts down application servers
     4  func (app *App) Shutdown() (err error) {
     5  	app.runningServersMu.Lock()
     6  	// this is not a hot path, we can freely use defer here
     7  	defer app.runningServersMu.Unlock()
     8  
     9  	newRunningList := []runningServerInfo{}
    10  	for _, info := range app.runningServers {
    11  		app.internalLog.WithField("bind", info.bind).Warn("shutting down server")
    12  		err = info.srv.Shutdown()
    13  		if err != nil {
    14  			app.internalLog.WithError(err).Error("could not shutdown server")
    15  			newRunningList = append(newRunningList, info)
    16  			continue
    17  		}
    18  	}
    19  
    20  	app.runningServers = newRunningList
    21  
    22  	if err == nil {
    23  		app.internalLog.Warn("application servers shutted down successfully")
    24  		return
    25  	}
    26  	app.internalLog.WithError(err).WithField("stillRunning", len(app.runningServers)).Warn("could not stop servers")
    27  	return
    28  }