Other: Single instance

This commit is contained in:
James Houlahan
2022-10-12 23:30:43 +02:00
parent fd63611b41
commit 593d86f3a7
16 changed files with 470 additions and 73 deletions

View File

@ -6,7 +6,9 @@ import (
"net"
"time"
"github.com/Masterminds/semver/v3"
"github.com/ProtonMail/proton-bridge/v2/internal/focus/proto"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/protobuf/types/known/emptypb"
@ -27,6 +29,12 @@ func TryRaise() bool {
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
}
@ -37,3 +45,37 @@ func TryRaise() bool {
return true
}
// 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 TryVersion() (*semver.Version, bool) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
cc, err := grpc.DialContext(
ctx,
net.JoinHostPort(Host, fmt.Sprint(Port)),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
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{})
if err != nil {
return nil, false
}
version, err := semver.NewVersion(raw.GetVersion())
if err != nil {
return nil, false
}
return version, true
}