// Close gracefully shuts the service down in the required order, with timeout
Close gracefully shuts the service down in the required order, with timeout
// Close gracefully shuts the service down in the required order with timeout
func (svc *Service) Close(ctx context.Context) error {
	timeout := svc.shutdownTimeout
	log.Info(ctx, "commencing graceful shutdown", log.Data{"graceful_shutdown_timeout": timeout})
	ctx, cancel := context.WithTimeout(ctx, timeout)
	var gracefulShutdown bool
	go func() {
		defer cancel()
		var hasShutdownError bool
		if svc.serviceList.HealthCheck {
			svc.healthCheck.Stop()
		}
		if svc.serviceList.KafkaConsumer {
			log.Info(ctx, "stopping kafka consumer listener")
			if err := svc.consumer.Stop(); err != nil {
				log.Error(ctx, "error stopping kafka consumer listener", err)
				hasShutdownError = true
			}
			log.Info(ctx, "stopped kafka consumer listener")
		}
		if err := svc.server.Shutdown(ctx); err != nil {
			log.Error(ctx, "failed to shutdown http server", err)
			hasShutdownError = true
		}
		if svc.serviceList.KafkaConsumer {
			log.Info(ctx, "closing kafka consumer")
			if err := svc.consumer.Close(ctx); err != nil {
				log.Error(ctx, "error closing kafka consumer", err)
				hasShutdownError = true
			}
			log.Info(ctx, "closed kafka consumer")
		}
		if !hasShutdownError {
			gracefulShutdown = true
		}
	}()
	<-ctx.Done()
	if !gracefulShutdown {
		err := errors.New("failed to shutdown gracefully")
		log.Error(ctx, "failed to shutdown gracefully ", err)
		return err
	}
	log.Info(ctx, "graceful shutdown was successful")
	return nil
}
func ( svc * Service ) Close ( ctx context . Context ) error { timeout := svc . shutdownTimeout log . Info ( ctx , " " , log . Data { " " : timeout } ) ctx , cancel := context . WithTimeout ( ctx , timeout ) var gracefulShutdown bool go func ( ) { defer cancel ( ) var hasShutdownError bool if svc . serviceList . HealthCheck { svc . healthCheck . Stop ( ) } if svc . serviceList . KafkaConsumer { log . Info ( ctx , " " ) if err := svc . consumer . Stop ( ) ; err != nil { log . Error ( ctx , " " , err ) hasShutdownError = true } log . Info ( ctx , " " ) } if err := svc . server . Shutdown ( ctx ) ; err != nil { log . Error ( ctx , " " , err ) hasShutdownError = true } if svc . serviceList . KafkaConsumer { log . Info ( ctx , " " ) if err := svc . consumer . Close ( ctx ) ; err != nil { log . Error ( ctx , " " , err ) hasShutdownError = true } log . Info ( ctx , " " ) } if ! hasShutdownError { gracefulShutdown = true } } ( ) <- ctx . Done ( ) if ! gracefulShutdown { err := errors . New ( " " ) log . Error ( ctx , " " , err ) return err } log . Info ( ctx , " " ) return nil }
Close gracefully shuts the service down in the required order, with timeout
Close gracefully shuts the service down in the required order with timeout
// track shutdown gracefully closes up // stop healthcheck, as it depends on everything else // If kafka consumer exists, stop listening to it. // This will automatically stop the event consumer loops and no more messages will be processed. // The kafka consumer will be closed after the service shuts down. // stop any incoming requests before closing any outbound connections // If kafka consumer exists, close it. // wait for shutdown success (via cancel) or failure (timeout)