bbgo_origin/pkg/server/ping.go

46 lines
873 B
Go
Raw Permalink Normal View History

2021-02-04 08:59:00 +00:00
package server
import (
"context"
"time"
"github.com/sirupsen/logrus"
)
2021-02-21 10:58:25 +00:00
func PingUntil(ctx context.Context, interval time.Duration, baseURL string, callback func()) {
2021-02-04 08:59:00 +00:00
pingURL := baseURL + "/api/ping"
2021-02-17 11:06:55 +00:00
timeout := time.NewTimer(3 * time.Minute)
2021-02-04 08:59:00 +00:00
2021-02-21 10:58:25 +00:00
ticker := time.NewTicker(interval)
2021-02-04 08:59:00 +00:00
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 {
2021-02-17 11:06:55 +00:00
callback()
2021-02-04 08:59:00 +00:00
return
}
}
}
}
func pingAndOpenURL(ctx context.Context, baseURL string) {
setupURL := baseURL + "/setup"
2021-02-21 10:58:25 +00:00
go PingUntil(ctx, time.Second, 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")
}
})
}