GODT-1978: Don't install the same update twice

This commit is contained in:
James Houlahan
2022-11-04 15:53:41 +01:00
parent b9ffa96e8b
commit d74873be31
3 changed files with 59 additions and 42 deletions

View File

@ -73,9 +73,14 @@ type Bridge struct {
// updater is the bridge's updater. // updater is the bridge's updater.
updater Updater updater Updater
curVersion *semver.Version
installCh chan installJob installCh chan installJob
// curVersion is the current version of the bridge,
// newVersion is the version that was installed by the updater.
curVersion *semver.Version
newVersion *semver.Version
newVersionLock safe.RWMutex
// focusService is used to raise the bridge window when needed. // focusService is used to raise the bridge window when needed.
focusService *focus.Service focusService *focus.Service
@ -241,8 +246,11 @@ func newBridge(
imapEventCh: imapEventCh, imapEventCh: imapEventCh,
updater: updater, updater: updater,
installCh: make(chan installJob),
curVersion: curVersion, curVersion: curVersion,
installCh: make(chan installJob, 1), newVersion: curVersion,
newVersionLock: safe.NewRWMutex(),
focusService: focusService, focusService: focusService,
autostarter: autostarter, autostarter: autostarter,

View File

@ -21,6 +21,7 @@ import (
"context" "context"
"github.com/ProtonMail/proton-bridge/v2/internal/events" "github.com/ProtonMail/proton-bridge/v2/internal/events"
"github.com/ProtonMail/proton-bridge/v2/internal/safe"
"github.com/ProtonMail/proton-bridge/v2/internal/updater" "github.com/ProtonMail/proton-bridge/v2/internal/updater"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -86,14 +87,10 @@ func (bridge *Bridge) handleUpdate(version updater.VersionInfo) {
}) })
default: default:
safe.RLock(func() {
if version.Version.GreaterThan(bridge.newVersion) {
log.Info("An update is available") log.Info("An update is available")
bridge.publish(events.UpdateAvailable{
Version: version,
Compatible: true,
Silent: true,
})
select { select {
case bridge.installCh <- installJob{version: version, silent: true}: case bridge.installCh <- installJob{version: version, silent: true}:
log.Info("The update will be installed silently") log.Info("The update will be installed silently")
@ -102,6 +99,8 @@ func (bridge *Bridge) handleUpdate(version updater.VersionInfo) {
log.Info("An update is already being installed") log.Info("An update is already being installed")
} }
} }
}, bridge.newVersionLock)
}
} }
type installJob struct { type installJob struct {
@ -110,12 +109,19 @@ type installJob struct {
} }
func (bridge *Bridge) installUpdate(ctx context.Context, job installJob) { func (bridge *Bridge) installUpdate(ctx context.Context, job installJob) {
safe.Lock(func() {
log := logrus.WithFields(logrus.Fields{ log := logrus.WithFields(logrus.Fields{
"version": job.version.Version, "version": job.version.Version,
"current": bridge.curVersion, "current": bridge.curVersion,
"channel": bridge.vault.GetUpdateChannel(), "channel": bridge.vault.GetUpdateChannel(),
}) })
bridge.publish(events.UpdateAvailable{
Version: job.version,
Compatible: true,
Silent: job.silent,
})
bridge.publish(events.UpdateInstalling{ bridge.publish(events.UpdateInstalling{
Version: job.version, Version: job.version,
Silent: job.silent, Silent: job.silent,
@ -136,5 +142,8 @@ func (bridge *Bridge) installUpdate(ctx context.Context, job installJob) {
Version: job.version, Version: job.version,
Silent: job.silent, Silent: job.silent,
}) })
bridge.newVersion = job.version.Version
} }
}, bridge.newVersionLock)
} }

View File

@ -109,5 +109,5 @@ type UpdateForced struct {
} }
func (event UpdateForced) String() string { func (event UpdateForced) String() string {
return fmt.Sprintf("UpdateForced") return "UpdateForced"
} }