Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix(notifications): stop re-sending image update pushes on every poll
Kyle Mendellcd7c39cfcb6c
diff --git a/backend/internal/apns/service.go b/backend/internal/apns/service.goindex 6b77e0f43..99e58f585 100644--- a/backend/internal/apns/service.go+++ b/backend/internal/apns/service.go@@ -441,13 +441,15 @@ func truncateInternal(s string, limit int) string {return string(r[:limit-1]) + "…"}-func (s *ApnsService) Enqueue(ctx context.Context, environmentID, environmentName string, eventType notifications.NotificationEventType, subject string, metadata database.JSON) error {+// Enqueue queues a push for every subscribed device and reports whether one was+// queued. The outbox retries delivery to the relay, so a queued push counts as delivered.+func (s *ApnsService) Enqueue(ctx context.Context, environmentID, environmentName string, eventType notifications.NotificationEventType, subject string, metadata database.JSON) (bool, error) {if !s.Enabled(ctx) {- return nil+ return false, nil}recipients, err := s.recipientsInternal(ctx, environmentID, eventType)if err != nil || len(recipients) == 0 {- return err+ return false, err}batchCount := 0@@ -499,11 +501,11 @@ func (s *ApnsService) Enqueue(ctx context.Context, environmentID, environmentNam}raw, err := json.Marshal(envelope)if err != nil {- return fmt.Errorf("failed to marshal push envelope: %w", err)+ return false, fmt.Errorf("failed to marshal push envelope: %w", err)}entry := OutboxEntry{EventID: envelope.EventID, Envelope: string(raw), NextAttemptAt: time.Now()}if enqueueNotificationErr := s.db.WithContext(ctx).Create(&entry).Error; enqueueNotificationErr != nil {- return fmt.Errorf("failed to enqueue push notification: %w", enqueueNotificationErr)+ return false, fmt.Errorf("failed to enqueue push notification: %w", enqueueNotificationErr)}go func() {drainCtx := context.WithoutCancel(ctx)@@ -511,7 +513,7 @@ func (s *ApnsService) Enqueue(ctx context.Context, environmentID, environmentNamslog.WarnContext(drainCtx, "Push outbox drain failed", "error", drainOutboxErr)}}()- return nil+ return true, nil}func (s *ApnsService) recipientsInternal(ctx context.Context, environmentID string, eventType notifications.NotificationEventType) ([]string, error) {diff --git a/backend/internal/notification/service.go b/backend/internal/notification/service.goindex 2eb59ce31..9b67bbf62 100644--- a/backend/internal/notification/service.go+++ b/backend/internal/notification/service.go@@ -633,9 +633,15 @@ func (s *NotificationService) notifyEnabledProviders(}if s.apnsSvc != nil {- if enqueueErr := s.apnsSvc.Enqueue(ctx, target.EnvironmentID, target.EnvironmentName, eventType, logRef, metadata); enqueueErr != nil {+ // A queued push counts as a delivery, so push-only setups still mark+ // updates notified instead of re-sending them on every poll.+ enqueued, enqueueErr := s.apnsSvc.Enqueue(ctx, target.EnvironmentID, target.EnvironmentName, eventType, logRef, metadata)+ if enqueueErr != nil {slog.WarnContext(ctx, "Failed to enqueue mobile push notification", "error", enqueueErr)}+ if enqueued {+ delivered+++ }}if len(errs) > 0 {@@ -880,8 +886,13 @@ func (s *NotificationService) sendBatchImageUpdateNotificationForTarget(ctx cont}content := s.templates.BatchImageUpdate(target.EnvironmentName, updatesWithChanges)content.Vars = notifications.EventVars(target.EnvironmentName, target.EnvironmentID, notifications.NotificationEventImageUpdate)- logRef := strings.Join(slices.Collect(maps.Keys(updatesWithChanges)), ", ")- return s.notifyEnabledProviders(ctx, target, notifications.NotificationEventImageUpdate, logRef, metadata, content)+ return s.notifyEnabledProviders(ctx, target, notifications.NotificationEventImageUpdate, batchImageUpdateLogRefInternal(updatesWithChanges), metadata, content)+}++// batchImageUpdateLogRefInternal lists the image refs in sorted order. The push body is built+// from it, and the relay dedupes on that body, so a repeat of the same set must read the same.+func batchImageUpdateLogRefInternal(updates map[string]*imageupdate.Response) string {+ return strings.Join(slices.Sorted(maps.Keys(updates)), ", ")}func (s *NotificationService) SendPruneReportNotification(ctx context.Context, result *system.PruneAllResult) error {diff --git a/backend/internal/notification/service_test.go b/backend/internal/notification/service_test.goindex 555d4ead7..8920fce56 100644--- a/backend/internal/notification/service_test.go+++ b/backend/internal/notification/service_test.go@@ -6,6 +6,7 @@ import ("encoding/base64""encoding/json""log/slog"+ "maps""net/http""net/http/httptest""strings"@@ -793,3 +794,15 @@ func TestNotificationService_AgentDispatchHTTPFailureFallsBackToTunnel(t *testinrequire.Error(t, err)require.Contains(t, err.Error(), "failed to dispatch notification to manager")}++func TestBatchImageUpdateLogRef_IsSortedSoRepeatsMatch(t *testing.T) {+ updates := map[string]*imageupdate.Response{}+ for _, ref := range []string{"redis:7", "nginx:latest", "postgres:16", "alpine:3", "caddy:2"} {+ updates[ref] = newNotificationTestUpdateInfo()+ }++ want := "alpine:3, caddy:2, nginx:latest, postgres:16, redis:7"+ for range 20 {+ require.Equal(t, want, batchImageUpdateLogRefInternal(maps.Clone(updates)))+ }+}