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/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()
|
||||||
|
|||||||
@ -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 {
|
|
||||||
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 {
|
raised = true
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}); err != nil {
|
||||||
|
logrus.WithError(err).Debug("Failed to raise application")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := cc.Close(); err != nil {
|
return raised
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TryVersion tries to raise the application by dialing the focus service.
|
// TryVersion tries to determine the version of the running application instance.
|
||||||
// It returns true if the service is running and the application was told to raise.
|
// It returns the version and true if the version could be determined.
|
||||||
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)),
|
if err != nil {
|
||||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
return fmt.Errorf("failed to call client.Version: %w", err)
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
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))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user