github.com/google/fleetspeak@v0.1.15-0.20240426164851-4f31f62c1aea/fleetspeak/src/client/entry/wait_windows.go (about)

     1  //go:build windows
     2  
     3  package entry
     4  
     5  import (
     6  	"os"
     7  	"os/signal"
     8  	"syscall"
     9  	"time"
    10  
    11  	log "github.com/golang/glog"
    12  
    13  	"github.com/google/fleetspeak/fleetspeak/src/client"
    14  )
    15  
    16  // Self-imposed timeout for shutting down gracefully.
    17  var shutdownTimeout = 20 * time.Second
    18  
    19  func Wait(cl *client.Client, _ string /* profileDir */) {
    20  	s := make(chan os.Signal, 2)
    21  	signal.Notify(s, os.Interrupt, syscall.SIGTERM)
    22  	defer signal.Stop(s)
    23  
    24  	for si := range s {
    25  		switch si {
    26  		case os.Interrupt, syscall.SIGTERM:
    27  			log.Infof("Received signal %v, cleaning up...", si)
    28  			shutdownTimer := time.AfterFunc(shutdownTimeout, func() {
    29  				// TODO: Add server-side monitoring for this.
    30  				log.Exitf("Fleetspeak received SIGTERM, but failed to shut down in %v. Exiting ungracefully.", shutdownTimeout)
    31  			})
    32  			cl.Stop()
    33  			shutdownTimer.Stop()
    34  			return
    35  		}
    36  	}
    37  }