bbgo_origin/pkg/server/ping.go
2021-02-04 20:34:31 +08:00

46 lines
838 B
Go

package server
import (
"context"
"time"
"github.com/sirupsen/logrus"
)
func PingUntil(ctx context.Context, baseURL string, callback func()) {
pingURL := baseURL + "/api/ping"
timeout := time.NewTimer(time.Minute)
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-timeout.C:
logrus.Warnf("ping hits 1 minute timeout")
return
case <-ctx.Done():
return
case <-ticker.C:
var response map[string]interface{}
var err = getJSON(pingURL, &response)
if err == nil {
go callback()
return
}
}
}
}
func pingAndOpenURL(ctx context.Context, baseURL string) {
setupURL := baseURL + "/setup"
go PingUntil(ctx, baseURL, func() {
if err := openURL(setupURL); err != nil {
logrus.WithError(err).Errorf("can not call open command to open the web page")
}
})
}