GODT-2010: Add better logging for app focus feature

This commit is contained in:
James Houlahan
2022-11-01 13:16:32 +01:00
parent 99745ac067
commit 1dbc9a1366
2 changed files with 49 additions and 45 deletions

View File

@ -31,6 +31,7 @@ import (
"github.com/Masterminds/semver/v3" "github.com/Masterminds/semver/v3"
"github.com/ProtonMail/proton-bridge/v2/internal/focus" "github.com/ProtonMail/proton-bridge/v2/internal/focus"
"github.com/allan-simon/go-singleinstance" "github.com/allan-simon/go-singleinstance"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
@ -42,9 +43,12 @@ import (
// it will kill old and continue with this new bridge (i.e. no error returned). // it will kill old and continue with this new bridge (i.e. no error returned).
func checkSingleInstance(lockFilePath string, curVersion *semver.Version) (*os.File, error) { func checkSingleInstance(lockFilePath string, curVersion *semver.Version) (*os.File, error) {
if lock, err := singleinstance.CreateLockFile(lockFilePath); err == nil { if lock, err := singleinstance.CreateLockFile(lockFilePath); err == nil {
logrus.WithField("path", lockFilePath).Debug("Created lock file; no other instance is running")
return lock, nil return lock, nil
} }
logrus.Debug("Failed to create lock file; another instance is running")
// We couldn't create the lock file, so another instance is probably running. // We couldn't create the lock file, so another instance is probably running.
// Check if it's an older version of the app. // Check if it's an older version of the app.
lastVersion, ok := focus.TryVersion() lastVersion, ok := focus.TryVersion()

View File

@ -21,7 +21,6 @@ import (
"context" "context"
"fmt" "fmt"
"net" "net"
"time"
"github.com/Masterminds/semver/v3" "github.com/Masterminds/semver/v3"
"github.com/ProtonMail/proton-bridge/v2/internal/focus/proto" "github.com/ProtonMail/proton-bridge/v2/internal/focus/proto"
@ -34,65 +33,66 @@ import (
// TryRaise tries to raise the application by dialing the focus service. // TryRaise tries to raise the application by dialing the focus service.
// It returns true if the service is running and the application was told to raise. // It returns true if the service is running and the application was told to raise.
func TryRaise() bool { func TryRaise() bool {
ctx, cancel := context.WithTimeout(context.Background(), time.Second) var raised bool
defer cancel()
cc, err := grpc.DialContext( if err := withClientConn(context.Background(), func(ctx context.Context, client proto.FocusClient) error {
ctx, if _, err := client.Raise(ctx, &emptypb.Empty{}); err != nil {
net.JoinHostPort(Host, fmt.Sprint(Port)), return fmt.Errorf("failed to call client.Raise: %w", err)
grpc.WithTransportCredentials(insecure.NewCredentials()), }
)
if err != nil { raised = true
return nil
}); err != nil {
logrus.WithError(err).Debug("Failed to raise application")
return false return false
} }
defer func() { return raised
if err := cc.Close(); err != nil {
logrus.WithError(err).Warn("Failed to close focus connection")
}
}()
if _, err := proto.NewFocusClient(cc).Raise(ctx, &emptypb.Empty{}); err != nil {
return false
} }
if err := cc.Close(); err != nil { // TryVersion tries to determine the version of the running application instance.
return false // It returns the version and true if the version could be determined.
}
return true
}
// TryVersion tries to raise the application by dialing the focus service.
// It returns true if the service is running and the application was told to raise.
func TryVersion() (*semver.Version, bool) { func TryVersion() (*semver.Version, bool) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second) var version *semver.Version
defer cancel()
cc, err := grpc.DialContext( if err := withClientConn(context.Background(), func(ctx context.Context, client proto.FocusClient) error {
ctx, raw, err := client.Version(ctx, &emptypb.Empty{})
net.JoinHostPort(Host, fmt.Sprint(Port)),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil { if err != nil {
return nil, false return fmt.Errorf("failed to call client.Version: %w", err)
} }
defer func() { parsed, err := semver.NewVersion(raw.GetVersion())
if err := cc.Close(); err != nil {
logrus.WithError(err).Warn("Failed to close focus connection")
}
}()
raw, err := proto.NewFocusClient(cc).Version(ctx, &emptypb.Empty{})
if err != nil { if err != nil {
return nil, false return fmt.Errorf("failed to parse version: %w", err)
} }
version, err := semver.NewVersion(raw.GetVersion()) version = parsed
if err != nil {
return nil
}); err != nil {
logrus.WithError(err).Debug("Failed to determine version of running instance")
return nil, false return nil, false
} }
return version, true return version, true
} }
func withClientConn(ctx context.Context, fn func(context.Context, proto.FocusClient) error) error {
cc, err := grpc.DialContext(
ctx,
net.JoinHostPort(Host, fmt.Sprint(Port)),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
return err
}
defer func() {
if err := cc.Close(); err != nil {
logrus.WithError(err).Warn("Failed to close focus connection")
}
}()
return fn(ctx, proto.NewFocusClient(cc))
}