pull out pingUntil function

This commit is contained in:
c9s 2021-02-04 14:29:15 +08:00
parent f2686d02b0
commit e257f6e8f6

View File

@ -666,16 +666,20 @@ func openURL(url string) error {
return cmd.Start()
}
func pingAndOpenURL(ctx context.Context, baseURL string) {
func pingUntil(ctx context.Context, baseURL string, callback func()) {
pingURL := baseURL + "/api/ping"
setupURL := baseURL + "/setup"
timeout := time.NewTimer(time.Minute)
ticker := time.NewTimer(time.Second)
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
@ -683,11 +687,18 @@ func pingAndOpenURL(ctx context.Context, baseURL string) {
var response map[string]interface{}
var err = getJSON(pingURL, &response)
if err == nil {
if err := openURL(setupURL); err != nil {
logrus.WithError(err).Errorf("can not call open command to open the web page")
}
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")
}
})
}