forked from Silverfish/proton-bridge
GODT-2010: Add better logging for app focus feature
This commit is contained in:
@ -31,6 +31,7 @@ import (
|
||||
"github.com/Masterminds/semver/v3"
|
||||
"github.com/ProtonMail/proton-bridge/v2/internal/focus"
|
||||
"github.com/allan-simon/go-singleinstance"
|
||||
"github.com/sirupsen/logrus"
|
||||
"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).
|
||||
func checkSingleInstance(lockFilePath string, curVersion *semver.Version) (*os.File, error) {
|
||||
if lock, err := singleinstance.CreateLockFile(lockFilePath); err == nil {
|
||||
logrus.WithField("path", lockFilePath).Debug("Created lock file; no other instance is running")
|
||||
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.
|
||||
// Check if it's an older version of the app.
|
||||
lastVersion, ok := focus.TryVersion()
|
||||
|
||||
@ -21,7 +21,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/Masterminds/semver/v3"
|
||||
"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.
|
||||
// It returns true if the service is running and the application was told to raise.
|
||||
func TryRaise() bool {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
var raised bool
|
||||
|
||||
cc, err := grpc.DialContext(
|
||||
ctx,
|
||||
net.JoinHostPort(Host, fmt.Sprint(Port)),
|
||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||
)
|
||||
if err != nil {
|
||||
if err := withClientConn(context.Background(), func(ctx context.Context, client proto.FocusClient) error {
|
||||
if _, err := client.Raise(ctx, &emptypb.Empty{}); err != nil {
|
||||
return fmt.Errorf("failed to call client.Raise: %w", err)
|
||||
}
|
||||
|
||||
raised = true
|
||||
|
||||
return nil
|
||||
}); err != nil {
|
||||
logrus.WithError(err).Debug("Failed to raise application")
|
||||
return false
|
||||
}
|
||||
|
||||
defer func() {
|
||||
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 {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
return raised
|
||||
}
|
||||
|
||||
// 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.
|
||||
// TryVersion tries to determine the version of the running application instance.
|
||||
// It returns the version and true if the version could be determined.
|
||||
func TryVersion() (*semver.Version, bool) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
var version *semver.Version
|
||||
|
||||
cc, err := grpc.DialContext(
|
||||
ctx,
|
||||
net.JoinHostPort(Host, fmt.Sprint(Port)),
|
||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||
)
|
||||
if err := withClientConn(context.Background(), func(ctx context.Context, client proto.FocusClient) error {
|
||||
raw, err := client.Version(ctx, &emptypb.Empty{})
|
||||
if err != nil {
|
||||
return nil, false
|
||||
return fmt.Errorf("failed to call client.Version: %w", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err := cc.Close(); err != nil {
|
||||
logrus.WithError(err).Warn("Failed to close focus connection")
|
||||
}
|
||||
}()
|
||||
|
||||
raw, err := proto.NewFocusClient(cc).Version(ctx, &emptypb.Empty{})
|
||||
parsed, err := semver.NewVersion(raw.GetVersion())
|
||||
if err != nil {
|
||||
return nil, false
|
||||
return fmt.Errorf("failed to parse version: %w", err)
|
||||
}
|
||||
|
||||
version, err := semver.NewVersion(raw.GetVersion())
|
||||
if err != nil {
|
||||
version = parsed
|
||||
|
||||
return nil
|
||||
}); err != nil {
|
||||
logrus.WithError(err).Debug("Failed to determine version of running instance")
|
||||
return nil, false
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user