github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow@v0.28.1-0.20240311201729-34c6856b157f/pkg/common/readyCheck.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one
     3   * or more contributor license agreements.  See the NOTICE file
     4   * distributed with this work for additional information
     5   * regarding copyright ownership.  The ASF licenses this file
     6   * to you under the Apache License, Version 2.0 (the
     7   * "License"); you may not use this file except in compliance
     8   * with the License.  You may obtain a copy of the License at
     9   * 
    10   *  http://www.apache.org/licenses/LICENSE-2.0
    11   * 
    12   * Unless required by applicable law or agreed to in writing,
    13   * software distributed under the License is distributed on an
    14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   * KIND, either express or implied.  See the License for the
    16   * specific language governing permissions and limitations
    17   * under the License. 
    18   */
    19  
    20  package common
    21  
    22  import (
    23  	"fmt"
    24  	"net/http"
    25  	"time"
    26  )
    27  
    28  func ReadyCheck(healthCheckURL string, pollInterval time.Duration, portMapping string, openDevUI bool) {
    29  	ready := make(chan bool)
    30  
    31  	go PollReadyCheckURL(healthCheckURL, pollInterval, ready)
    32  	select {
    33  	case <-ready:
    34  		fmt.Println("\n✅ SonataFlow project is up and running")
    35  		if openDevUI {
    36  			OpenBrowserURL(fmt.Sprintf("http://localhost:%s/q/dev", portMapping))
    37  		}
    38  	case <-time.After(10 * time.Minute):
    39  		fmt.Printf("Timeout reached. Server at %s is not ready.", healthCheckURL)
    40  	}
    41  }
    42  
    43  func PollReadyCheckURL(healthCheckURL string, interval time.Duration, ready chan<- bool) {
    44  	client := http.Client{
    45  		Timeout: 5 * time.Second,
    46  	}
    47  
    48  	for {
    49  		resp, err := client.Get(healthCheckURL)
    50  		if err == nil && resp.StatusCode == http.StatusOK {
    51  			if resp.StatusCode == http.StatusOK {
    52  				resp.Body.Close() // close the response body right after checking status
    53  				ready <- true
    54  				return
    55  			}
    56  			resp.Body.Close()
    57  		}
    58  		time.Sleep(interval)
    59  	}
    60  }