bbgo_origin/pkg/server/ping.go

46 lines
838 B
Go
Raw Normal View History

2021-02-04 08:59:00 +00:00
package server
import (
"context"
"time"
"github.com/sirupsen/logrus"
)
2021-02-04 10:22:47 +00:00
func PingUntil(ctx context.Context, baseURL string, callback func()) {
2021-02-04 08:59:00 +00:00
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"
2021-02-04 10:22:47 +00:00
go PingUntil(ctx, baseURL, func() {
2021-02-04 08:59:00 +00:00
if err := openURL(setupURL); err != nil {
logrus.WithError(err).Errorf("can not call open command to open the web page")
}
})
}