func()

in commands/helpers/health_check.go [24:75]


func (c *HealthCheckCommand) Execute(_ *cli.Context) {
	var ports []string
	var addr string
	var waitAll bool

	if c.ctx == nil {
		c.ctx = context.Background()
	}

	// If command-line ports were given, use those. Otherwise search the environment. The command-line
	// 'port' flag is used by the kubernetes executor, and in kubernetes the networking environment is
	// shared among all containers in the pod. So we use localhost instead of another tcp address.
	if len(c.Ports) > 0 {
		addr = "localhost"

		// The urfave/cli package gives us an unwanted trailing entry, which apparently contains the
		// concatenation of all the --port arguments. Elide it.
		ports = c.Ports[:len(c.Ports)-1]

		// For kubernetes port checks, wait for all services to respond.
		waitAll = true
	} else {
		for _, e := range os.Environ() {
			parts := strings.Split(e, "=")

			switch {
			case len(parts) != 2:
				continue
			case strings.HasSuffix(parts[0], "_TCP_ADDR"):
				addr = parts[1]
			case strings.HasSuffix(parts[0], "_TCP_PORT"):
				ports = append(ports, parts[1])
			}
		}
	}

	if addr == "" || len(ports) == 0 {
		logrus.Fatalln("No HOST or PORT found")
	}

	fmt.Printf("waiting for TCP connection to %s on %v...\n", addr, ports)
	wg := sync.WaitGroup{}
	wg.Add(len(ports))
	ctx, cancel := context.WithCancel(c.ctx)
	defer cancel()

	for _, port := range ports {
		go checkPort(ctx, addr, port, cancel, wg.Done, waitAll)
	}

	wg.Wait()
}