Skip to content

Commit

Permalink
Program errors out due to no ping API response despite API responding (
Browse files Browse the repository at this point in the history
…#431)

* Program errors out due to no ping API response despite API responding
Fixes #393

* add API trace for ping and validate connection

* fix linter
  • Loading branch information
simulot authored Aug 9, 2024
1 parent 1a4de53 commit 3396f6f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 16 deletions.
29 changes: 15 additions & 14 deletions cmd/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,21 @@ func (app *SharedFlags) Start(ctx context.Context) error {
app.Immich.SetDeviceUUID(app.DeviceUUID)
}

if app.APITrace {
if app.APITraceWriter == nil {
err := configuration.MakeDirForFile(app.LogFile)
if err != nil {
return err
}
app.APITraceWriterName = strings.TrimSuffix(app.LogFile, filepath.Ext(app.LogFile)) + ".trace.log"
app.APITraceWriter, err = os.OpenFile(app.APITraceWriterName, os.O_CREATE|os.O_WRONLY, 0o664)
if err != nil {
return err
}
app.Immich.EnableAppTrace(app.APITraceWriter)
}
}

err = app.Immich.PingServer(ctx)
if err != nil {
return err
Expand All @@ -186,20 +201,6 @@ func (app *SharedFlags) Start(ctx context.Context) error {
app.Log.Info(fmt.Sprintf("Connected, user: %s", user.Email))
}

if app.APITrace {
if app.APITraceWriter == nil {
err := configuration.MakeDirForFile(app.LogFile)
if err != nil {
return err
}
app.APITraceWriterName = strings.TrimSuffix(app.LogFile, filepath.Ext(app.LogFile)) + ".trace.log"
app.APITraceWriter, err = os.OpenFile(app.APITraceWriterName, os.O_CREATE|os.O_WRONLY, 0o664)
if err != nil {
return err
}
app.Immich.EnableAppTrace(app.APITraceWriter)
}
}
return nil
}

Expand Down
26 changes: 26 additions & 0 deletions helpers/fshelper/teereadercloser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package fshelper

import "io"

type teeReadCloser struct {
r io.ReadCloser
w io.Writer
}

func (t *teeReadCloser) Read(p []byte) (n int, err error) {
n, err = t.r.Read(p)
if n > 0 {
if n, err := t.w.Write(p[:n]); err != nil {
return n, err
}
}
return
}

func (t *teeReadCloser) Close() error {
return t.r.Close()
}

func TeeReadCloser(r io.ReadCloser, w io.Writer) io.ReadCloser {
return &teeReadCloser{r: r, w: w}
}
15 changes: 15 additions & 0 deletions immich/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"strings"
"sync/atomic"
"time"

"github.com/simulot/immich-go/helpers/fshelper"
)

type TooManyInternalError struct {
Expand Down Expand Up @@ -292,3 +294,16 @@ func responseJSON[T any](object *T) serverResponseOption {
return errors.New("can't decode nil response")
}
}

func responseCopy(buffer *bytes.Buffer) serverResponseOption {
return func(sc *serverCall, resp *http.Response) error {
if resp != nil {
if resp.Body != nil {
newBody := fshelper.TeeReadCloser(resp.Body, buffer)
resp.Body = newBody
return nil
}
}
return nil
}
}
6 changes: 4 additions & 2 deletions immich/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package immich

import (
"bytes"
"context"
"crypto/tls"
"fmt"
Expand Down Expand Up @@ -111,9 +112,10 @@ func NewImmichClient(endPoint string, key string, options ...clientOption) (*Imm
// Ping server
func (ic *ImmichClient) PingServer(ctx context.Context) error {
r := PingResponse{}
err := ic.newServerCall(ctx, "PingServer").do(getRequest("/server-info/ping", setAcceptJSON()), responseJSON(&r))
b := bytes.NewBuffer(nil)
err := ic.newServerCall(ctx, "PingServer").do(getRequest("/server-info/ping", setAcceptJSON()), responseCopy(b), responseJSON(&r))
if err != nil {
return fmt.Errorf("the ping API end point doesn't respond at this address: %s", ic.endPoint+"/server-info/ping")
return fmt.Errorf("unexpected response to the immich's ping API at this address: %s:\n%s", ic.endPoint+"/server-info/ping", b.String())
}
if r.Res != "pong" {
return fmt.Errorf("incorrect ping response: %s", r.Res)
Expand Down

0 comments on commit 3396f6f

Please sign in to comment.