fix: reconnect automatically after disconnect (#13)

This commit is contained in:
martin 2023-12-19 01:55:45 +01:00 committed by GitHub
parent 0ea6cd556c
commit 00a1ed912b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 24 deletions

2
go.mod
View file

@ -15,6 +15,6 @@ require (
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/sys v0.15.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)

4
go.sum
View file

@ -29,8 +29,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=

View file

@ -18,6 +18,10 @@ import (
var wg sync.WaitGroup
var (
mutex sync.Mutex
)
var unmarshalError = "Can not unmarshal JSON"
func Allrequests(r *prometheus.Registry) {
@ -52,8 +56,7 @@ func Analyze(r *prometheus.Registry) {
res3, err3 := (<-serverinfo)()
if err != nil && err2 != nil && err3 != nil {
} else {
if err == nil && err2 == nil && err3 == nil {
prom.SendBackMessagePreference(res3, res2, res1, r)
}
}
@ -62,38 +65,36 @@ func GetAllUsers(c chan func() (*models.StructAllUsers, error)) {
defer wg.Done()
resp, err := Apirequest("/api/user?isAll=true", "GET")
if err == nil {
if models.GetPromptError() == true {
models.SetPromptError(false)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
} else {
result := new(models.StructAllUsers)
if err := json.Unmarshal(body, &result); err != nil { // Parse []byte to go struct pointer
if err := json.Unmarshal(body, &result); err != nil {
log.Error(unmarshalError)
}
c <- (func() (*models.StructAllUsers, error) { return result, nil })
return
}
}
c <- (func() (*models.StructAllUsers, error) { return new(models.StructAllUsers), err })
}
func ServerVersion(r *prometheus.Registry) {
defer wg.Done()
resp, err := Apirequest("/api/server-info/version", "GET")
if err == nil {
if models.GetPromptError() == true {
models.SetPromptError(false)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
} else {
var result models.StructServerVersion
if err := json.Unmarshal(body, &result); err != nil { // Parse []byte to go struct pointer
if err := json.Unmarshal(body, &result); err != nil {
log.Error(unmarshalError)
}
@ -107,22 +108,20 @@ func ServerInfo(c chan func() (*models.StructServerInfo, error)) {
resp, err := Apirequest("/api/server-info/statistics", "GET")
if err == nil {
if models.GetPromptError() == true {
models.SetPromptError(false)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
} else {
result := new(models.StructServerInfo)
if err := json.Unmarshal(body, &result); err != nil { // Parse []byte to go struct pointer
if err := json.Unmarshal(body, &result); err != nil {
log.Println(unmarshalError)
}
c <- (func() (*models.StructServerInfo, error) { return result, nil })
return
}
}
c <- (func() (*models.StructServerInfo, error) { return new(models.StructServerInfo), err })
}
func GetAllJobsStatus(c chan func() (*models.StructAllJobsStatus, error)) {
@ -130,22 +129,20 @@ func GetAllJobsStatus(c chan func() (*models.StructAllJobsStatus, error)) {
resp, err := Apirequest("/api/jobs", "GET")
if err == nil {
if models.GetPromptError() == true {
models.SetPromptError(false)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
} else {
result := new(models.StructAllJobsStatus)
if err := json.Unmarshal(body, &result); err != nil { // Parse []byte to go struct pointer
if err := json.Unmarshal(body, &result); err != nil {
log.Println(unmarshalError)
}
c <- (func() (*models.StructAllJobsStatus, error) { return result, nil })
return
}
}
c <- (func() (*models.StructAllJobsStatus, error) { return new(models.StructAllJobsStatus), err })
}
func Apirequest(uri string, method string) (*http.Response, error) {
@ -160,19 +157,22 @@ func Apirequest(uri string, method string) (*http.Response, error) {
resp, err := client.Do(req)
if err != nil {
err := fmt.Errorf("Can't connect to server")
mutex.Lock()
if models.GetPromptError() == false {
log.Error(err.Error())
models.SetPromptError(true)
}
mutex.Unlock()
return resp, err
}
switch resp.StatusCode {
case http.StatusOK:
mutex.Lock()
if models.GetPromptError() {
models.SetPromptError(false)
}
mutex.Unlock()
return resp, nil
case http.StatusNotFound:
err := fmt.Errorf("%d", resp.StatusCode)
@ -188,10 +188,12 @@ func Apirequest(uri string, method string) (*http.Response, error) {
return resp, err
default:
err := fmt.Errorf("%d", resp.StatusCode)
mutex.Lock()
if !models.GetPromptError() {
models.SetPromptError(true)
log.Debug("Error code ", resp.StatusCode)
}
mutex.Unlock()
return resp, err
}