mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-24 15:55:14 +00:00
46 lines
873 B
Go
46 lines
873 B
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func PingUntil(ctx context.Context, interval time.Duration, baseURL string, callback func()) {
|
|
pingURL := baseURL + "/api/ping"
|
|
timeout := time.NewTimer(3 * time.Minute)
|
|
|
|
ticker := time.NewTicker(interval)
|
|
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 {
|
|
callback()
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func pingAndOpenURL(ctx context.Context, baseURL string) {
|
|
setupURL := baseURL + "/setup"
|
|
go PingUntil(ctx, time.Second, baseURL, func() {
|
|
if err := openURL(setupURL); err != nil {
|
|
logrus.WithError(err).Errorf("can not call open command to open the web page")
|
|
}
|
|
})
|
|
}
|