Skip to content
This repository has been archived by the owner on Mar 22, 2024. It is now read-only.

Commit

Permalink
fix: reconnect automatically after disconnect (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
martabal authored Dec 19, 2023
1 parent 0ea6cd5 commit 00a1ed9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
44 changes: 23 additions & 21 deletions src/immich/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import (

var wg sync.WaitGroup

var (
mutex sync.Mutex
)

var unmarshalError = "Can not unmarshal JSON"

func Allrequests(r *prometheus.Registry) {
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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)
}

Expand All @@ -107,45 +108,41 @@ 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)) {
defer wg.Done()
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) {
Expand All @@ -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)
Expand All @@ -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
}

Expand Down

0 comments on commit 00a1ed9

Please sign in to comment.