mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-15 14:56:42 +00:00
feat(BRIDGE-379): mailbox pre-checker on startup & conflict resolver for bridge internal mailboxes; TODO potentially add this for system mailboxes as well
This commit is contained in:
@ -19,10 +19,12 @@ package syncservice
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/ProtonMail/gluon/async"
|
||||
"github.com/ProtonMail/gluon/db"
|
||||
"github.com/ProtonMail/gluon/reporter"
|
||||
"github.com/ProtonMail/go-proton-api"
|
||||
"github.com/ProtonMail/proton-bridge/v3/internal/network"
|
||||
@ -34,6 +36,10 @@ const NumSyncStages = 4
|
||||
|
||||
type LabelMap = map[string]proton.Label
|
||||
|
||||
type labelConflictChecker interface {
|
||||
CheckAndReportConflicts(ctx context.Context, labels map[string]proton.Label) error
|
||||
}
|
||||
|
||||
// Handler is the interface from which we control the syncing of the IMAP data. One instance should be created for each
|
||||
// user and used for every subsequent sync request.
|
||||
type Handler struct {
|
||||
@ -95,12 +101,17 @@ func (t *Handler) Execute(
|
||||
updateApplier UpdateApplier,
|
||||
messageBuilder MessageBuilder,
|
||||
coolDown time.Duration,
|
||||
labelConflictChecker labelConflictChecker,
|
||||
) {
|
||||
t.log.Info("Sync triggered")
|
||||
t.group.Once(func(ctx context.Context) {
|
||||
start := time.Now()
|
||||
t.log.WithField("start", start).Info("Beginning user sync")
|
||||
|
||||
if err := labelConflictChecker.CheckAndReportConflicts(ctx, labels); err != nil {
|
||||
t.log.WithError(err).Error("Failed to check and report label conflicts")
|
||||
}
|
||||
|
||||
syncReporter.OnStart(ctx)
|
||||
var err error
|
||||
for {
|
||||
@ -108,11 +119,18 @@ func (t *Handler) Execute(
|
||||
t.log.WithError(err).Error("Sync aborted")
|
||||
break
|
||||
} else if err = t.run(ctx, syncReporter, labels, updateApplier, messageBuilder); err != nil {
|
||||
if sentryErr := t.sentryReporter.ReportMessageWithContext("Failed to sync, will retry later", reporter.Context{
|
||||
"err": err.Error(),
|
||||
"user_id": t.userID,
|
||||
}); sentryErr != nil {
|
||||
t.log.WithError(sentryErr).Error("Failed to report sentry message")
|
||||
if db.IsUniqueLabelConstraintError(err) {
|
||||
if sentryErr := t.sentryReporter.ReportMessageWithContext("Failed to sync due to label unique constraint conflict",
|
||||
reporter.Context{"err": err}); sentryErr != nil {
|
||||
t.log.WithError(sentryErr).Error("Failed to report label unique constraint conflict error to Sentry")
|
||||
}
|
||||
} else if !(errors.Is(err, context.Canceled)) {
|
||||
if sentryErr := t.sentryReporter.ReportMessageWithContext("Failed to sync, will retry later", reporter.Context{
|
||||
"err": err.Error(),
|
||||
"user_id": t.userID,
|
||||
}); sentryErr != nil {
|
||||
t.log.WithError(sentryErr).Error("Failed to report sentry message")
|
||||
}
|
||||
}
|
||||
|
||||
t.log.WithError(err).Error("Failed to sync, will retry later")
|
||||
|
||||
Reference in New Issue
Block a user