fix: reconnect automatically after disconnect (#13)
This commit is contained in:
parent
0ea6cd556c
commit
00a1ed912b
3 changed files with 26 additions and 24 deletions
2
go.mod
2
go.mod
|
@ -15,6 +15,6 @@ require (
|
||||||
github.com/prometheus/client_model v0.5.0 // indirect
|
github.com/prometheus/client_model v0.5.0 // indirect
|
||||||
github.com/prometheus/common v0.45.0 // indirect
|
github.com/prometheus/common v0.45.0 // indirect
|
||||||
github.com/prometheus/procfs v0.12.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
|
google.golang.org/protobuf v1.31.0 // indirect
|
||||||
)
|
)
|
||||||
|
|
4
go.sum
4
go.sum
|
@ -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 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
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.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
|
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
|
||||||
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
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=
|
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.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||||
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
|
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
|
||||||
|
|
|
@ -18,6 +18,10 @@ import (
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
|
var (
|
||||||
|
mutex sync.Mutex
|
||||||
|
)
|
||||||
|
|
||||||
var unmarshalError = "Can not unmarshal JSON"
|
var unmarshalError = "Can not unmarshal JSON"
|
||||||
|
|
||||||
func Allrequests(r *prometheus.Registry) {
|
func Allrequests(r *prometheus.Registry) {
|
||||||
|
@ -52,8 +56,7 @@ func Analyze(r *prometheus.Registry) {
|
||||||
|
|
||||||
res3, err3 := (<-serverinfo)()
|
res3, err3 := (<-serverinfo)()
|
||||||
|
|
||||||
if err != nil && err2 != nil && err3 != nil {
|
if err == nil && err2 == nil && err3 == nil {
|
||||||
} else {
|
|
||||||
prom.SendBackMessagePreference(res3, res2, res1, r)
|
prom.SendBackMessagePreference(res3, res2, res1, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -62,38 +65,36 @@ func GetAllUsers(c chan func() (*models.StructAllUsers, error)) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
resp, err := Apirequest("/api/user?isAll=true", "GET")
|
resp, err := Apirequest("/api/user?isAll=true", "GET")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if models.GetPromptError() == true {
|
|
||||||
models.SetPromptError(false)
|
|
||||||
}
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
result := new(models.StructAllUsers)
|
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)
|
log.Error(unmarshalError)
|
||||||
}
|
}
|
||||||
|
|
||||||
c <- (func() (*models.StructAllUsers, error) { return result, nil })
|
c <- (func() (*models.StructAllUsers, error) { return result, nil })
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
c <- (func() (*models.StructAllUsers, error) { return new(models.StructAllUsers), err })
|
||||||
}
|
}
|
||||||
|
|
||||||
func ServerVersion(r *prometheus.Registry) {
|
func ServerVersion(r *prometheus.Registry) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
resp, err := Apirequest("/api/server-info/version", "GET")
|
resp, err := Apirequest("/api/server-info/version", "GET")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if models.GetPromptError() == true {
|
|
||||||
models.SetPromptError(false)
|
|
||||||
}
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
var result models.StructServerVersion
|
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)
|
log.Error(unmarshalError)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,22 +108,20 @@ func ServerInfo(c chan func() (*models.StructServerInfo, error)) {
|
||||||
resp, err := Apirequest("/api/server-info/statistics", "GET")
|
resp, err := Apirequest("/api/server-info/statistics", "GET")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|
||||||
if models.GetPromptError() == true {
|
|
||||||
models.SetPromptError(false)
|
|
||||||
}
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
result := new(models.StructServerInfo)
|
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)
|
log.Println(unmarshalError)
|
||||||
}
|
}
|
||||||
c <- (func() (*models.StructServerInfo, error) { return result, nil })
|
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)) {
|
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")
|
resp, err := Apirequest("/api/jobs", "GET")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|
||||||
if models.GetPromptError() == true {
|
|
||||||
models.SetPromptError(false)
|
|
||||||
}
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
result := new(models.StructAllJobsStatus)
|
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)
|
log.Println(unmarshalError)
|
||||||
}
|
}
|
||||||
c <- (func() (*models.StructAllJobsStatus, error) { return result, nil })
|
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) {
|
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)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err := fmt.Errorf("Can't connect to server")
|
err := fmt.Errorf("Can't connect to server")
|
||||||
|
mutex.Lock()
|
||||||
if models.GetPromptError() == false {
|
if models.GetPromptError() == false {
|
||||||
log.Error(err.Error())
|
log.Error(err.Error())
|
||||||
models.SetPromptError(true)
|
models.SetPromptError(true)
|
||||||
}
|
}
|
||||||
|
mutex.Unlock()
|
||||||
return resp, err
|
return resp, err
|
||||||
|
|
||||||
}
|
}
|
||||||
switch resp.StatusCode {
|
switch resp.StatusCode {
|
||||||
case http.StatusOK:
|
case http.StatusOK:
|
||||||
|
mutex.Lock()
|
||||||
if models.GetPromptError() {
|
if models.GetPromptError() {
|
||||||
models.SetPromptError(false)
|
models.SetPromptError(false)
|
||||||
}
|
}
|
||||||
|
mutex.Unlock()
|
||||||
return resp, nil
|
return resp, nil
|
||||||
case http.StatusNotFound:
|
case http.StatusNotFound:
|
||||||
err := fmt.Errorf("%d", resp.StatusCode)
|
err := fmt.Errorf("%d", resp.StatusCode)
|
||||||
|
@ -188,10 +188,12 @@ func Apirequest(uri string, method string) (*http.Response, error) {
|
||||||
return resp, err
|
return resp, err
|
||||||
default:
|
default:
|
||||||
err := fmt.Errorf("%d", resp.StatusCode)
|
err := fmt.Errorf("%d", resp.StatusCode)
|
||||||
|
mutex.Lock()
|
||||||
if !models.GetPromptError() {
|
if !models.GetPromptError() {
|
||||||
models.SetPromptError(true)
|
models.SetPromptError(true)
|
||||||
log.Debug("Error code ", resp.StatusCode)
|
log.Debug("Error code ", resp.StatusCode)
|
||||||
}
|
}
|
||||||
|
mutex.Unlock()
|
||||||
return resp, err
|
return resp, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue