Skip to content

Modern Docker Management, Designed for Everyone

perf: batch queries and cache hot paths across services (#4438)

Kyle Mendella6b79438f328
diff --git a/backend/internal/activity/service.go b/backend/internal/activity/service.go
index 8378f5c0f..d229abe88 100644
--- a/backend/internal/activity/service.go
+++ b/backend/internal/activity/service.go
@@ -42,6 +42,9 @@ const (
// StartActivity's row creation and the worker's Track call.
abandonedActivityGrace = 2 * time.Minute
+ // abandonedActivitySweepLimit bounds one sweep; later sweeps take the rest.
+ abandonedActivitySweepLimit = 500
+
// terminalPublishRetention bounds how long a terminal publish suppresses
// stale non-terminal snapshots for the same activity. Stale publishers are
// goroutines already past their commit, so they publish within moments; the
@@ -971,59 +974,65 @@ func (s *ActivityService) FailAbandonedActivities(ctx context.Context) (int64, e
return 0, nil
}
- cutoff := time.Now().Add(-abandonedActivityGrace)
activeStatuses := []activity.Status{activity.StatusQueued, activity.StatusRunning}
+ s.runningMu.Lock()
+ trackedIDs := slices.Collect(maps.Keys(s.running))
+ s.runningMu.Unlock()
+ query := s.db.WithContext(ctx).
+ Select("id", "started_at").
+ Where("status IN ? AND started_at < ?", activeStatuses, time.Now().Add(-abandonedActivityGrace)).
+ Where("type <> ?", activity.TypeJobRun)
+ if len(trackedIDs) > 0 {
+ query = query.Where("id NOT IN ?", trackedIDs)
+ }
var candidates []Activity
- if err := s.db.WithContext(ctx).
- Where("status IN ? AND started_at < ?", activeStatuses, cutoff).
- Where("type <> ?", activity.TypeJobRun).
- Find(&candidates).Error; err != nil {
+ if err := query.Order("started_at").Limit(abandonedActivitySweepLimit).Find(&candidates).Error; err != nil {
return 0, fmt.Errorf("find abandoned activities: %w", err)
}
+ // A worker may have Tracked its activity since the snapshot above.
+ candidates = slices.DeleteFunc(candidates, func(candidate Activity) bool { return s.isTrackedInternal(candidate.ID) })
+ if len(candidates) == 0 {
+ return 0, nil
+ }
const message = "Activity was marked failed because its worker is no longer running"
- errMessage := message
- var swept int64
- var sweepErrs []error
- for i := range candidates {
- activityID := candidates[i].ID
- if s.isTrackedInternal(activityID) {
- continue
- }
-
- now := time.Now()
- var finalized Activity
- lostRace := false
- if err := s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
- updates := completeActivityUpdatesInternal(candidates[i].StartedAt, activity.StatusFailed, message, &errMessage, nil, now)
- result := tx.Model(&Activity{}).
- Where("id = ? AND status IN ?", activityID, activeStatuses).
- Updates(updates)
- if result.Error != nil {
- return fmt.Errorf("fail abandoned activity: %w", result.Error)
- }
- if result.RowsAffected == 0 {
- lostRace = true
- return nil
- }
- if err := tx.First(&finalized, "id = ?", activityID).Error; err != nil {
- return fmt.Errorf("load failed abandoned activity: %w", err)
- }
- return nil
- }); err != nil {
- sweepErrs = append(sweepErrs, fmt.Errorf("sweep activity %s: %w", activityID, err))
- continue
+ now := time.Now()
+ ids := make([]string, 0, len(candidates))
+ var durationExpr strings.Builder
+ durationArgs := make([]any, 0, 2*len(candidates))
+ durationExpr.WriteString("CASE id")
+ for _, candidate := range candidates {
+ ids = append(ids, candidate.ID)
+ durationExpr.WriteString(" WHEN ? THEN ?")
+ durationArgs = append(durationArgs, candidate.ID, now.Sub(candidate.StartedAt).Milliseconds())
+ }
+ durationExpr.WriteString(" ELSE duration_ms END")
+ updates := completeActivityUpdatesInternal(now, activity.StatusFailed, message, new(message), nil, now)
+ updates["duration_ms"] = gorm.Expr(durationExpr.String(), durationArgs...)
+
+ // Status-guarded so a worker completing concurrently wins; only rows this
+ // sweep failed carry its message, so the reload skips the race losers.
+ var finalized []Activity
+ if err := s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
+ if err := tx.Model(&Activity{}).
+ Where("id IN ? AND status IN ?", ids, activeStatuses).
+ Updates(updates).Error; err != nil {
+ return fmt.Errorf("fail abandoned activities: %w", err)
}
- if lostRace {
- continue
+ if err := tx.Where("id IN ? AND status = ? AND error = ?", ids, activity.StatusFailed, message).Find(&finalized).Error; err != nil {
+ return fmt.Errorf("load failed abandoned activities: %w", err)
}
+ return nil
+ }); err != nil {
+ return 0, fmt.Errorf("sweep abandoned activities: %w", err)
+ }
- s.releaseSlotInternal(activityID)
- s.publishTerminalSnapshotInternal(ctx, &finalized)
- swept++
+ for i := range finalized {
+ s.releaseSlotInternal(finalized[i].ID)
+ s.publishTerminalSnapshotInternal(ctx, &finalized[i])
}
- return swept, errors.Join(sweepErrs...)
+ return int64(len(finalized)), nil
}
// ResolveOrphanedQueuedActivities fails any activity still queued at startup.
@@ -1167,11 +1176,11 @@ func (s *ActivityService) ListActivitiesPaginated(ctx context.Context, environme
if term := strings.TrimSpace(params.Search); term != "" {
escaped := strings.NewReplacer(`\`, `\\`, `%`, `\%`, `_`, `\_`).Replace(term)
searchPattern := "%" + escaped + "%"
+ // Matches the Activity Center's own search fields.
q = q.Where(
- "type LIKE ? ESCAPE '\\' OR COALESCE(resource_name, '') LIKE ? ESCAPE '\\' OR COALESCE(latest_message, "+
- "'') LIKE ? ESCAPE '\\' OR COALESCE(step, '') LIKE ? ESCAPE '\\' OR COALESCE(error, '') LIKE ? ESCAPE "+
- "'\\'",
- searchPattern, searchPattern, searchPattern, searchPattern, searchPattern,
+ "type LIKE ? ESCAPE '\\' OR COALESCE(resource_name, '') LIKE ? ESCAPE '\\' OR "+
+ "COALESCE(latest_message, '') LIKE ? ESCAPE '\\' OR COALESCE(error, '') LIKE ? ESCAPE '\\'",
+ searchPattern, searchPattern, searchPattern, searchPattern,
)
}
diff --git a/backend/internal/apikey/service.go b/backend/internal/apikey/service.go
index 1bab990ea..ea9c97277 100644
--- a/backend/internal/apikey/service.go
+++ b/backend/internal/apikey/service.go
@@ -143,12 +143,16 @@ func (s *ApiKeyService) BackfillApiKeyPermissions(ctx context.Context) error {
}
return dbutil.WithTx(ctx, s.db.DB, func(tx *gorm.DB) error {
+ keyIDs := make([]string, 0, len(keys))
for _, key := range keys {
- var existing int64
- if err := tx.Model(&role.ApiKeyPermission{}).Where("api_key_id = ?", key.ID).Count(&existing).Error; err != nil {
- return fmt.Errorf("failed to count permissions for api key %s: %w", key.ID, err)
- }
- if existing > 0 {
+ keyIDs = append(keyIDs, key.ID)
+ }
+ var grantedKeyIDs []string
+ if err := tx.Model(&role.ApiKeyPermission{}).Where("api_key_id IN ?", keyIDs).Distinct().Pluck("api_key_id", &grantedKeyIDs).Error; err != nil {
+ return fmt.Errorf("failed to count permissions for bootstrap api keys: %w", err)
+ }
+ for _, key := range keys {
+ if slices.Contains(grantedKeyIDs, key.ID) {
continue
}
perms, err := s.backfillPermsForKeyInternal(ctx, tx, key)
diff --git a/backend/internal/backup/service.go b/backend/internal/backup/service.go
index 2c8b8dfa6..9fd7d45f3 100644
--- a/backend/internal/backup/service.go
+++ b/backend/internal/backup/service.go
@@ -823,12 +823,10 @@ func (r PolicyReconciliation[P, U]) Run(ctx context.Context, updates []U) error
return saveErr
}
}
- for i := range removed {
- if deleteErr := tx.Delete(&removed[i]).Error; deleteErr != nil {
- return deleteErr
- }
+ if len(removed) == 0 {
+ return nil
}
- return nil
+ return tx.Delete(&removed).Error
})
}
if persistErr != nil {
diff --git a/backend/internal/build/service.go b/backend/internal/build/service.go
index 2bcd70d9f..51285844f 100644
--- a/backend/internal/build/service.go
+++ b/backend/internal/build/service.go
@@ -10,6 +10,7 @@ import (
"net/url"
"os"
"path/filepath"
+ "slices"
"strings"
"time"
@@ -52,7 +53,10 @@ type BuildService struct {
gitCleanupFn func(string) error
}
-const buildHistoryOutputLimitBytes = 2 * 1024 * 1024
+const (
+ buildHistoryOutputLimitBytes = 2 * 1024 * 1024
+ deleteBuildsBatchSize = 500
+)
func NewBuildService(
db *database.DB,
@@ -603,6 +607,35 @@ func (s *BuildService) completeBuildRecord(
})
}
+// PruneHistory deletes finished and failed builds completed before the
+// retention window. Running builds are never pruned.
+func (s *BuildService) PruneHistory(ctx context.Context, retentionDays int) (int64, error) {
+ if s.db == nil || retentionDays <= 0 {
+ return 0, nil
+ }
+
+ var buildIDs []string
+ if err := s.db.WithContext(ctx).Model(&ImageBuild{}).
+ Where("status IN ?", []ImageBuildStatus{ImageBuildStatusSuccess, ImageBuildStatusFailed}).
+ Where("COALESCE(completed_at, updated_at, created_at) < ?", time.Now().Add(-time.Duration(retentionDays)*24*time.Hour)).
+ Pluck("id", &buildIDs).Error; err != nil {
+ return 0, fmt.Errorf("failed to find builds older than retention window: %w", err)
+ }
+
+ var deleted int64
+ err := s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
+ for batch := range slices.Chunk(buildIDs, deleteBuildsBatchSize) {
+ result := tx.Where("id IN ?", batch).Delete(&ImageBuild{})
+ if result.Error != nil {
+ return fmt.Errorf("failed to delete builds: %w", result.Error)
+ }
+ deleted += result.RowsAffected
+ }
+ return nil
+ })
+ return deleted, err
+}
+
func buildToRecord(build ImageBuild, includeOutput bool) image.BuildRecord {
buildArgs := jsonToStringMap(build.BuildArgs)
labels := jsonToStringMap(build.Labels)
diff --git a/backend/internal/build/service_test.go b/backend/internal/build/service_test.go
index a56e8ece5..225b0a539 100644
--- a/backend/internal/build/service_test.go
+++ b/backend/internal/build/service_test.go
@@ -476,6 +476,58 @@ func TestBuildService_ListImageBuilds_OmitsOutputColumn(t *testing.T) {
assert.Equal(t, output, *detail.Output)
}
+func TestBuildService_PruneHistory_DeletesOnlyOldTerminalBuilds(t *testing.T) {
+ db, err := setupBuildHistoryTestDB()
+ require.NoError(t, err)
+ svc := &BuildService{db: db}
+
+ old := time.Now().Add(-40 * 24 * time.Hour)
+ recent := time.Now().Add(-time.Hour)
+ for _, build := range []ImageBuild{
+ {
+ ID: "old-success",
+ CreatedAt: old,
+ EnvironmentID: "0",
+ Status: ImageBuildStatusSuccess,
+ CompletedAt: &old,
+ },
+ {
+ ID: "old-failed",
+ CreatedAt: old,
+ EnvironmentID: "0",
+ Status: ImageBuildStatusFailed,
+ CompletedAt: &old,
+ },
+ {
+ ID: "old-running",
+ CreatedAt: old,
+ EnvironmentID: "0",
+ Status: ImageBuildStatusRunning,
+ },
+ {
+ ID: "recent-success",
+ CreatedAt: recent,
+ EnvironmentID: "0",
+ Status: ImageBuildStatusSuccess,
+ CompletedAt: &recent,
+ },
+ } {
+ require.NoError(t, db.WithContext(t.Context()).Create(&build).Error)
+ }
+
+ deleted, err := svc.PruneHistory(t.Context(), 30)
+ require.NoError(t, err)
+ assert.Equal(t, int64(2), deleted)
+
+ var remaining []string
+ require.NoError(t, db.WithContext(t.Context()).Model(&ImageBuild{}).Order("id").Pluck("id", &remaining).Error)
+ assert.Equal(t, []string{"old-running", "recent-success"}, remaining)
+
+ deleted, err = svc.PruneHistory(t.Context(), 0)
+ require.NoError(t, err)
+ assert.Zero(t, deleted)
+}
+
func setupBuildHistoryTestDB() (*database.DB, error) {
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
if err != nil {
diff --git a/backend/internal/config/schema/schema_test.go b/backend/internal/config/schema/schema_test.go
index b9de69145..12912729c 100644
--- a/backend/internal/config/schema/schema_test.go
+++ b/backend/internal/config/schema/schema_test.go
@@ -293,6 +293,7 @@ var expectedSettingOverrideKeys = []string{
"autoUpdateInterval",
"avatarMaxUploadSizeMb",
"baseServerUrl",
+ "buildHistoryRetentionDays",
"buildProvider",
"buildTimeout",
"buildsDirectory",
diff --git a/backend/internal/environment/service.go b/backend/internal/environment/service.go
index 64680cfcf..e44bdee79 100644
--- a/backend/internal/environment/service.go
+++ b/backend/internal/environment/service.go
@@ -3,6 +3,7 @@ package environment
import (
"cmp"
"context"
+ "crypto/subtle"
"errors"
"fmt"
"hash/maphash"
@@ -66,22 +67,16 @@ type EnvironmentService struct {
remoteEnvs *remoteEnvSnapshotCache
environmentCache *hot.HotCache[environmentCacheKey, Environment]
environmentGens [environmentCacheStripes]atomic.Uint64
+ accessTokens *hot.HotCache[string, string]
+ jobs *entityjobs.Registry
- // jobs holds one health-check job per enabled environment; SetScheduler wires it on the manager.
- jobs *entityjobs.Registry
-
- // variableSyncer is set by SetVariableSyncer on the manager to avoid a wire cycle.
- variableSyncer VariableSyncer
-
+ variableSyncer VariableSyncer
runtimeWatchers runtimeWatchers
-
- // syncGate skips agent config pushes while the last accepted payload is unchanged.
- syncGate *utils.SyncGate
-
- proxy *proxy.Service
- health *health.Service
- snippets *snippets.Service
- swarm *swarm.Service
+ syncGate *utils.SyncGate
+ proxy *proxy.Service
+ health *health.Service
+ snippets *snippets.Service
+ swarm *swarm.Service
}
const (
@@ -167,6 +162,7 @@ func NewEnvironmentService(
WithMissingSharedCache().
WithJanitor().
Build(),
+ accessTokens: hot.NewHotCache[string, string](hot.LRU, 1024).WithTTL(environmentCacheTTL).WithJanitor().Build(),
jobs: entityjobs.New(environmentHealthJobPrefix, environmentHealthAdmissionScope),
runtimeWatchers: runtimeWatchers{chans: make(map[int]chan struct{})},
proxy: proxy.New(db, httpClient, settingsService, syncGate),
@@ -612,6 +608,14 @@ func (s *EnvironmentService) ResolveEnvironmentByAccessToken(ctx context.Context
return nil, ErrEnvironmentAccessTokenRequired
}
+ if envID, ok, _ := s.accessTokens.Get(token); ok {
+ cached, err := s.GetEnvironmentByIDCached(ctx, envID)
+ if err == nil && subtle.ConstantTimeCompare([]byte(kit.FromPtr(cached.AccessToken)), []byte(token)) == 1 {
+ return cached, nil
+ }
+ s.accessTokens.Delete(token)
+ }
+
var env Environment
if err := s.db.WithContext(ctx).
Where("access_token = ?", token).
@@ -622,6 +626,7 @@ func (s *EnvironmentService) ResolveEnvironmentByAccessToken(ctx context.Context
return nil, fmt.Errorf("failed to resolve environment by access token: %w", err)
}
+ s.accessTokens.Set(token, env.ID)
return &env, nil
}
diff --git a/backend/internal/event/service.go b/backend/internal/event/service.go
index d8d80ef1f..bae2265e0 100644
--- a/backend/internal/event/service.go
+++ b/backend/internal/event/service.go
@@ -24,6 +24,7 @@ import (
"go.getarcane.app/kit/pkg/mapping"
"go.getarcane.app/streams/agg"
"go.getarcane.app/streams/bus"
+ "golang.org/x/sync/semaphore"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"gorm.io/gorm"
@@ -39,12 +40,16 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
)
+// maxConcurrentEdgeSyncs bounds in-flight event forwards to the manager.
+const maxConcurrentEdgeSyncs = 16
+
type EventService struct {
changes *concurrency.Signal[struct{}]
correlation *correlation.Service
db *database.DB
cfg *config.Config
httpClient *http.Client
+ edgeSyncs *semaphore.Weighted
}
func NewEventService(db *database.DB, cfg *config.Config, httpClient *http.Client) *EventService {
@@ -59,6 +64,7 @@ func NewEventService(db *database.DB, cfg *config.Config, httpClient *http.Clien
db: db,
cfg: cfg,
httpClient: httpClient,
+ edgeSyncs: semaphore.NewWeighted(maxConcurrentEdgeSyncs),
}
}
@@ -177,6 +183,12 @@ func (s *EventService) forwardEventToManager(ctx context.Context, eventModel *Ev
syncCtx, cancel := context.WithTimeout(context.WithoutCancel(parentCtx), 10*time.Second)
defer cancel()
+ if err := s.edgeSyncs.Acquire(syncCtx, 1); err != nil {
+ slog.WarnContext(syncCtx, "Dropped event sync to manager; too many in flight", "type", outgoing.Type, "error", err)
+ return
+ }
+ defer s.edgeSyncs.Release(1)
+
if err := edge.PublishEventToManager(syncCtx, outgoing); err != nil {
if !errors.Is(err, edge.ErrNoActiveAgentTunnel) {
slog.WarnContext(syncCtx, "Failed to sync event to manager over edge tunnel", "type", outgoing.Type, "error", err)
diff --git a/backend/internal/gitrepo/service.go b/backend/internal/gitrepo/service.go
index 926a17906..401b91df3 100644
--- a/backend/internal/gitrepo/service.go
+++ b/backend/internal/gitrepo/service.go
@@ -707,12 +707,17 @@ func (s *GitRepositoryService) createNewRepository(ctx context.Context, item git
}
func (s *GitRepositoryService) deleteUnsynced(ctx context.Context, existingMap map[string]*GitRepository, syncedIDs map[string]bool) error {
+ var unsyncedIDs []string
for id := range existingMap {
if !syncedIDs[id] {
- if err := s.db.WithContext(ctx).Delete(&GitRepository{}, "id = ?", id).Error; err != nil {
- return fmt.Errorf("failed to delete repository %s: %w", id, err)
- }
+ unsyncedIDs = append(unsyncedIDs, id)
}
}
+ if len(unsyncedIDs) == 0 {
+ return nil
+ }
+ if err := s.db.WithContext(ctx).Delete(&GitRepository{}, "id IN ?", unsyncedIDs).Error; err != nil {
+ return fmt.Errorf("failed to delete repositories %v: %w", unsyncedIDs, err)
+ }
return nil
}
diff --git a/backend/internal/image/service.go b/backend/internal/image/service.go
index e4887aa25..231eadad1 100644
--- a/backend/internal/image/service.go
+++ b/backend/internal/image/service.go
@@ -253,11 +253,11 @@ func (s *ImageService) GetImageDetail(ctx context.Context, id string) (*imagetyp
g.Go(func() (workerErr error) {
defer utils.RecoverToError(&workerErr, "image worker")
- imageList, imageListErr := dockerClient.ImageList(gctx, client.ImageListOptions{})
+ images, imageListErr := s.dockerService.ListImages(gctx)
if imageListErr != nil {
return fmt.Errorf("failed to list images: %w", imageListErr)
}
- for _, img := range imageList.Items {
+ for _, img := range images {
if img.ID == id {
listSize = img.Size
break
diff --git a/backend/internal/imageupdate/service.go b/backend/internal/imageupdate/service.go
index 4fd357e07..2e367b715 100644
--- a/backend/internal/imageupdate/service.go
+++ b/backend/internal/imageupdate/service.go
@@ -592,27 +592,22 @@ func (s *ImageUpdateService) getImageRefByID(ctx context.Context, imageID string
}
func (s *ImageUpdateService) getAllImageRefs(ctx context.Context, limit int) ([]string, error) {
- dockerClient, err := s.dockerClient(ctx)
- if err != nil {
- return nil, err
- }
-
imageCtx, cancelImage := s.dockerAPIContext(ctx)
- imageList, err := dockerClient.ImageList(imageCtx, client.ImageListOptions{})
+ images, err := s.dockerService.ListImages(imageCtx)
cancelImage()
if err != nil {
return nil, fmt.Errorf("failed to list Docker images: %w", err)
}
containerCtx, cancelContainers := s.dockerAPIContext(ctx)
- containerList, err := dockerClient.ContainerList(containerCtx, client.ContainerListOptions{All: true})
+ containers, err := s.dockerService.ListContainers(containerCtx)
cancelContainers()
if err != nil {
slog.WarnContext(ctx, "failed to list Docker containers; continuing without update-check opt-out filtering", "error", err.Error())
- return filterImageSummariesByContainerOptOut(imageList.Items, nil, limit), nil
+ return filterImageSummariesByContainerOptOut(images, nil, limit), nil
}
- return filterImageSummariesByContainerOptOut(imageList.Items, containerList.Items, limit), nil
+ return filterImageSummariesByContainerOptOut(images, containers, limit), nil
}
func newMonitoringEligibility(containers []container.Summary) monitoringEligibility {
diff --git a/backend/internal/job/service.go b/backend/internal/job/service.go
index 58c81a5e7..0ac2d8f07 100644
--- a/backend/internal/job/service.go
+++ b/backend/internal/job/service.go
@@ -25,6 +25,7 @@ import (
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
+ "gorm.io/gorm"
"github.com/getarcaneapp/arcane/backend/v2/internal/activity"
"github.com/getarcaneapp/arcane/backend/v2/internal/config"
@@ -672,6 +673,22 @@ func (s *JobService) reconcileRun(ctx context.Context, run scheduler.Run) (sched
return reconciler.Reconcile(s.runs.ExecutionContext(ctx, run, ""), run)
}
}
+ var activityIDs []string
+ for _, target := range run.Outcome.Targets {
+ if run.JobID != "auto-update" && target.Status != scheduler.Succeeded && target.Status != scheduler.Skipped && target.ActivityID != "" {
+ activityIDs = append(activityIDs, target.ActivityID)
+ }
+ }
+ var records []activity.Activity
+ if len(activityIDs) > 0 {
+ if err := s.db.WithContext(ctx).Select("id", "status").Where("id IN ?", activityIDs).Find(&records).Error; err != nil {
+ return scheduler.Outcome{Status: scheduler.Failed}, err
+ }
+ }
+ statusByID := make(map[string]activitytypes.Status, len(records))
+ for _, record := range records {
+ statusByID[record.ID] = record.Status
+ }
// A successful activity proves only that target, never the entire batch.
targets := make([]scheduler.TargetOutcome, 0, len(run.Outcome.Targets))
for index, target := range run.Outcome.Targets {
@@ -686,11 +703,11 @@ func (s *JobService) reconcileRun(ctx context.Context, run scheduler.Run) (sched
Targets: slices.Concat(targets, run.Outcome.Targets[index:]),
}, nil
}
- var record activity.Activity
- if err := s.db.WithContext(ctx).First(&record, "id = ?", target.ActivityID).Error; err != nil {
- return scheduler.Outcome{Status: scheduler.Failed}, err
+ status, found := statusByID[target.ActivityID]
+ if !found {
+ return scheduler.Outcome{Status: scheduler.Failed}, gorm.ErrRecordNotFound
}
- if record.Status != activitytypes.StatusSuccess {
+ if status != activitytypes.StatusSuccess {
return scheduler.Outcome{
Status: scheduler.Failed,
Message: "Interrupted operation has no confirmed completion",
diff --git a/backend/internal/network/service.go b/backend/internal/network/service.go
index 9050172e5..2708831e7 100644
--- a/backend/internal/network/service.go
+++ b/backend/internal/network/service.go
@@ -3,12 +3,16 @@ package network
import (
"cmp"
"context"
+ "errors"
"fmt"
"log/slog"
"net/netip"
"net/url"
"slices"
+ "strconv"
"strings"
+ "sync/atomic"
+ "time"
"github.com/getarcaneapp/arcane/types/v2/base"
networktypes "github.com/getarcaneapp/arcane/types/v2/network"
@@ -21,6 +25,7 @@ import (
"go.getarcane.app/kit/pkg"
"go.getarcane.app/kit/pkg/mapping"
"golang.org/x/sync/errgroup"
+ "golang.org/x/sync/singleflight"
"github.com/getarcaneapp/arcane/backend/v2/internal/common"
"github.com/getarcaneapp/arcane/backend/v2/internal/database"
@@ -32,10 +37,27 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
)
+const (
+ networkTopologyCacheTTL = 5 * time.Second
+ networkTopologyBuildTimeout = 30 * time.Second
+)
+
type NetworkService struct {
db *database.DB
dockerService *dockerInternal.DockerClientService
eventService *event.EventService
+
+ // Shared, immutable topology build; network mutations bump topologyGen,
+ // which retires cached entries and in-flight builds started before them.
+ topologyFlight singleflight.Group
+ topologyCache atomic.Pointer[networkTopologyCacheEntry]
+ topologyGen atomic.Uint64
+}
+
+type networkTopologyCacheEntry struct {
+ topology *networktypes.Topology
+ builtAt time.Time
+ gen uint64
}
func NewNetworkService(db *database.DB, dockerService *dockerInternal.DockerClientService, eventService *event.EventService) *NetworkService {
@@ -60,7 +82,46 @@ func (s *NetworkService) GetNetworkByID(ctx context.Context, id string) (*networ
return new(networkInspect.Network), nil
}
+// GetNetworkTopology returns a short-TTL cached topology shared by all callers; it must not be mutated.
func (s *NetworkService) GetNetworkTopology(ctx context.Context) (*networktypes.Topology, error) {
+ gen := s.topologyGen.Load()
+ if entry := s.topologyCache.Load(); entry != nil && entry.gen == gen && time.Since(entry.builtAt) < networkTopologyCacheTTL {
+ return entry.topology, nil
+ }
+
+ // Keyed by generation so callers after a network change never join an older build.
+ ch := s.topologyFlight.DoChan(strconv.FormatUint(gen, 10), func() (any, error) {
+ buildCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), networkTopologyBuildTimeout)
+ defer cancel()
+
+ topology, err := s.buildTopology(buildCtx)
+ if err != nil {
+ return nil, err
+ }
+ s.topologyCache.Store(&networkTopologyCacheEntry{
+ topology: topology,
+ builtAt: time.Now(),
+ gen: gen,
+ })
+ return topology, nil
+ })
+
+ select {
+ case <-ctx.Done():
+ return nil, ctx.Err()
+ case res := <-ch:
+ if res.Err != nil {
+ return nil, res.Err
+ }
+ topology, ok := res.Val.(*networktypes.Topology)
+ if !ok {
+ return nil, errors.New("network topology cache returned unexpected type")
+ }
+ return topology, nil
+ }
+}
+
+func (s *NetworkService) buildTopology(ctx context.Context) (*networktypes.Topology, error) {
dockerClient, err := s.dockerService.GetClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to connect to Docker: %w", err)
@@ -170,6 +231,7 @@ func (s *NetworkService) CreateNetwork(ctx context.Context, name string, options
s.eventService.LogErrorEvent(ctx, event.EventTypeNetworkError, "network", "", name, user.ID, user.Username, "0", err, database.JSON{"action": "create", "driver": options.Driver})
return nil, fmt.Errorf("failed to create network: %w", err)
}
+ s.topologyGen.Add(1)
if logErr := s.eventService.LogNetworkEvent(ctx, event.EventTypeNetworkCreate, response.ID, name, user.ID, user.Username, "0", database.JSON{
"action": "create",
@@ -200,6 +262,7 @@ func (s *NetworkService) RemoveNetwork(ctx context.Context, id string, user user
s.eventService.LogErrorEvent(ctx, event.EventTypeNetworkError, "network", id, networkName, user.ID, user.Username, "0", networkRemoveErr, database.JSON{"action": "delete"})
return fmt.Errorf("failed to remove network: %w", networkRemoveErr)
}
+ s.topologyGen.Add(1)
if logErr := s.eventService.LogNetworkEvent(ctx, event.EventTypeNetworkDelete, id, networkName, user.ID, user.Username, "0", database.JSON{
"action": "delete",
@@ -260,6 +323,7 @@ func (s *NetworkService) ConnectContainer(ctx context.Context, networkID string,
)
return fmt.Errorf("failed to connect container to network: %w", networkConnectErr)
}
+ s.topologyGen.Add(1)
if logErr := s.eventService.LogNetworkEvent(ctx, event.EventTypeNetworkConnect, networkID, networkID, user.ID, user.Username, "0", database.JSON{
"action": "connect",
@@ -301,6 +365,7 @@ func (s *NetworkService) DisconnectContainer(ctx context.Context, networkID stri
)
return fmt.Errorf("failed to disconnect container from network: %w", networkDisconnectErr)
}
+ s.topologyGen.Add(1)
if logErr := s.eventService.LogNetworkEvent(ctx, event.EventTypeNetworkDisconnect, networkID, networkID, user.ID, user.Username, "0", database.JSON{
"action": "disconnect",
@@ -324,6 +389,7 @@ func (s *NetworkService) PruneNetworks(ctx context.Context) (*network.PruneRepor
if err != nil {
return nil, fmt.Errorf("failed to prune networks: %w", err)
}
+ s.topologyGen.Add(1)
if logErr := s.eventService.LogNetworkEvent(ctx, event.EventTypeNetworkDelete, "", "bulk_prune", usertypes.SystemUser.ID, usertypes.SystemUser.Username, "0", database.JSON{
"action": "prune",
diff --git a/backend/internal/project/children/listing/service.go b/backend/internal/project/children/listing/service.go
index c79f8e7c5..8c609953b 100644
--- a/backend/internal/project/children/listing/service.go
+++ b/backend/internal/project/children/listing/service.go
@@ -546,6 +546,12 @@ func PaginationConfig() pagination.Config[project.Details] {
return strings.EqualFold(cmp.Or(strings.TrimSpace(kit.FromPtr(p.UpdateInfo).Status), "unknown"), strings.TrimSpace(filterValue))
},
},
+ {
+ Key: "name",
+ Fn: func(p project.Details, filterValue string) bool {
+ return slices.Contains(kit.TrimNonEmpty(strings.Split(filterValue, ",")), p.Name)
+ },
+ },
{
Key: "archived",
Fn: func(p project.Details, filterValue string) bool {
diff --git a/backend/internal/project/handler.go b/backend/internal/project/handler.go
index 4c8bef8c3..e7195863a 100644
--- a/backend/internal/project/handler.go
+++ b/backend/internal/project/handler.go
@@ -51,6 +51,7 @@ type ListProjectsInput struct {
Archived string `query:"archived" doc:"Archived filter: 'true' (only archived), 'all' (include archived). Default excludes archived."`
Tags string `query:"tags" doc:"Filter by tag names (comma-separated, OR semantics)"`
Label string `query:"label" doc:"Filter by container label key or key=value on any running service"`
+ Name string `query:"name" doc:"Filter by exact project name (comma-separated for several)"`
}
type GetProjectStatusCountsInput struct {
@@ -164,6 +165,9 @@ func (h *ProjectHandler) ListProjects(ctx context.Context, input *ListProjectsIn
if input.Label != "" {
params.Filters["label"] = input.Label
}
+ if input.Name != "" {
+ params.Filters["name"] = input.Name
+ }
if _, ok := httpx.EnvironmentScopeFromContext(ctx); ok {
body, err := h.projectService.AggregateProjects(ctx, params)
if err != nil {
diff --git a/backend/internal/project/service.go b/backend/internal/project/service.go
index 433d44b6e..f3eaf6fd8 100644
--- a/backend/internal/project/service.go
+++ b/backend/internal/project/service.go
@@ -2167,6 +2167,7 @@ func (s *ProjectService) ListProjects(ctx context.Context, params pagination.Que
query = query.Where("is_archived = ?", archived)
}
query = listing.ApplyProjectTagsDBFilter(query, strings.TrimSpace(params.Filters["tags"]))
+ query = pagination.ApplyFilter(query, "name", strings.TrimSpace(params.Filters["name"]))
if term := strings.TrimSpace(params.Search); term != "" {
pattern := "%" + term + "%"
query = query.Where(
@@ -2458,15 +2459,27 @@ func (s *ProjectService) HandleProjectFilesChanged(ctx context.Context, paths []
return
}
- var projectsList []Project
- if err := s.db.WithContext(ctx).Find(&projectsList).Error; err != nil {
+ // Match on paths alone, then load full rows only for the affected projects.
+ var candidates []Project
+ if err := s.db.WithContext(ctx).Select("id", "path").Find(&candidates).Error; err != nil {
slog.WarnContext(ctx, "failed to resolve changed project files", "error", fmt.Errorf("list projects for changed paths: %w", err))
return
}
- for i := range projectsList {
- if !slices.ContainsFunc(paths, func(changedPath string) bool { return projects.IsSafeSubdirectory(projectsList[i].Path, changedPath) }) {
- continue
+ var changedIDs []string
+ for _, candidate := range candidates {
+ if slices.ContainsFunc(paths, func(changedPath string) bool { return projects.IsSafeSubdirectory(candidate.Path, changedPath) }) {
+ changedIDs = append(changedIDs, candidate.ID)
}
+ }
+ if len(changedIDs) == 0 {
+ return
+ }
+ var projectsList []Project
+ if err := s.db.WithContext(ctx).Where("id IN ?", changedIDs).Find(&projectsList).Error; err != nil {
+ slog.WarnContext(ctx, "failed to resolve changed project files", "error", fmt.Errorf("load projects for changed paths: %w", err))
+ return
+ }
+ for i := range projectsList {
s.refreshProjectImageRefs(ctx, &projectsList[i])
if err := s.reconcileComposeTagsForProject(ctx, &projectsList[i]); err != nil {
slog.WarnContext(ctx, "failed to reconcile Compose project tags after file change", "projectId", projectsList[i].ID, "error", err)
@@ -2530,7 +2543,7 @@ func (s *ProjectService) SyncProjectsFromFileSystem(ctx context.Context) error {
// Decide deletions before performing any, so the mass-wipe guard can veto a
// suspicious pass (e.g. an unmounted projects volume) as a whole.
var all []Project
- if findErr := s.db.WithContext(ctx).Find(&all).Error; findErr != nil {
+ if findErr := s.db.WithContext(ctx).Select("id", "name", "dir_name", "path", "gitops_managed_by").Find(&all).Error; findErr != nil {
slog.WarnContext(ctx, "error during DB cleanup of projects", "error", fmt.Errorf("list projects for cleanup failed: %w", findErr))
return nil
}
@@ -3297,8 +3310,9 @@ func (s *ProjectService) StreamProjectLogs(ctx context.Context, projectID string
}
func (s *ProjectService) GetProjectStatusCounts(ctx context.Context) (projecttypes.StatusCounts, error) {
+ // Runtime status comes from containers, so only the matching and fallback columns are read.
var projectsList []Project
- if listProjectsErr := s.db.WithContext(ctx).Find(&projectsList).Error; listProjectsErr != nil {
+ if listProjectsErr := s.db.WithContext(ctx).Select("id", "name", "path", "status", "compose_project_name", "is_archived").Find(&projectsList).Error; listProjectsErr != nil {
return projecttypes.StatusCounts{}, fmt.Errorf("failed to list projects: %w", listProjectsErr)
}
return s.listing.StatusCounts(ctx, projectRecords(projectsList)), nil
diff --git a/backend/internal/project/service_test.go b/backend/internal/project/service_test.go
index c7a920ec7..385c3fee8 100644
--- a/backend/internal/project/service_test.go
+++ b/backend/internal/project/service_test.go
@@ -4744,6 +4744,54 @@ func TestProjectService_ListProjects_FiltersArchivedProjects(t *testing.T) {
assert.Equal(t, []string{"active-demo", "archived-demo"}, []string{items[0].Name, items[1].Name})
}
+func TestProjectService_ListProjects_FiltersByExactName(t *testing.T) {
+ db := setupProjectTestDB(t)
+ ctx := t.Context()
+
+ settingsService, err := newSettingsServiceForTest(t, ctx, db)
+ require.NoError(t, err)
+ projectsRoot := t.TempDir()
+ require.NoError(t, settingsService.SetStringSetting(ctx, "projectsDirectory", projectsRoot))
+
+ for _, name := range []string{"demo", "demo-extra"} {
+ require.NoError(t, db.Create(&Project{
+ ID: "project-" + name,
+ Name: name,
+ DirName: new(name),
+ Path: createComposeProjectDir(t, projectsRoot, name),
+ Status: ProjectStatusStopped,
+ }).Error)
+ }
+
+ svc := NewProjectService(
+ db,
+ settingsService,
+ event.NewEventService(db, nil, nil),
+ image.NewImageService(db, nil, nil, nil, nil, nil, nil, nil),
+ testDockerService,
+ testBuildBuilder{},
+ nil,
+ nil,
+ config.Load(),
+ kv.NewKVService(db),
+ testRegistryCredentials,
+ )
+
+ // Sorting by status pages in memory, so both pagination paths are covered.
+ for _, sort := range []string{"name", "status"} {
+ items, page, listErr := svc.ListProjects(ctx, pagination.QueryParams{
+ Filters: map[string]string{"name": "demo"},
+ Limit: 1,
+ Sort: sort,
+ Order: pagination.SortAsc,
+ })
+ require.NoError(t, listErr, sort)
+ require.EqualValues(t, 1, page.TotalItems, sort)
+ require.Len(t, items, 1, sort)
+ assert.Equal(t, "project-demo", items[0].ID, sort)
+ }
+}
+
func TestProjectService_ArchiveProject_RequiresStoppedProject(t *testing.T) {
db := setupProjectTestDB(t)
ctx := t.Context()
diff --git a/backend/internal/role/service.go b/backend/internal/role/service.go
index 652d9f2fa..252738931 100644
--- a/backend/internal/role/service.go
+++ b/backend/internal/role/service.go
@@ -912,12 +912,16 @@ func (s *RoleService) ReconcileEnvOidcMappings(ctx context.Context, rawSpec stri
return dbutil.WithTx(ctx, s.db.DB, func(tx *gorm.DB) error {
// Verify every referenced role exists. Done inside the tx so a concurrent
// role delete can't race past this check.
+ roleIDs := make([]string, 0, len(specs))
+ for _, sp := range specs {
+ roleIDs = append(roleIDs, sp.RoleID)
+ }
+ var existingIDs []string
+ if err := tx.Model(&Role{}).Where("id IN ?", roleIDs).Pluck("id", &existingIDs).Error; err != nil {
+ return fmt.Errorf("OIDC_ROLE_MAPPINGS: failed to verify roles: %w", err)
+ }
for i, sp := range specs {
- var count int64
- if err := tx.Model(&Role{}).Where("id = ?", sp.RoleID).Count(&count).Error; err != nil {
- return fmt.Errorf("OIDC_ROLE_MAPPINGS[%d]: failed to verify role: %w", i, err)
- }
- if count == 0 {
+ if !slices.Contains(existingIDs, sp.RoleID) {
return fmt.Errorf("OIDC_ROLE_MAPPINGS[%d]: role %q does not exist", i, sp.RoleID)
}
}
diff --git a/backend/internal/settings/model.go b/backend/internal/settings/model.go
index 84f0529e7..37319c023 100644
--- a/backend/internal/settings/model.go
+++ b/backend/internal/settings/model.go
@@ -119,6 +119,7 @@ type Settings struct {
GitSyncMaxBinarySizeMb SettingVariable `key:"gitSyncMaxBinarySizeMb,envOverride" meta:"label=Git Sync Max Binary Size (MB);type=number;keywords=git,sync,binary,size,limit,repository,compose,gitops,mb;category=general;description=Maximum size in MB for a single binary file copied during a Git sync. Set 0 to disable the environment cap (default: 10)"`
DockerHost SettingVariable `key:"dockerHost,authrequired,envOverride" meta:"label=Docker Host;type=text;keywords=docker,host,daemon,socket,unix,remote;category=internal;description=URI for Docker daemon"`
BuildProvider SettingVariable `key:"buildProvider,envOverride" meta:"label=Build Provider;type=select;keywords=build,buildkit,depot,provider,remote,local;category=build;description=Default build provider (local or depot)" catmeta:"id=build;title=Build;icon=code;url=/settings/builds;description=Configure BuildKit and Depot build settings"`
+ BuildHistoryRetentionDays SettingVariable `key:"buildHistoryRetentionDays,envOverride" meta:"label=Build History Retention;type=number;keywords=build,history,retention,days,cleanup,image;category=build;description=Delete completed and failed image build records older than this many days. Set 0 to disable age-based cleanup."`
BuildsDirectory SettingVariable `key:"buildsDirectory,envOverride" meta:"label=Builds Directory;type=text;keywords=builds,directory,path,workspace,context;category=build;description=Root directory for manual build workspaces"`
BuildTimeout SettingVariable `key:"buildTimeout,envOverride" meta:"label=Build Timeout;type=number;keywords=build,timeout,seconds,buildkit;category=build;description=Timeout for BuildKit builds in seconds (default: 1800 = 30 minutes)"`
DepotProjectId SettingVariable `key:"depotProjectId,envOverride" meta:"label=Depot Project ID;type=text;keywords=depot,project,id,build,provider;category=build;description=Depot project identifier"`
diff --git a/backend/internal/settings/service.go b/backend/internal/settings/service.go
index d450f4a2f..c664e05c9 100644
--- a/backend/internal/settings/service.go
+++ b/backend/internal/settings/service.go
@@ -351,24 +351,25 @@ func DefaultSettingsConfig() *Settings {
LifecycleDefaultRunnerImage: SettingVariable{Value: "alpine:latest"},
LifecycleMaxTimeoutSec: SettingVariable{Value: "300"},
- DockerAPITimeout: SettingVariable{Value: "30"},
- DockerImagePullTimeout: SettingVariable{Value: "600"},
- TrivyScanTimeout: SettingVariable{Value: "900"},
- ImagePatchSuffix: SettingVariable{Value: "patched"},
- ImagePatchTimeoutSec: SettingVariable{Value: "600"},
- ImagePatchAllPlatforms: SettingVariable{Value: "false"},
- ImageAutoPatchEnabled: SettingVariable{Value: "false"},
- ImageAutoPatchInterval: SettingVariable{Value: "0 0 3 * * *"},
- GitOperationTimeout: SettingVariable{Value: "300"},
- HTTPClientTimeout: SettingVariable{Value: "30"},
- RegistryTimeout: SettingVariable{Value: "30"},
- RegistryTagTimeout: SettingVariable{Value: "120"},
- ProxyRequestTimeout: SettingVariable{Value: "60"},
- DeployWaitTimeout: SettingVariable{Value: "600"},
- BuildProvider: SettingVariable{Value: "local"},
- BuildTimeout: SettingVariable{Value: "1800"},
- DepotProjectId: SettingVariable{Value: ""},
- DepotToken: SettingVariable{Value: ""},
+ DockerAPITimeout: SettingVariable{Value: "30"},
+ DockerImagePullTimeout: SettingVariable{Value: "600"},
+ TrivyScanTimeout: SettingVariable{Value: "900"},
+ ImagePatchSuffix: SettingVariable{Value: "patched"},
+ ImagePatchTimeoutSec: SettingVariable{Value: "600"},
+ ImagePatchAllPlatforms: SettingVariable{Value: "false"},
+ ImageAutoPatchEnabled: SettingVariable{Value: "false"},
+ ImageAutoPatchInterval: SettingVariable{Value: "0 0 3 * * *"},
+ GitOperationTimeout: SettingVariable{Value: "300"},
+ HTTPClientTimeout: SettingVariable{Value: "30"},
+ RegistryTimeout: SettingVariable{Value: "30"},
+ RegistryTagTimeout: SettingVariable{Value: "120"},
+ ProxyRequestTimeout: SettingVariable{Value: "60"},
+ DeployWaitTimeout: SettingVariable{Value: "600"},
+ BuildProvider: SettingVariable{Value: "local"},
+ BuildTimeout: SettingVariable{Value: "1800"},
+ BuildHistoryRetentionDays: SettingVariable{Value: "30"},
+ DepotProjectId: SettingVariable{Value: ""},
+ DepotToken: SettingVariable{Value: ""},
ApnsEnabled: SettingVariable{Value: "false"},
ApnsChannelID: SettingVariable{Value: ""},
diff --git a/backend/internal/template/service.go b/backend/internal/template/service.go
index 169fac4a9..2206b7f05 100644
--- a/backend/internal/template/service.go
+++ b/backend/internal/template/service.go
@@ -980,6 +980,12 @@ func (s *TemplateService) SyncLocalTemplatesFromFilesystem(ctx context.Context)
return fmt.Errorf("read dir %s: %w", dir, err)
}
+ // One query for every local template; each folder below matches in memory.
+ var localTemplates []ComposeTemplate
+ if findErr := s.db.WithContext(ctx).Where("is_remote = ? AND registry_id IS NULL", false).Order("id").Find(&localTemplates).Error; findErr != nil {
+ return fmt.Errorf("query existing templates: %w", findErr)
+ }
+
for _, ent := range entries {
// Only process directories; root-level compose files are ignored to prevent duplication.
if !ent.IsDirectory {
@@ -994,16 +1000,17 @@ func (s *TemplateService) SyncLocalTemplatesFromFilesystem(ctx context.Context)
folderErr = s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
// Match on description (the absolute compose path) or, for disk imports, the folder name so
// directory reconfigurations and compose renames keep a single row.
- var existing ComposeTemplate
- findErr := tx.Where("is_remote = ? AND registry_id IS NULL AND (description = ? OR (name = ? AND description LIKE ?))", false, desc, ent.Name, "Imported from %").
- First(&existing).Error
- if findErr != nil && !errors.Is(findErr, gorm.ErrRecordNotFound) {
- return fmt.Errorf("query existing template: %w", findErr)
- }
-
- tpl := &existing
- if findErr != nil {
- tpl = &ComposeTemplate{ID: uuid.New().String()}
+ existingIndex := slices.IndexFunc(localTemplates, func(t ComposeTemplate) bool {
+ return t.Description == desc || (t.Name == ent.Name && strings.HasPrefix(t.Description, "Imported from "))
+ })
+ exists := existingIndex >= 0
+ tpl := &ComposeTemplate{ID: uuid.New().String()}
+ if exists {
+ // Reload inside the transaction so a concurrent download's metadata isn't overwritten by the preloaded copy.
+ tpl = &ComposeTemplate{}
+ if loadErr := tx.Where("id = ?", localTemplates[existingIndex].ID).First(tpl).Error; loadErr != nil {
+ return fmt.Errorf("reload template %s: %w", localTemplates[existingIndex].ID, loadErr)
+ }
}
tpl.Name = ent.Name
tpl.Description = desc
@@ -1013,7 +1020,7 @@ func (s *TemplateService) SyncLocalTemplatesFromFilesystem(ctx context.Context)
tpl.IsRemote = false
setTemplateIconURL(tpl, iconURL)
- if findErr == nil {
+ if exists {
if saveErr := tx.Save(tpl).Error; saveErr != nil {
return fmt.Errorf("update template %s: %w", tpl.ID, saveErr)
}
diff --git a/backend/internal/updater/service.go b/backend/internal/updater/service.go
index 5a9b3532a..bfb6dd743 100644
--- a/backend/internal/updater/service.go
+++ b/backend/internal/updater/service.go
@@ -720,6 +720,19 @@ func (s *UpdaterService) PendingImageUpdates(ctx context.Context) ([]updater.Ima
if err != nil {
return nil, err
}
+ // Resolve each container's policy and normalized ref once, not per record.
+ type eligibleContainer struct {
+ id string
+ ref string
+ }
+ eligible := make([]eligibleContainer, 0, len(running))
+ for _, cnt := range running {
+ resolved, policyErr := tagpolicy.Resolve(cnt.Image, updater.DefaultLabelPolicy().TagPolicy(cnt.Labels))
+ if policyErr != nil || resolved.Strategy == "tag" {
+ continue
+ }
+ eligible = append(eligible, eligibleContainer{id: cnt.ID, ref: refs.NormalizeImageUpdateRef(cnt.Image)})
+ }
converted := func(record imageupdate.ImageUpdateRecord, containerID string) updater.ImageUpdateRecord {
return updater.ImageUpdateRecord{
ContainerID: containerID,
@@ -742,12 +755,11 @@ func (s *UpdaterService) PendingImageUpdates(ctx context.Context) ([]updater.Ima
out = append(out, converted(record, record.ContainerID))
continue
}
- for _, cnt := range running {
- resolved, policyErr := tagpolicy.Resolve(cnt.Image, updater.DefaultLabelPolicy().TagPolicy(cnt.Labels))
- if policyErr != nil || resolved.Strategy == "tag" || refs.NormalizeImageUpdateRef(cnt.Image) != refs.NormalizeImageUpdateRef(converted(record, "").ImageRef()) {
- continue
+ recordRef := refs.NormalizeImageUpdateRef(converted(record, "").ImageRef())
+ for _, cnt := range eligible {
+ if cnt.ref == recordRef {
+ out = append(out, converted(record, cnt.id))
}
- out = append(out, converted(record, cnt.ID))
}
}
return out, nil
@@ -1172,6 +1184,8 @@ func (s *UpdaterService) collectUsedImagesFromContainers(ctx context.Context, ou
return err
}
+ // Containers sharing an image add the same tags, so each image is inspected once.
+ inspectedImages := map[string]struct{}{}
for _, summary := range listResult.Items {
if labels.IsUpdateDisabled(summary.Labels) {
slog.DebugContext(ctx, "collectUsedImagesFromContainers: container opted out by labels", "containerId", summary.ID)
@@ -1199,10 +1213,13 @@ func (s *UpdaterService) collectUsedImagesFromContainers(ctx context.Context, ou
slog.DebugContext(ctx, "collectUsedImagesFromContainers: container inspect labels opted out", "containerId", summary.ID)
continue
}
- if imageInspect, imageErr := dcli.ImageInspect(ctx, inspectResult.Container.Image); imageErr == nil {
- for _, tag := range imageInspect.RepoTags {
- if strings.TrimSpace(tag) != "" && tag != "<none>:<none>" {
- addNormalizedImageUpdateRef(ctx, out, tag, "normalizedTagsForContainer repo tag", "imageId", inspectResult.Container.Image)
+ if _, inspected := inspectedImages[inspectResult.Container.Image]; !inspected {
+ inspectedImages[inspectResult.Container.Image] = struct{}{}
+ if imageInspect, imageErr := dcli.ImageInspect(ctx, inspectResult.Container.Image); imageErr == nil {
+ for _, tag := range imageInspect.RepoTags {
+ if strings.TrimSpace(tag) != "" && tag != "<none>:<none>" {
+ addNormalizedImageUpdateRef(ctx, out, tag, "normalizedTagsForContainer repo tag", "imageId", inspectResult.Container.Image)
+ }
}
}
}
diff --git a/backend/pkg/projects/cmds.go b/backend/pkg/projects/cmds.go
index f7d8b9ab1..e2645f21f 100644
--- a/backend/pkg/projects/cmds.go
+++ b/backend/pkg/projects/cmds.go
@@ -178,27 +178,21 @@ func composeUpOptionsInternal(proj *composetypes.Project,
// ComposePs lists a project's compose containers. dockerHost is the
// config-resolved docker host used to reuse the shared read-only compose
-// client; empty falls back to a one-shot environment-resolved client.
+// client; empty uses the environment-resolved host.
func ComposePs(ctx context.Context, dockerHost string, proj *composetypes.Project, services []string, all bool) ([]api.ContainerSummary, error) {
- c, shared, err := plainComposeClientInternal(ctx, dockerHost)
+ c, err := plainComposeClientInternal(dockerHost)
if err != nil {
return nil, err
}
- if !shared {
- defer func() { _ = c.Close() }()
- }
return c.svc.Ps(ctx, proj.Name, api.PsOptions{All: all, Services: services})
}
func ComposeGenerate(ctx context.Context, dockerHost, projectName string, containerIDs []string) (string, error) {
- c, shared, err := plainComposeClientInternal(ctx, dockerHost)
+ c, err := plainComposeClientInternal(dockerHost)
if err != nil {
return "", err
}
- if !shared {
- defer func() { _ = c.Close() }()
- }
model, err := c.svc.Generate(ctx, api.GenerateOptions{ProjectName: projectName, Containers: containerIDs})
if err != nil {
@@ -243,13 +237,10 @@ func ComposeLogs(ctx context.Context, projectName string, out io.Writer, follow
func ListGlobalComposeContainers(ctx context.Context, dockerClient client.APIClient, dockerHost string) ([]container.Summary, error) {
cli := dockerClient
if cli == nil {
- c, shared, err := plainComposeClientInternal(ctx, dockerHost)
+ c, err := plainComposeClientInternal(dockerHost)
if err != nil {
return nil, err
}
- if !shared {
- defer func() { _ = c.Close() }()
- }
cli = c.dockerCli.Client()
}
diff --git a/backend/pkg/projects/compose.go b/backend/pkg/projects/compose.go
index 44e6f5086..9425c431c 100644
--- a/backend/pkg/projects/compose.go
+++ b/backend/pkg/projects/compose.go
@@ -1,10 +1,12 @@
package projects
import (
+ "cmp"
"context"
"io"
"log/slog"
"maps"
+ "os"
"strings"
"sync"
"sync/atomic"
@@ -126,32 +128,30 @@ func NewClient(ctx context.Context, dockerHost string, authConfigs map[string]re
}
type plainComposeClientEntryInternal struct {
- dockerHost string
- client *Client
+ host string
+ client *Client
}
var plainComposeClient atomic.Pointer[plainComposeClientEntryInternal]
-func plainComposeClientInternal(ctx context.Context, dockerHost string) (*Client, bool, error) {
- if dockerHost == "" {
- c, err := NewClient(ctx, "", nil, nil)
- return c, false, err
- }
-
- if entry := plainComposeClient.Load(); entry != nil && entry.dockerHost == dockerHost {
- return entry.client, true, nil
+// plainComposeClientInternal returns the shared read-only compose client for
+// dockerHost; empty resolves the host from the process environment.
+func plainComposeClientInternal(dockerHost string) (*Client, error) {
+ host := cmp.Or(dockerHost, os.Getenv("DOCKER_HOST"))
+ if entry := plainComposeClient.Load(); entry != nil && entry.host == host {
+ return entry.client, nil
}
// Built on context.Background so request-scoped values (progress
// writers, deadlines) do not leak into the long-lived client.
c, err := NewClient(context.Background(), dockerHost, nil, nil) //nolint:contextcheck,forbidigo // Cached Compose client must not retain request values or cancellation.
if err != nil {
- return nil, false, err
+ return nil, err
}
// Concurrent first calls may race and briefly duplicate a client; the
// last store wins and later calls converge on it.
- plainComposeClient.Store(&plainComposeClientEntryInternal{dockerHost: dockerHost, client: c})
- return c, true, nil
+ plainComposeClient.Store(&plainComposeClientEntryInternal{host: host, client: c})
+ return c, nil
}
func buildComposeAuthConfigsInternal(authConfigs map[string]registry.AuthConfig) map[string]clitypes.AuthConfig {
diff --git a/backend/pkg/projects/compose_test.go b/backend/pkg/projects/compose_test.go
index 50ab5e06f..dcfa07aa8 100644
--- a/backend/pkg/projects/compose_test.go
+++ b/backend/pkg/projects/compose_test.go
@@ -32,27 +32,23 @@ func TestWrapDockerCLIWithInspectCompatibility(t *testing.T) {
}
func TestPlainComposeClientCachedPerDockerHost(t *testing.T) {
- ctx := t.Context()
-
- first, shared, err := plainComposeClientInternal(ctx, "tcp://first.example.com:2375")
+ first, err := plainComposeClientInternal("tcp://first.example.com:2375")
require.NoError(t, err)
- require.True(t, shared)
- again, shared, err := plainComposeClientInternal(ctx, "tcp://first.example.com:2375")
+ again, err := plainComposeClientInternal("tcp://first.example.com:2375")
require.NoError(t, err)
- require.True(t, shared)
require.Same(t, first, again)
- other, shared, err := plainComposeClientInternal(ctx, "tcp://second.example.com:2375")
+ other, err := plainComposeClientInternal("tcp://second.example.com:2375")
require.NoError(t, err)
- require.True(t, shared)
require.NotSame(t, first, other)
- // No configured host: a one-shot client the caller owns and closes.
- oneShot, shared, err := plainComposeClientInternal(ctx, "")
+ // No configured host: the environment-resolved client is shared too.
+ defaultHost, err := plainComposeClientInternal("")
+ require.NoError(t, err)
+ defaultAgain, err := plainComposeClientInternal("")
require.NoError(t, err)
- require.False(t, shared)
- require.NoError(t, oneShot.Close())
+ require.Same(t, defaultHost, defaultAgain)
}
func TestBuildComposeAuthConfigs(t *testing.T) {
diff --git a/backend/pkg/scheduler/event_cleanup_job.go b/backend/pkg/scheduler/event_cleanup_job.go
index c46dbc03d..d95d2279c 100644
--- a/backend/pkg/scheduler/event_cleanup_job.go
+++ b/backend/pkg/scheduler/event_cleanup_job.go
@@ -8,6 +8,7 @@ import (
schedulertypes "github.com/getarcaneapp/arcane/types/v2/scheduler"
"github.com/getarcaneapp/arcane/backend/v2/internal/activity"
+ "github.com/getarcaneapp/arcane/backend/v2/internal/build"
"github.com/getarcaneapp/arcane/backend/v2/internal/event"
"github.com/getarcaneapp/arcane/backend/v2/internal/settings"
"github.com/getarcaneapp/arcane/backend/v2/pkg/scheduler/schedule"
@@ -18,13 +19,15 @@ const EventCleanupJobName = "event-cleanup"
type EventCleanupJob struct {
eventService *event.EventService
activityService *activity.ActivityService
+ buildService *build.BuildService
settingsService *settings.SettingsService
}
-func NewEventCleanupJob(eventService *event.EventService, activityService *activity.ActivityService, settingsService *settings.SettingsService) *EventCleanupJob {
+func NewEventCleanupJob(eventService *event.EventService, activityService *activity.ActivityService, buildService *build.BuildService, settingsService *settings.SettingsService) *EventCleanupJob {
return &EventCleanupJob{
eventService: eventService,
activityService: activityService,
+ buildService: buildService,
settingsService: settingsService,
}
}
@@ -66,6 +69,19 @@ func (j *EventCleanupJob) Run(ctx context.Context) (schedulertypes.Outcome, erro
"maxEntries", maxEntries,
"deleted", deleted)
}
+
+ buildRetentionDays := j.settingsService.GetIntSetting(ctx, "buildHistoryRetentionDays", 30)
+ deletedBuilds, err := j.buildService.PruneHistory(ctx, buildRetentionDays)
+ if err != nil {
+ slog.ErrorContext(ctx, "Failed to prune image build history", "jobName", EventCleanupJobName, "error", err)
+ return schedulertypes.Outcome{}, err
+ }
+
+ slog.InfoContext(ctx, "Image build history cleanup completed successfully",
+ "jobName", EventCleanupJobName,
+ "retentionDays", buildRetentionDays,
+ "deleted", deletedBuilds)
+
return schedulertypes.Outcome{Status: schedulertypes.Succeeded}, nil
}
diff --git a/backend/pkg/scheduler/image_update_watcher.go b/backend/pkg/scheduler/image_update_watcher.go
index a9062be2d..0ea1025f8 100644
--- a/backend/pkg/scheduler/image_update_watcher.go
+++ b/backend/pkg/scheduler/image_update_watcher.go
@@ -354,9 +354,8 @@ func (w *ImageUpdateWatcher) RunNow(ctx context.Context) (err error) {
if err != nil {
return err
}
- ticker := time.NewTicker(50 * time.Millisecond)
- defer ticker.Stop()
- for {
+ // Back off from 50 ms to 1 s so quick runs return fast without polling long scans hard.
+ for delay := 50 * time.Millisecond; ; delay = min(delay*2, time.Second) {
current, getErr := coordinator.Get(ctx, run.EnvironmentID, run.JobID, run.ID)
if getErr != nil {
return getErr
@@ -370,7 +369,7 @@ func (w *ImageUpdateWatcher) RunNow(ctx context.Context) (err error) {
select {
case <-ctx.Done():
return ctx.Err()
- case <-ticker.C:
+ case <-time.After(delay):
case <-w.stopped:
return errors.New("image update watcher stopped")
}
diff --git a/frontend/messages/en.json b/frontend/messages/en.json
index bf55435b2..683cece71 100644
--- a/frontend/messages/en.json
+++ b/frontend/messages/en.json
@@ -1808,6 +1808,7 @@
"security_scan_all_failed": "Failed to start scan for {count} images",
"security_scan_all_partial": "Started {succeeded} scans; {failed} failed to start",
"security_scan_all_error": "Error starting scan all operation",
+ "security_scan_all_capped": "Scanning the first {count} of {total} images",
"vuln_ignore": "Ignore vulnerability",
"vuln_ignore_success": "Ignored {cve}",
"vuln_ignore_failed": "Failed to ignore vulnerability",
@@ -2389,6 +2390,10 @@
"activity_history_retention_days_description": "Delete completed Activity Center entries older than this many days.",
"activity_history_retention_days_placeholder": "30",
"activity_history_retention_days_help": "Set to 0 to disable age-based cleanup.",
+ "build_history_retention_days": "Build History Retention Days",
+ "build_history_retention_days_description": "Delete completed and failed image build records older than this many days.",
+ "build_history_retention_days_placeholder": "30",
+ "build_history_retention_days_help": "Set to 0 to disable age-based cleanup.",
"activity_history_max_entries": "Maximum Entries",
"activity_history_max_entries_description": "Keep this many completed Activity Center entries per environment.",
"activity_history_max_entries_placeholder": "1000",
@@ -2868,6 +2873,8 @@
"update_center_title": "Arcane update available",
"update_center_whats_new": "What's new",
"update_center_release_notes_unavailable": "Release notes are not available for this version.",
+ "release_notes_load_failed": "Release notes could not be loaded.",
+ "code_editor_load_failed": "The code editor could not be loaded.",
"update_center_view_full_release": "View full release on GitHub",
"update_center_summary": "Arcane will pull the new image, restart, and reload this page. Your data and configuration are preserved.",
"update_center_install": "Install update",
diff --git a/frontend/src/lib/components/activity/activity-center.svelte b/frontend/src/lib/components/activity/activity-center.svelte
index d32978359..660e99b82 100644
--- a/frontend/src/lib/components/activity/activity-center.svelte
+++ b/frontend/src/lib/components/activity/activity-center.svelte
@@ -110,7 +110,7 @@
disabled={activityStore.isCancelling(activity.id)}
title={m.activity_cancel()}
aria-label={m.activity_cancel()}
- class="absolute top-1/2 right-11 z-(--arcane-z-raised) flex size-7 -translate-y-1/2 items-center justify-center rounded-md bg-background/70 text-muted-foreground opacity-0 backdrop-blur-sm transition group-hover/activity:opacity-100 hover:bg-destructive/10 hover:text-destructive focus-visible:opacity-100 focus-visible:ring-2 focus-visible:ring-ring focus-visible:outline-hidden disabled:pointer-events-none disabled:opacity-40"
+ class="absolute top-1/2 right-11 z-(--arcane-z-raised) flex size-7 -translate-y-1/2 items-center justify-center rounded-md bg-background/90 text-muted-foreground opacity-0 transition group-hover/activity:opacity-100 hover:bg-destructive/10 hover:text-destructive focus-visible:opacity-100 focus-visible:ring-2 focus-visible:ring-ring focus-visible:outline-hidden disabled:pointer-events-none disabled:opacity-40"
>
<CloseIcon class="size-4" aria-hidden="true" />
</button>
diff --git a/frontend/src/lib/components/code-panel.svelte b/frontend/src/lib/components/code-panel.svelte
index 9a661460c..ac0a9f79e 100644
--- a/frontend/src/lib/components/code-panel.svelte
+++ b/frontend/src/lib/components/code-panel.svelte
@@ -6,7 +6,6 @@
DiagnosticSummary,
EditorContext
} from '#lib/components/code-editor/analysis/types.js';
- import CodeEditor from '#lib/components/code-editor/editor.svelte';
import * as Card from '#lib/components/ui/card/index.js';
import { IsMobile } from '#lib/hooks/is-mobile.svelte.js';
import { CodeIcon, FileTextIcon, SearchIcon, ArrowsUpDownIcon } from '#lib/icons/index.js';
@@ -101,24 +100,31 @@
onkeydown={handleEditorKeydown}
>
<div class={effectiveAutoHeight ? '' : 'absolute inset-0'}>
- <CodeEditor
- bind:value
- {language}
- {validationMode}
- fontSize="13px"
- autoHeight={effectiveAutoHeight}
- {readOnly}
- bind:hasErrors
- bind:validationReady
- bind:diagnosticSummary
- {fileId}
- {originalValue}
- {enableDiff}
- {editorContext}
- bind:outlineOpen
- bind:diffOpen
- bind:commandPaletteOpen
- />
+ <!-- CodeMirror loads with the first panel that renders, not with the route. -->
+ {#await import('#lib/components/code-editor/editor.svelte')}
+ <p class="p-4 text-sm text-muted-foreground">{m.common_loading()}</p>
+ {:then { default: CodeEditor }}
+ <CodeEditor
+ bind:value
+ {language}
+ {validationMode}
+ fontSize="13px"
+ autoHeight={effectiveAutoHeight}
+ {readOnly}
+ bind:hasErrors
+ bind:validationReady
+ bind:diagnosticSummary
+ {fileId}
+ {originalValue}
+ {enableDiff}
+ {editorContext}
+ bind:outlineOpen
+ bind:diffOpen
+ bind:commandPaletteOpen
+ />
+ {:catch}
+ <p class="p-4 text-sm text-destructive">{m.code_editor_load_failed()}</p>
+ {/await}
</div>
</div>
{#if error}
diff --git a/frontend/src/lib/components/dialogs/update-all-dialog.svelte b/frontend/src/lib/components/dialogs/update-all-dialog.svelte
index 44a3c53c1..c01561bf1 100644
--- a/frontend/src/lib/components/dialogs/update-all-dialog.svelte
+++ b/frontend/src/lib/components/dialogs/update-all-dialog.svelte
@@ -21,9 +21,10 @@
} from '#lib/types/system-upgrade.js';
import { cn } from '#lib/utils.js';
import { handleApiResultWithCallbacks } from '#lib/utils/api.js';
- import { formatElapsedTime, formatRelativeTime, nowInstantString } from '#lib/utils/formatting.js';
+ import { formatRelativeTime, nowInstantString } from '#lib/utils/formatting.js';
import { tryCatch } from '#lib/utils/try-catch.js';
+ import UpdateAllElapsed from './update-all-elapsed.svelte';
import VersionUpdateSummary from './version-update-summary.svelte';
// open has no $bindable fallback: upstream binds can start out undefined, and
@@ -55,8 +56,6 @@
let phase = $state<Phase>('confirm');
let job = $state<UpdateAllJob | null>(null);
let reconnecting = $state(false);
- // Ticks once a second while the dialog is open so elapsed stage times advance.
- let clock = $state<string>();
let pollActive = false;
let pollTimer: ReturnType<typeof setTimeout> | null = null;
// Bumped on every confirm and reset so a startup/status response that lands after
@@ -157,11 +156,7 @@
// opening and its cleanup once per close.
const observeUpdate: Attachment = () => {
if (!debugDemo) void restoreActiveJob();
- const ticker = setInterval(() => (clock = nowInstantString()), 1000);
- return () => {
- clearInterval(ticker);
- resetState();
- };
+ return () => resetState();
};
async function handleConfirm() {
@@ -518,11 +513,7 @@
<Spinner class="size-4" />
<div>
<span>{m.environments_update_all_manager_restarting()}</span>
- <span class="block text-xs">
- {m.environments_update_all_elapsed({
- duration: formatElapsedTime(managerResult?.stageStartedAt ?? job?.createdAt, { base: clock })
- })}
- </span>
+ <UpdateAllElapsed class="block text-xs" since={managerResult?.stageStartedAt ?? job?.createdAt} />
</div>
</div>
{/if}
@@ -573,11 +564,7 @@
<div class={cn('max-w-48 shrink-0 text-right text-xs', statusPresentation(result.status).text)}>
<span>{rowLabel(result)}</span>
{#if result.status === 'updating' && result.stageStartedAt}
- <span class="block text-muted-foreground">
- {m.environments_update_all_elapsed({
- duration: formatElapsedTime(result.stageStartedAt, { base: clock })
- })}
- </span>
+ <UpdateAllElapsed class="block text-muted-foreground" since={result.stageStartedAt} />
{/if}
</div>
</li>
diff --git a/frontend/src/lib/components/dialogs/update-all-elapsed.svelte b/frontend/src/lib/components/dialogs/update-all-elapsed.svelte
new file mode 100644
index 000000000..074ea60d6
--- /dev/null
+++ b/frontend/src/lib/components/dialogs/update-all-elapsed.svelte
@@ -0,0 +1,20 @@
+<script lang="ts">
+ import { m } from '#lib/paraglide/messages.js';
+ import { formatElapsedTime, nowInstantString, type InstantInput } from '#lib/utils/formatting.js';
+
+ let { since, class: className }: { since: InstantInput; class?: string } = $props();
+
+ // Owns the one-second tick so only this text updates while a stage is running.
+ let clock = $state(nowInstantString());
+
+ $effect(() => {
+ const ticker = setInterval(() => (clock = nowInstantString()), 1000);
+ return () => clearInterval(ticker);
+ });
+</script>
+
+<span class={className}>
+ {m.environments_update_all_elapsed({
+ duration: formatElapsedTime(since, { base: clock })
+ })}
+</span>
diff --git a/frontend/src/lib/components/gitops/project-git-backup-summary.svelte b/frontend/src/lib/components/gitops/project-git-backup-summary.svelte
index 18be3e107..8df558bdb 100644
--- a/frontend/src/lib/components/gitops/project-git-backup-summary.svelte
+++ b/frontend/src/lib/components/gitops/project-git-backup-summary.svelte
@@ -253,9 +253,7 @@
{/if}
<div class="grid gap-3 sm:grid-cols-2 xl:grid-cols-4">
- <div
- class="hover-lift flex items-center justify-between gap-3 rounded-xl border border-border/70 bg-card/60 p-4 backdrop-blur-md"
- >
+ <div class="hover-lift flex items-center justify-between gap-3 rounded-xl border border-border/70 bg-card/80 p-4">
<div class="space-y-2">
<p class="text-sm font-medium tracking-wide text-muted-foreground">{m.files_in_last_backup()}</p>
<h3 class="text-2xl font-semibold tracking-tight tabular-nums">{snapshotFiles}</h3>
@@ -289,7 +287,7 @@
</div>
{#if canBackup}
- <div class="rounded-xl border border-border/70 bg-card/60 backdrop-blur-md">
+ <div class="rounded-xl border border-border/70 bg-card/80">
<div class="flex items-center justify-between gap-2 border-b border-border/50 px-4 py-3">
<h3 class="flex items-center gap-2 text-sm font-medium">
<ClockIcon class="size-4" />
diff --git a/frontend/src/lib/components/job-card/job-run-history.svelte b/frontend/src/lib/components/job-card/job-run-history.svelte
index f6792ffa0..a52214957 100644
--- a/frontend/src/lib/components/job-card/job-run-history.svelte
+++ b/frontend/src/lib/components/job-card/job-run-history.svelte
@@ -13,7 +13,7 @@
import { extractApiErrorMessage } from '#lib/utils/api.js';
import { formatDateTimeShort } from '#lib/utils/formatting.js';
- import { jobStatusLabel, jobStatusTone, jobStepLabel } from './job-status';
+ import { isJobRunPending, jobStatusLabel, jobStatusTone, jobStepLabel } from './job-status';
// Renders the run list and selected-run detail; the host decides where it lives (panel or dialog).
let {
@@ -42,13 +42,14 @@
const runs = createQuery(() => ({
queryKey: queryKeys.jobs.runs(environmentId, jobId, page),
queryFn: () => jobScheduleService.listRuns(jobId, environmentId, page),
- refetchInterval: 5000
+ // Idle polling still picks up scheduled runs.
+ refetchInterval: (query) => (query.state.data?.runs.some((run) => isJobRunPending(run.status)) ? 5000 : 30_000)
}));
const detail = createQuery(() => ({
queryKey: queryKeys.jobs.run(environmentId, jobId, selected),
queryFn: () => jobScheduleService.getRun(jobId, selected!, environmentId),
enabled: !!selected,
- refetchInterval: 5000
+ refetchInterval: (query) => (isJobRunPending(query.state.data?.status) ? 5000 : false)
}));
const action = createMutation(() => ({
mutationFn: ({ runId, action }: { runId: string; action: 'retry' | 'cancel' }) =>
diff --git a/frontend/src/lib/components/job-card/job-status.ts b/frontend/src/lib/components/job-card/job-status.ts
index 1f37847f6..021b13274 100644
--- a/frontend/src/lib/components/job-card/job-status.ts
+++ b/frontend/src/lib/components/job-card/job-status.ts
@@ -1,4 +1,5 @@
import { m } from '#lib/paraglide/messages.js';
+import type { JobRunStatus } from '#lib/types/job.js';
import type { JobStatus } from '#lib/types/settings.js';
export function jobStatusLabel(status: string, isWorker = false): string {
const labels: Record<string, () => string> = {
@@ -80,3 +81,8 @@ export function jobStatusTone(status: string | undefined): JobStatusTone {
return 'gray';
}
}
+
+/** Whether a run can still change, so views showing it keep polling. */
+export function isJobRunPending(status: JobRunStatus | undefined): boolean {
+ return status === 'queued' || status === 'waiting' || status === 'running' || status === 'retrying';
+}
diff --git a/frontend/src/lib/components/logs/log-viewer.svelte b/frontend/src/lib/components/logs/log-viewer.svelte
index b6a6720f9..04233cdc8 100644
--- a/frontend/src/lib/components/logs/log-viewer.svelte
+++ b/frontend/src/lib/components/logs/log-viewer.svelte
@@ -2,9 +2,11 @@
// Dozzle reference: the grouped row shell and left-side timestamp treatment here were
// informed by amir20/dozzle's LogItem.vue and GroupedLogItem.vue.
import { dev } from '$app/env';
- import { onDestroy } from 'svelte';
+ import { onDestroy, untrack } from 'svelte';
import * as Collapsible from '#lib/components/ui/collapsible/index.js';
+ import { createVirtualizer } from '#lib/components/ui/virtualizer.svelte.js';
+ import VirtualRows from '#lib/components/virtual-rows.svelte';
import { ArrowDownIcon, ArrowRightIcon } from '#lib/icons/index.js';
import { m } from '#lib/paraglide/messages.js';
import { environmentStore } from '#lib/stores/environment.store.svelte.js';
@@ -13,12 +15,7 @@
import { tryCatch } from '#lib/utils/try-catch.js';
import { ReconnectingWebSocket } from '#lib/utils/ws.js';
- import {
- buildLogDisplayEntries,
- tryParseStructuredLog,
- type LogViewerDisplayEntry,
- type LogViewerEntry
- } from './log-viewer.utils';
+ import { buildLogDisplayEntries, getStructuredLog, type LogViewerDisplayEntry, type LogViewerEntry } from './log-viewer.utils';
import StructuredLogEntry from './structured-log-entry.svelte';
interface Props {
@@ -65,8 +62,10 @@
}: Props = $props();
let logs = $state.raw<LogViewerEntry[]>([]);
+ // Bumped when entries are pushed onto `logs` in place, which $state.raw does not track.
+ let logsVersion = $state(0);
let pending: LogViewerEntry[] = [];
- let flushScheduled = false;
+ let flushFrame: number | null = null;
let seq = 0;
let dropBefore = $state(0);
@@ -75,18 +74,21 @@
let lastCompactSeq = 0;
let visibleLogs = $derived.by(() => {
+ void logsVersion;
const kept = dropBefore === 0 ? logs : logs.filter((l) => l.id >= dropBefore);
const term = searchTerm.trim().toLowerCase();
if (!term) return kept;
return kept.filter((l) => l.message.toLowerCase().includes(term) || l.service?.toLowerCase().includes(term));
});
- let displayLogs = $derived.by(() =>
- buildLogDisplayEntries(visibleLogs, {
+ // Reads logsVersion directly: an in-place append leaves visibleLogs referentially unchanged.
+ let displayLogs = $derived.by(() => {
+ void logsVersion;
+ return buildLogDisplayEntries(visibleLogs, {
groupAdjacentLines,
type
- })
- );
+ });
+ });
let expandedGroups = $state<Set<number>>(new Set());
let hasAutoEnabledStructuredView = $state(false);
@@ -108,25 +110,26 @@
}
function scheduleFlush() {
- if (flushScheduled) return;
- flushScheduled = true;
- queueMicrotask(() => {
- flushScheduled = false;
+ if (flushFrame !== null) return;
+ flushFrame = requestAnimationFrame(() => {
+ flushFrame = null;
if (!pending.length) return;
- logs = [...logs, ...pending];
+ for (const entry of pending) logs.push(entry);
pending = [];
+ logsVersion++;
if (logs.length > maxLines * COMPACT_FACTOR) {
maybeCompact();
}
- if (autoScroll && logContainer) {
- requestAnimationFrame(() => {
- if (logContainer) logContainer.scrollTop = logContainer.scrollHeight;
- });
- }
});
}
+ function cancelFlush() {
+ if (flushFrame !== null) cancelAnimationFrame(flushFrame);
+ flushFrame = null;
+ }
+
function resetLogState() {
+ cancelFlush();
logs = [];
pending = [];
seq = 0;
@@ -335,29 +338,29 @@
}
function addLogEntry(logData: { level: string; message: string; timestamp?: string; service?: string; containerId?: string }) {
- const timestamp = logData.timestamp || nowInstantString();
- const { isJson, isStructured, parsed } = tryParseStructuredLog(logData.message);
+ const entry: LogViewerEntry = {
+ id: seq++,
+ timestamp: logData.timestamp || nowInstantString(),
+ level: logData.level as LogViewerEntry['level'],
+ message: logData.message,
+ service: logData.service,
+ containerId: logData.containerId ?? (type === 'container' ? (containerId ?? undefined) : undefined)
+ };
- if (isStructured && !hasAutoEnabledStructuredView && !showParsedJson) {
+ // Parse eagerly only while detecting whether to auto-enable the structured view.
+ if (!hasAutoEnabledStructuredView && !showParsedJson && getStructuredLog(entry).isStructured) {
hasAutoEnabledStructuredView = true;
showParsedJson = true;
}
- pending.push({
- id: seq++,
- timestamp,
- level: logData.level as LogViewerEntry['level'],
- message: logData.message,
- service: logData.service,
- containerId: logData.containerId ?? (type === 'container' ? (containerId ?? undefined) : undefined),
- isJson,
- isStructured,
- parsedJson: parsed
- });
+ pending.push(entry);
+ // Animation frames pause in hidden tabs, so bound the queue to what a flush would keep.
+ if (pending.length > maxLines * COMPACT_FACTOR) pending = pending.slice(-Math.max(1, maxLines));
scheduleFlush();
}
onDestroy(() => {
+ cancelFlush();
stopLogStream(false);
});
@@ -416,6 +419,27 @@
return !!value && typeof value === 'object' && !Array.isArray(value);
}
+ // Keep short logs on the plain render path.
+ const VIRTUALIZE_THRESHOLD = 100;
+ const shouldVirtualize = $derived(displayLogs.length > VIRTUALIZE_THRESHOLD);
+ const rowVirtualizer = createVirtualizer<HTMLElement, HTMLDivElement>(() => ({
+ count: displayLogs.length,
+ getScrollElement: () => logContainer ?? null,
+ getItemKey: (index) => displayLogs[index]?.key ?? index,
+ estimateSize: () => 32,
+ overscan: 12,
+ enabled: shouldVirtualize && !!logContainer
+ }));
+
+ // Follow new output (and late row measurements) to the bottom while auto-scroll is on.
+ $effect(() => {
+ void displayLogs;
+ void rowVirtualizer.totalSize;
+ if (logContainer && untrack(() => autoScroll)) {
+ logContainer.scrollTop = logContainer.scrollHeight;
+ }
+ });
+
$effect(() => {
const key = streamKey();
if (!key) {
@@ -492,9 +516,10 @@
</script>
{#snippet renderLogMessage(entry: LogViewerEntry, contentClass: string)}
+ {@const structured = showParsedJson ? getStructuredLog(entry) : null}
<div class={contentClass}>
- {#if entry.isStructured && showParsedJson && isStructuredLogData(entry.parsedJson)}
- <StructuredLogEntry data={entry.parsedJson} rawJson={entry.message} />
+ {#if structured?.isStructured && isStructuredLogData(structured.parsed)}
+ <StructuredLogEntry data={structured.parsed} rawJson={entry.message} />
{:else}
{@html ansiToHtml(entry.message)}
{/if}
@@ -542,6 +567,102 @@
</div>
{/snippet}
+{#snippet logRow(displayLog: LogViewerDisplayEntry, index: number)}
+ <!-- Mobile view -->
+ <div
+ class={cn(
+ 'border-l-2 border-transparent px-3 py-2 transition-colors hover:border-info hover:bg-card/50 sm:hidden',
+ index % 2 === 1 && 'bg-card/60'
+ )}
+ >
+ <div class="mb-1 flex flex-wrap items-center gap-2 text-xs">
+ {#if showTimestamps && getDisplayEntryTimestamp(displayLog)}
+ <span
+ class="inline-flex shrink-0 self-start rounded-md border border-info/15 bg-card px-2 py-1 font-semibold text-info tabular-nums"
+ title={getDisplayEntryTimestamp(displayLog)}
+ >
+ {formatLogTimestamp(getDisplayEntryTimestamp(displayLog))}
+ </span>
+ {/if}
+ {#if type === 'project' && getDisplayEntryService(displayLog)}
+ <span
+ class="shrink-0 truncate font-semibold {getServiceColor(getDisplayEntryService(displayLog)!)}"
+ title={getDisplayEntryService(displayLog)}
+ >
+ {getDisplayEntryService(displayLog)}
+ </span>
+ {/if}
+ {@render renderLevelLabel(getDisplayEntryLevel(displayLog), '')}
+ </div>
+ {#if displayLog.grouped}
+ <Collapsible.Root open={isGroupExpanded(displayLog)} onOpenChange={(open: boolean) => setGroupExpanded(displayLog, open)}>
+ {@render renderGroupedSummary(displayLog, 'text-sm break-words whitespace-pre-wrap text-foreground/80')}
+ <Collapsible.Content>
+ <div class="mt-2 space-y-1.5 border-l border-border pl-4">
+ {#each displayLog.entries.slice(1) as entry (entry.id)}
+ {@render renderLogMessage(entry, 'text-sm break-words whitespace-pre-wrap text-foreground/80')}
+ {/each}
+ </div>
+ </Collapsible.Content>
+ </Collapsible.Root>
+ {:else}
+ <div class="flex flex-col gap-1.5">
+ {#each displayLog.entries as entry (entry.id)}
+ {@render renderLogMessage(entry, 'text-sm break-words whitespace-pre-wrap text-foreground/80')}
+ {/each}
+ </div>
+ {/if}
+ </div>
+
+ <!-- Desktop view -->
+ <div
+ class={cn(
+ 'hidden items-start border-l-2 border-transparent px-3 py-1.5 transition-colors hover:border-info hover:bg-card/50 sm:flex',
+ index % 2 === 1 && 'bg-card/60'
+ )}
+ >
+ {#if showTimestamps && getDisplayEntryTimestamp(displayLog)}
+ <span
+ class="mr-3 inline-flex min-w-44.5 shrink-0 self-start rounded-md border border-info/15 bg-card px-2.5 py-1 text-2xs font-semibold text-info tabular-nums"
+ title={getDisplayEntryTimestamp(displayLog)}
+ >
+ {formatLogTimestamp(getDisplayEntryTimestamp(displayLog))}
+ </span>
+ {/if}
+ {#if type === 'project' && getDisplayEntryService(displayLog)}
+ <span
+ class="mr-3 max-w-30 min-w-30 shrink-0 truncate text-xs font-semibold {getServiceColor(
+ getDisplayEntryService(displayLog)!
+ )}"
+ title={getDisplayEntryService(displayLog)}
+ >
+ {getDisplayEntryService(displayLog)}
+ </span>
+ {/if}
+ {@render renderLevelLabel(getDisplayEntryLevel(displayLog), 'mr-2 min-w-fit pt-1 text-xs')}
+ <div class="min-w-0 flex-1">
+ {#if displayLog.grouped}
+ <Collapsible.Root open={isGroupExpanded(displayLog)} onOpenChange={(open: boolean) => setGroupExpanded(displayLog, open)}>
+ {@render renderGroupedSummary(displayLog, 'break-words whitespace-pre-wrap text-foreground/80')}
+ <Collapsible.Content>
+ <div class="mt-2 space-y-1 border-l border-border pl-4">
+ {#each displayLog.entries.slice(1) as entry (entry.id)}
+ {@render renderLogMessage(entry, 'break-words whitespace-pre-wrap text-foreground/80')}
+ {/each}
+ </div>
+ </Collapsible.Content>
+ </Collapsible.Root>
+ {:else}
+ <div class="flex flex-1 flex-col gap-1">
+ {#each displayLog.entries as entry (entry.id)}
+ {@render renderLogMessage(entry, 'break-words whitespace-pre-wrap text-foreground/80')}
+ {/each}
+ </div>
+ {/if}
+ </div>
+ </div>
+{/snippet}
+
<div
class={cn(
'dark rounded-t-none rounded-b-xl border bg-black text-white',
@@ -580,107 +701,11 @@
{m.log_viewer_waiting_for_logs()}
{/if}
</div>
+ {:else if shouldVirtualize}
+ <VirtualRows virtualizer={rowVirtualizer} rows={displayLogs} row={logRow} />
{:else}
{#each displayLogs as displayLog, index (displayLog.key)}
- <!-- Mobile view -->
- <div
- class={cn(
- 'border-l-2 border-transparent px-3 py-2 transition-colors hover:border-info hover:bg-card/50 sm:hidden',
- index % 2 === 1 && 'bg-card/60'
- )}
- >
- <div class="mb-1 flex flex-wrap items-center gap-2 text-xs">
- {#if showTimestamps && getDisplayEntryTimestamp(displayLog)}
- <span
- class="inline-flex shrink-0 self-start rounded-md border border-info/15 bg-card px-2 py-1 font-semibold text-info tabular-nums"
- title={getDisplayEntryTimestamp(displayLog)}
- >
- {formatLogTimestamp(getDisplayEntryTimestamp(displayLog))}
- </span>
- {/if}
- {#if type === 'project' && getDisplayEntryService(displayLog)}
- <span
- class="shrink-0 truncate font-semibold {getServiceColor(getDisplayEntryService(displayLog)!)}"
- title={getDisplayEntryService(displayLog)}
- >
- {getDisplayEntryService(displayLog)}
- </span>
- {/if}
- {@render renderLevelLabel(getDisplayEntryLevel(displayLog), '')}
- </div>
- {#if displayLog.grouped}
- <Collapsible.Root
- open={isGroupExpanded(displayLog)}
- onOpenChange={(open: boolean) => setGroupExpanded(displayLog, open)}
- >
- {@render renderGroupedSummary(displayLog, 'text-sm break-words whitespace-pre-wrap text-foreground/80')}
- <Collapsible.Content>
- <div class="mt-2 space-y-1.5 border-l border-border pl-4">
- {#each displayLog.entries.slice(1) as entry (entry.id)}
- {@render renderLogMessage(entry, 'text-sm break-words whitespace-pre-wrap text-foreground/80')}
- {/each}
- </div>
- </Collapsible.Content>
- </Collapsible.Root>
- {:else}
- <div class="flex flex-col gap-1.5">
- {#each displayLog.entries as entry (entry.id)}
- {@render renderLogMessage(entry, 'text-sm break-words whitespace-pre-wrap text-foreground/80')}
- {/each}
- </div>
- {/if}
- </div>
-
- <!-- Desktop view -->
- <div
- class={cn(
- 'hidden items-start border-l-2 border-transparent px-3 py-1.5 transition-colors hover:border-info hover:bg-card/50 sm:flex',
- index % 2 === 1 && 'bg-card/60'
- )}
- >
- {#if showTimestamps && getDisplayEntryTimestamp(displayLog)}
- <span
- class="mr-3 inline-flex min-w-44.5 shrink-0 self-start rounded-md border border-info/15 bg-card px-2.5 py-1 text-2xs font-semibold text-info tabular-nums"
- title={getDisplayEntryTimestamp(displayLog)}
- >
- {formatLogTimestamp(getDisplayEntryTimestamp(displayLog))}
- </span>
- {/if}
- {#if type === 'project' && getDisplayEntryService(displayLog)}
- <span
- class="mr-3 max-w-30 min-w-30 shrink-0 truncate text-xs font-semibold {getServiceColor(
- getDisplayEntryService(displayLog)!
- )}"
- title={getDisplayEntryService(displayLog)}
- >
- {getDisplayEntryService(displayLog)}
- </span>
- {/if}
- {@render renderLevelLabel(getDisplayEntryLevel(displayLog), 'mr-2 min-w-fit pt-1 text-xs')}
- <div class="min-w-0 flex-1">
- {#if displayLog.grouped}
- <Collapsible.Root
- open={isGroupExpanded(displayLog)}
- onOpenChange={(open: boolean) => setGroupExpanded(displayLog, open)}
- >
- {@render renderGroupedSummary(displayLog, 'break-words whitespace-pre-wrap text-foreground/80')}
- <Collapsible.Content>
- <div class="mt-2 space-y-1 border-l border-border pl-4">
- {#each displayLog.entries.slice(1) as entry (entry.id)}
- {@render renderLogMessage(entry, 'break-words whitespace-pre-wrap text-foreground/80')}
- {/each}
- </div>
- </Collapsible.Content>
- </Collapsible.Root>
- {:else}
- <div class="flex flex-1 flex-col gap-1">
- {#each displayLog.entries as entry (entry.id)}
- {@render renderLogMessage(entry, 'break-words whitespace-pre-wrap text-foreground/80')}
- {/each}
- </div>
- {/if}
- </div>
- </div>
+ {@render logRow(displayLog, index)}
{/each}
{/if}
</div>
diff --git a/frontend/src/lib/components/logs/log-viewer.utils.ts b/frontend/src/lib/components/logs/log-viewer.utils.ts
index 22d0d9520..ddb65015d 100644
--- a/frontend/src/lib/components/logs/log-viewer.utils.ts
+++ b/frontend/src/lib/components/logs/log-viewer.utils.ts
@@ -7,9 +7,14 @@ export interface LogViewerEntry {
message: string;
service?: string;
containerId?: string;
- parsedJson?: unknown;
- isJson?: boolean;
- isStructured?: boolean;
+ /** Parse result cached by getStructuredLog; absent until first requested. */
+ structured?: StructuredLogResult;
+}
+
+export interface StructuredLogResult {
+ isJson: boolean;
+ isStructured: boolean;
+ parsed?: Record<string, unknown> | unknown[];
}
export interface LogViewerDisplayEntry {
@@ -70,8 +75,8 @@ function canGroupLogEntries(previous: LogViewerEntry, current: LogViewerEntry):
previous.containerId === current.containerId &&
previous.level === current.level &&
looksLikeMultilineContinuation(previous.message, current.message) &&
- !previous.isStructured &&
- !current.isStructured
+ !getStructuredLog(previous).isStructured &&
+ !getStructuredLog(current).isStructured
);
}
@@ -254,11 +259,13 @@ function tryParseLogfmt(message: string): Record<string, unknown> | null {
return Object.keys(parsed).length > 0 ? parsed : null;
}
-export function tryParseStructuredLog(message: string): {
- isJson: boolean;
- isStructured: boolean;
- parsed?: Record<string, unknown> | unknown[];
-} {
+/** Parses an entry's message on first use and caches the result on the entry. */
+export function getStructuredLog(entry: LogViewerEntry): StructuredLogResult {
+ entry.structured ??= tryParseStructuredLog(entry.message);
+ return entry.structured;
+}
+
+function tryParseStructuredLog(message: string): StructuredLogResult {
const trimmed = sanitizeLogText(message).trim();
if (!trimmed) {
return { isJson: false, isStructured: false };
diff --git a/frontend/src/lib/components/release-notes.svelte b/frontend/src/lib/components/release-notes.svelte
index fe72b8fb3..f1269764d 100644
--- a/frontend/src/lib/components/release-notes.svelte
+++ b/frontend/src/lib/components/release-notes.svelte
@@ -1,28 +1,36 @@
<script lang="ts">
- import DOMPurify from 'isomorphic-dompurify';
- import { marked } from 'marked';
+ import { m } from '#lib/paraglide/messages.js';
let { markdown }: { markdown: string } = $props();
- const html = $derived.by(() => {
- if (!markdown) return '';
- try {
- const parsed = marked.parse(markdown, { async: false }) as string;
- return DOMPurify.sanitize(parsed, {
- ADD_ATTR: ['target', 'rel']
- });
- } catch {
- return '';
- }
- });
+ // marked and DOMPurify load on first render so they stay out of the app shell bundle.
+ const renderMarkdown = Promise.all([import('marked'), import('isomorphic-dompurify')]).then(
+ ([{ marked }, { default: DOMPurify }]) =>
+ (source: string): string => {
+ if (!source) return '';
+ try {
+ const parsed = marked.parse(source, { async: false }) as string;
+ return DOMPurify.sanitize(parsed, {
+ ADD_ATTR: ['target', 'rel']
+ });
+ } catch {
+ return '';
+ }
+ }
+ );
</script>
-{#if html}
- <div class="release-notes text-sm leading-relaxed">
- <!-- eslint-disable-next-line svelte/no-at-html-tags -->
- {@html html}
- </div>
-{/if}
+{#await renderMarkdown then render}
+ {@const html = render(markdown)}
+ {#if html}
+ <div class="release-notes text-sm leading-relaxed">
+ <!-- eslint-disable-next-line svelte/no-at-html-tags -->
+ {@html html}
+ </div>
+ {/if}
+{:catch}
+ <p class="text-sm text-destructive">{m.release_notes_load_failed()}</p>
+{/await}
<style>
.release-notes :global(h1),
diff --git a/frontend/src/lib/components/stat-card.svelte b/frontend/src/lib/components/stat-card.svelte
index 427e65b9f..d93a3f54f 100644
--- a/frontend/src/lib/components/stat-card.svelte
+++ b/frontend/src/lib/components/stat-card.svelte
@@ -67,7 +67,7 @@
{:else}
<div
class={cn(
- 'hover-lift group relative overflow-hidden rounded-xl border border-border/70 bg-card/60 p-4 backdrop-blur-md transition-colors',
+ 'hover-lift group relative overflow-hidden rounded-xl border border-border/70 bg-card/80 p-4 transition-colors',
iconColor,
className
)}
diff --git a/frontend/src/lib/components/virtual-rows.svelte b/frontend/src/lib/components/virtual-rows.svelte
index bfd6f1de9..87e87fddd 100644
--- a/frontend/src/lib/components/virtual-rows.svelte
+++ b/frontend/src/lib/components/virtual-rows.svelte
@@ -13,7 +13,7 @@
virtualizer: ReturnType<typeof createVirtualizer<HTMLElement, HTMLDivElement>>;
rows: TRow[];
class?: string;
- row: Snippet<[TRow]>;
+ row: Snippet<[TRow, number]>;
} = $props();
</script>
@@ -27,7 +27,7 @@
data-index={virtualItem.index}
{@attach virtualizer.measureElement}
>
- {@render row(item)}
+ {@render row(item, virtualItem.index)}
</div>
{/if}
{/each}
diff --git a/frontend/src/lib/constants/table-pagination.ts b/frontend/src/lib/constants/table-pagination.ts
index dc633cdd2..d2c6df569 100644
--- a/frontend/src/lib/constants/table-pagination.ts
+++ b/frontend/src/lib/constants/table-pagination.ts
@@ -1,2 +1,7 @@
export const TABLE_PAGE_SIZE_ALL = -1;
export const TABLE_PAGE_SIZE_OPTIONS = [10, 20, 30, 40, 50, 100] as const;
+
+/** Bulk reads walk a list in pages of this size instead of one unbounded request. */
+export const BULK_FETCH_PAGE_SIZE = 100;
+/** Bulk reads stop after this many rows; keep it a multiple of the page size. */
+export const BULK_FETCH_MAX_ITEMS = 5000;
diff --git a/frontend/src/lib/hooks/use-backup-activity.svelte.ts b/frontend/src/lib/hooks/use-backup-activity.svelte.ts
index 2b4e4daa0..a592aba55 100644
--- a/frontend/src/lib/hooks/use-backup-activity.svelte.ts
+++ b/frontend/src/lib/hooks/use-backup-activity.svelte.ts
@@ -21,7 +21,8 @@ export function useBackupActivity(
const activeQuery = createQuery(() => {
const environmentId = getEnvironmentId();
const discover = getDiscovery();
- const pollWithoutActivity = !!discover || !clientStream.streamConnected;
+ // Discovery has no activity stream; otherwise idle polling only runs while the stream is down.
+ const poll = !!discover || !clientStream.streamConnected;
return {
queryKey,
queryFn: async ({ signal }): Promise<string[]> => {
@@ -45,11 +46,8 @@ export function useBackupActivity(
}
return activities.filter(matches).map((activity) => activity.id);
},
- refetchInterval: (query) => {
- if (pollWithoutActivity || query.state.data?.length) return 5000;
- return false;
- },
- refetchIntervalInBackground: true,
+ // A running backup keeps polling as a fallback for missed stream events.
+ refetchInterval: (query) => (poll || query.state.data?.length ? 5000 : false),
staleTime: 0
};
});
diff --git a/frontend/src/lib/query/query-keys.ts b/frontend/src/lib/query/query-keys.ts
index 6a3c78da1..fe340d166 100644
--- a/frontend/src/lib/query/query-keys.ts
+++ b/frontend/src/lib/query/query-keys.ts
@@ -35,13 +35,21 @@ export const queryKeys = {
auth: {
all: ['auth'] as const,
logout: () => ['auth', 'logout'] as const,
+ currentUser: () => ['auth', 'current-user'] as const,
autoLoginConfig: () => ['auth', 'auto-login-config'] as const,
autoLoginAttempt: () => ['auth', 'auto-login-attempt'] as const
},
settings: {
all: ['settings'] as const,
global: () => ['settings', 'global'] as const,
- byEnvironment: (environmentId: string) => ['settings', environmentId] as const
+ byEnvironment: (environmentId: string) => ['settings', environmentId] as const,
+ public: (environmentId: string) => ['settings', 'public', environmentId] as const
+ },
+ roles: {
+ all: () => ['roles', 'all'] as const,
+ list: (options: SearchPaginationSortRequest) => ['roles', 'list', stableSerialize(options)] as const,
+ detail: (roleId: string) => ['roles', 'detail', roleId] as const,
+ permissionsManifest: () => ['roles', 'permissions-manifest'] as const
},
users: {
all: ['users'] as const,
@@ -104,6 +112,7 @@ export const queryKeys = {
deleteSelectedGlobal: () => ['events', 'delete-selected', 'global'] as const
},
system: {
+ appVersion: () => ['system', 'app-version'] as const,
upgradeAvailable: (scope: 'mobile-nav' | 'sidebar') => ['system', 'upgrade-available', scope] as const,
environmentUpgradeAvailable: (environmentId: string) =>
['system', 'upgrade-available', 'environment', environmentId] as const,
@@ -113,20 +122,24 @@ export const queryKeys = {
},
containers: {
all: ['containers'] as const,
+ listPrefix: (scope: string) => ['containers', scope] as const,
list: (environmentId: string, options: SearchPaginationSortRequest) =>
['containers', environmentId, stableSerialize(options)] as const,
create: (environmentId: string) => ['containers', 'create', environmentId] as const,
statusCounts: (environmentId: string) => ['containers', 'status-counts', environmentId] as const,
+ environment: (environmentId: string) => ['container', environmentId] as const,
detail: (environmentId: string, containerId: string) => ['container', environmentId, containerId] as const,
editConfig: (environmentId: string, containerId: string) => ['container', environmentId, containerId, 'edit-config'] as const,
processes: (environmentId: string, containerId: string) => ['container', environmentId, containerId, 'processes'] as const
},
images: {
all: ['images'] as const,
+ listPrefix: (scope: string) => ['images', scope] as const,
list: (environmentId: string, options: SearchPaginationSortRequest) =>
['images', environmentId, stableSerialize(options)] as const,
usageCounts: (environmentId: string) => ['images', 'usage-counts', environmentId] as const,
uploadLimit: (environmentId: string) => ['images', 'upload-limit', environmentId] as const,
+ environment: (environmentId: string) => ['image', environmentId] as const,
history: (environmentId: string, imageId: string) => ['image', environmentId, imageId, 'history'] as const,
detail: (environmentId: string, imageId: string) => ['image', environmentId, imageId] as const,
updateCheck: (environmentId: string, imageId: string) => ['image-update', environmentId, imageId] as const,
@@ -139,6 +152,7 @@ export const queryKeys = {
projects: {
all: ['projects'] as const,
environment: (environmentId: string) => ['project', environmentId] as const,
+ listPrefix: (scope: string) => ['projects', scope] as const,
list: (environmentId: string, options: SearchPaginationSortRequest) =>
['projects', environmentId, stableSerialize(options)] as const,
checkUpdates: (environmentId: string) => ['projects', 'check-updates', environmentId] as const,
@@ -191,6 +205,7 @@ export const queryKeys = {
},
vulnerabilities: {
overviewByEnvironment: (environmentId: string) => ['vulnerabilities', 'overview', environmentId] as const,
+ scanSummaries: (scope: string, imageKeys: string[]) => ['vulnerabilities', 'scan-summaries', scope, ...imageKeys] as const,
scanResult: (environmentId: string, imageId: string) => ['vulnerabilities', 'scan-result', environmentId, imageId] as const,
allByEnvironment: (environmentId: string, request: SearchPaginationSortRequest) =>
['vulnerabilities', 'all', environmentId, stableSerialize(request)] as const,
diff --git a/frontend/src/lib/stores/environment.store.svelte.ts b/frontend/src/lib/stores/environment.store.svelte.ts
index 79a9f24fc..a91b90445 100644
--- a/frontend/src/lib/stores/environment.store.svelte.ts
+++ b/frontend/src/lib/stores/environment.store.svelte.ts
@@ -1,5 +1,6 @@
import { goto, refreshAll } from '$app/navigation';
import { page } from '$app/state';
+import { replaceEqualDeep } from '@tanstack/svelte-query';
import { PersistedState } from 'runed';
import userStore from '#lib/stores/user-store.svelte.js';
@@ -147,6 +148,7 @@ function createEnvironmentManagementStore() {
return this.shown.map((env) => env.id).join(',');
},
initialize: async (environmentsData: Environment[]) => {
+ const previousShownKey = environmentStore.shownKey;
const available = _updateAvailable(environmentsData);
const hasRealEnvironments = environmentsData.length > 0;
@@ -167,6 +169,12 @@ function createEnvironmentManagementStore() {
return;
}
const updated = available.find((env) => env.id === _selectedEnvironment!.id);
+ // Like useEnvironmentRefresh's scope key: an unchanged selection and shown set needs no notify.
+ const unchanged =
+ updated?.enabled &&
+ replaceEqualDeep(_selectedEnvironment, updated) === _selectedEnvironment &&
+ previousShownKey === environmentStore.shownKey;
+ if (unchanged) return;
if (updated) _assignSelectedEnvironment(updated);
if (!updated?.enabled) _selectInitialEnvironment(available);
},
diff --git a/frontend/src/lib/stores/features.store.svelte.ts b/frontend/src/lib/stores/features.store.svelte.ts
index 92a483415..2ff92fe33 100644
--- a/frontend/src/lib/stores/features.store.svelte.ts
+++ b/frontend/src/lib/stores/features.store.svelte.ts
@@ -28,7 +28,9 @@ async function load(environmentId: string, force = false): Promise<void> {
if (!force) {
const pending = requests.get(environmentId);
if (pending) return pending;
- if (environments[environmentId]?.status === 'ready') return;
+ // An unavailable environment stays settled until a forced refresh, such as its reconnect.
+ const status = environments[environmentId]?.status;
+ if (status === 'ready' || status === 'unavailable') return;
}
controllers.get(environmentId)?.abort();
const controller = new AbortController();
diff --git a/frontend/src/lib/types/settings.ts b/frontend/src/lib/types/settings.ts
index 80c998e08..ef2bc24bb 100644
--- a/frontend/src/lib/types/settings.ts
+++ b/frontend/src/lib/types/settings.ts
@@ -137,6 +137,8 @@ export type Settings = {
buildProvider: 'local' | 'depot';
buildsDirectory: string;
buildTimeout: number;
+ // Missing on agents that predate the setting.
+ buildHistoryRetentionDays?: number;
depotProjectId: string;
depotToken?: string;
depotConfigured?: boolean;
diff --git a/frontend/src/routes/(app)/containers/[containerId]/+page.ts b/frontend/src/routes/(app)/containers/[containerId]/+page.ts
index 0d3ae53ca..fbf2d6216 100644
--- a/frontend/src/routes/(app)/containers/[containerId]/+page.ts
+++ b/frontend/src/routes/(app)/containers/[containerId]/+page.ts
@@ -3,6 +3,7 @@ import { error } from '@sveltejs/kit';
import { queryKeys } from '#lib/query/query-keys.js';
import { containerService } from '#lib/services/container-service.js';
import { projectService } from '#lib/services/project-service.js';
+import type { SearchPaginationSortRequest } from '#lib/types/shared.js';
import { throwPageLoadError } from '#lib/utils/api.js';
import { resolveDetailEnvironmentId } from '#lib/utils/environments.js';
import { tryCatch } from '#lib/utils/try-catch.js';
@@ -33,15 +34,17 @@ export const load: PageLoad = async ({ params, parent, url }) => {
if (composeProjectName) {
const operationResult = await tryCatch(
(async () => {
- const searchOptions = {
- search: composeProjectName,
- pagination: { page: 1, limit: 100 } // Ensure we don't miss projects beyond default page size
+ const findByName = async (options: SearchPaginationSortRequest) => {
+ const projectsResult = await queryClient.query({
+ queryKey: queryKeys.projects.list(envId, options),
+ queryFn: () => projectService.getProjectsForEnvironment(envId, options)
+ });
+ return projectsResult.data.find((p) => p.name === composeProjectName);
};
- const projectsResult = await queryClient.query({
- queryKey: queryKeys.projects.list(envId, searchOptions),
- queryFn: () => projectService.getProjectsForEnvironment(envId, searchOptions)
- });
- const matched = projectsResult.data.find((p) => p.name === composeProjectName);
+ // Agents without the name filter ignore it, so fall back to the searched lookup.
+ const matched =
+ (await findByName({ filters: { name: composeProjectName }, pagination: { page: 1, limit: 1 } })) ??
+ (await findByName({ search: composeProjectName, pagination: { page: 1, limit: 100 } }));
if (matched) {
return await queryClient.query({
queryKey: queryKeys.projects.detail(envId, matched.id),
diff --git a/frontend/src/routes/(app)/containers/components/container-table.svelte b/frontend/src/routes/(app)/containers/components/container-table.svelte
index 91c6c81e8..0d6f06b34 100644
--- a/frontend/src/routes/(app)/containers/components/container-table.svelte
+++ b/frontend/src/routes/(app)/containers/components/container-table.svelte
@@ -128,11 +128,11 @@
() => {
if (!updateTableMounted || scope !== environmentStore.shownKey) return;
for (const listScope of listScopes(environmentStore.shown.map((env) => env.id))) {
- void queryClient.invalidateQueries({ queryKey: ['containers', listScope] });
- void queryClient.invalidateQueries({ queryKey: ['projects', listScope] });
+ void queryClient.invalidateQueries({ queryKey: queryKeys.containers.listPrefix(listScope) });
+ void queryClient.invalidateQueries({ queryKey: queryKeys.projects.listPrefix(listScope) });
}
for (const env of environmentStore.shown) {
- void queryClient.invalidateQueries({ queryKey: ['container', env.id] });
+ void queryClient.invalidateQueries({ queryKey: queryKeys.containers.environment(env.id) });
void queryClient.invalidateQueries({ queryKey: queryKeys.projects.environment(env.id) });
}
void refreshContainers(requestOptions);
diff --git a/frontend/src/routes/(app)/environments/[id]/+page.svelte b/frontend/src/routes/(app)/environments/[id]/+page.svelte
index 4e0cdd68e..58d9e7c48 100644
--- a/frontend/src/routes/(app)/environments/[id]/+page.svelte
+++ b/frontend/src/routes/(app)/environments/[id]/+page.svelte
@@ -1,7 +1,6 @@
<script lang="ts">
import { beforeNavigate, goto, refreshAll } from '$app/navigation';
- import { createQuery } from '@tanstack/svelte-query';
- import { onMount } from 'svelte';
+ import { createQuery, useQueryClient } from '@tanstack/svelte-query';
import { toast } from 'svelte-sonner';
import { ActionButtonGroup, type ActionButton } from '#lib/components/action-button-group/index.js';
@@ -73,13 +72,14 @@
let lastEnvironment: Environment | undefined;
let loadedEnvironment = $derived((lastEnvironment = data.environment ?? lastEnvironment));
let environment = $derived(loadedEnvironment);
- let refreshedEnvironment: Environment | null = $state(null);
- let runtimeEnvironment: Environment = $derived.by(() => {
- const refreshed = refreshedEnvironment;
- return refreshed && refreshed.id === environment.id ? refreshed : environment;
- });
-
- let currentEnvironment = $derived(environmentStore.selected);
+ // Edge reachability only reaches the browser through the live environment stream; the certificate is detail-only.
+ const liveEnvironment = $derived(
+ environment.isEdge ? environmentStore.available.find((env) => env.id === environment.id) : undefined
+ );
+ let runtimeEnvironment: Environment = $derived(
+ liveEnvironment ? { ...liveEnvironment, edgeMTLSCertificate: environment.edgeMTLSCertificate } : environment
+ );
+ const queryClient = useQueryClient();
let isRefreshing = $state(false);
let isTestingConnection = $state(false);
@@ -481,18 +481,9 @@
if (overridden) throw new Error(featureOverrideMessage(overridden.id));
}
+ // The root load's refresh re-reads the environment list into the store.
+ await queryClient.invalidateQueries({ queryKey: queryKeys.environments.all });
await refreshEnvironment();
-
- // Update environment store if this is the current environment
- if (currentEnvironment?.id === environment.id) {
- await environmentStore.initialize(
- (
- await environmentManagementService.getEnvironments({
- pagination: { page: 1, limit: 1000 }
- })
- ).data
- );
- }
}
function featureOverrideMessage(id: FeatureID): string {
@@ -562,35 +553,6 @@
}
}
- onMount(() => {
- if (environment.isEdge) {
- void refreshRuntimeEnvironment();
- }
-
- const interval = window.setInterval(() => {
- if (!environment.isEdge) return;
- void refreshRuntimeEnvironment();
- }, 5000);
-
- return () => window.clearInterval(interval);
- });
-
- async function refreshRuntimeEnvironment() {
- const operationResult = await tryCatch(
- (async () => {
- const latestEnvironment = await environmentManagementService.get(environment.id);
- if (latestEnvironment.id === environment.id) {
- refreshedEnvironment = latestEnvironment;
- }
- })()
- );
- if (operationResult.error !== null) {
- const error = operationResult.error;
-
- console.debug('Failed to refresh environment runtime state:', error);
- }
- }
-
async function refreshEnvironment() {
if (isRefreshing) return;
try {
diff --git a/frontend/src/routes/(app)/environments/[id]/components/environment-connection-details.svelte b/frontend/src/routes/(app)/environments/[id]/components/environment-connection-details.svelte
index c80f73206..79c8efa0b 100644
--- a/frontend/src/routes/(app)/environments/[id]/components/environment-connection-details.svelte
+++ b/frontend/src/routes/(app)/environments/[id]/components/environment-connection-details.svelte
@@ -36,9 +36,11 @@
// Heartbeats arrive every 30s, so three missed ones means the tunnel is struggling.
const staleHeartbeatMs = 90_000;
- // Relative times keep ticking between data refreshes; the subscriber re-derives `now` every second while rendered.
+ // Relative times keep ticking between data refreshes; 10s keeps second-level labels close without a per-second render.
const subscribeToClock = createSubscriber((update) => {
- const timer = setInterval(update, 1000);
+ const timer = setInterval(() => {
+ if (!document.hidden) update();
+ }, 10_000);
return () => clearInterval(timer);
});
let now = $derived.by(() => {
diff --git a/frontend/src/routes/(app)/environments/[id]/components/jobs-tab.svelte b/frontend/src/routes/(app)/environments/[id]/components/jobs-tab.svelte
index 33efe2a57..4f5ad2410 100644
--- a/frontend/src/routes/(app)/environments/[id]/components/jobs-tab.svelte
+++ b/frontend/src/routes/(app)/environments/[id]/components/jobs-tab.svelte
@@ -4,7 +4,7 @@
import JobDetailPanel from '#lib/components/job-card/job-detail-panel.svelte';
import JobRow from '#lib/components/job-card/job-row.svelte';
- import { jobNameLabel, type JobPanelSection } from '#lib/components/job-card/job-status.js';
+ import { isJobRunPending, jobNameLabel, type JobPanelSection } from '#lib/components/job-card/job-status.js';
import SettingsSection from '#lib/components/settings/settings-section.svelte';
import { EmptyState } from '#lib/components/states/index.js';
import * as Alert from '#lib/components/ui/alert/index.js';
@@ -32,7 +32,6 @@
parseExcludedContainerSet
} from '#lib/utils/container-auto-update.js';
import { formatRelativeTime, parseInstant } from '#lib/utils/formatting.js';
- import { tryCatch } from '#lib/utils/try-catch.js';
import type { JobsTabProps } from './tab-props';
@@ -53,22 +52,29 @@
};
},
enabled: !!environmentId && canManageJobs,
- refetchInterval: 5000
+ // Idle polling still picks up scheduled runs and worker health changes.
+ refetchInterval: (query) =>
+ query.state.data?.jobs.some(
+ (job) =>
+ isJobRunPending(job.currentRun?.status) || job.children?.some((child) => isJobRunPending(child.currentRun?.status))
+ )
+ ? 5000
+ : 30_000
}));
const jobsResponse = $derived(jobsQuery.data);
const capabilityUnknown = $derived(
!!jobsResponse?.offline && (!jobsResponse.observedAt || jobsResponse.observedAt.startsWith('0001-'))
);
- const containersPromise = $derived.by(async () => {
- if (!environmentId) return [];
- if (!formInputs.autoUpdate.value && !formInputs.autoHealEnabled.value) return [];
- const result = await tryCatch(
- containerService.getContainersForEnvironment(environmentId, { pagination: { page: 1, limit: 100 } })
- );
- if (result.error) throw result.error;
- return result.data.data;
+ const containersQuery = createQuery(() => {
+ const options = { pagination: { page: 1, limit: 100 } };
+ return {
+ queryKey: queryKeys.containers.list(environmentId, options),
+ queryFn: () => containerService.getContainersForEnvironment(environmentId, options),
+ enabled: !!environmentId && (formInputs.autoUpdate.value || formInputs.autoHealEnabled.value)
+ };
});
+ const containers = $derived(containersQuery.data?.data ?? []);
let searchTerm = $state('');
let autoHealSearchTerm = $state('');
@@ -249,11 +255,11 @@
<div class="space-y-1">
<Label>
{m.excluded_containers()}
- {#await containersPromise then containers}
+ {#if containersQuery.isSuccess}
<span class="ml-1 font-normal text-muted-foreground">
({containers.filter((c) => excludedContainers.has(getContainerName(c))).length})
</span>
- {/await}
+ {/if}
</Label>
<p class="text-xs text-muted-foreground">{m.auto_update_exclude_description()}</p>
<p class="text-xs text-muted-foreground">
@@ -323,11 +329,11 @@
<div class="space-y-1">
<Label>
{m.excluded_containers()}
- {#await containersPromise then containers}
+ {#if containersQuery.isSuccess}
<span class="ml-1 font-normal text-muted-foreground">
({containers.filter((c) => autoHealExcludedContainers.has(getContainerName(c))).length})
</span>
- {/await}
+ {/if}
</Label>
<p class="text-xs text-muted-foreground">{m.auto_heal_exclude_description()}</p>
</div>
@@ -409,11 +415,16 @@
<ScrollArea.Root class="h-full">
<div class="p-2">
<div class="space-y-2">
- {#await containersPromise}
+ {#if containersQuery.isLoading}
<div class="flex items-center justify-center p-4">
<Spinner class="size-4" />
</div>
- {:then containers}
+ {:else if containersQuery.isError}
+ <div class="space-y-1 p-2 text-sm text-destructive">
+ <p>{m.jobs_containers_load_error()}</p>
+ <p class="text-xs opacity-80">{extractApiErrorMessage(containersQuery.error)}</p>
+ </div>
+ {:else}
{@const allItems = containers.map(config.mapItem)}
{@const filteredItems = config.term
? allItems.filter((item) => item.label.toLowerCase().includes(config.term.toLowerCase()))
@@ -445,11 +456,7 @@
</div>
{/each}
{/if}
- {:catch error}
- <div class="p-2 text-sm text-destructive">
- {(error instanceof Error ? error.message : '') || m.jobs_containers_load_error()}
- </div>
- {/await}
+ {/if}
</div>
</div>
</ScrollArea.Root>
diff --git a/frontend/src/routes/(app)/images/components/image-table.svelte b/frontend/src/routes/(app)/images/components/image-table.svelte
index 9be9e9e89..285c372c7 100644
--- a/frontend/src/routes/(app)/images/components/image-table.svelte
+++ b/frontend/src/routes/(app)/images/components/image-table.svelte
@@ -1,7 +1,7 @@
<script lang="ts">
import { goto } from '$app/navigation';
- import { useQueryClient } from '@tanstack/svelte-query';
- import { onMount, onDestroy, tick } from 'svelte';
+ import { createQuery } from '@tanstack/svelte-query';
+ import { onDestroy } from 'svelte';
import { toast } from 'svelte-sonner';
import { Temporal } from 'temporal-polyfill';
@@ -80,7 +80,6 @@
loading?: boolean;
} = $props();
- const queryClient = useQueryClient();
let isLoading = $state({
removing: false,
checking: false
@@ -89,9 +88,6 @@
const vulnerabilityManagementEnabled = $derived(
environmentStore.shown.some((env) => featureStore.isEnabled('vulnerabilityManagement', env.id))
);
- $effect(() => {
- if (!vulnerabilityManagementEnabled) stopBatchScanPolling();
- });
// Track the user store: hasPermission reads it non-reactively, so without
// this the deriveds would cache pre-hydration falses forever.
const canDeleteImage = $derived.by(() => {
@@ -121,9 +117,7 @@
let tagDialogImage = $state<ImageRow | null>(null);
let scanRequestedAtByImage = $state<Record<string, string>>({});
- let scanPollTimeout: ReturnType<typeof setTimeout> | null = null;
- let scanPollInFlight = false;
- // Set once on destroy so an in-flight poll can't re-arm a timer on a dead component.
+ // Set once on destroy so late async results don't touch a dead component.
let destroyed = false;
const SCAN_POLL_INTERVAL_MS = 4000;
@@ -245,7 +239,6 @@
}
toast.info(m.vuln_scan_started(), activityToastOptions(data.activityId));
- startBatchScanPolling();
}
});
}
@@ -296,130 +289,85 @@
}
if (newScanSummary.status === 'completed' || newScanSummary.status === 'failed') {
await onImageUpdated?.();
- } else if (isVulnerabilityScanInProgress(newScanSummary.status)) {
- startBatchScanPolling();
- }
- }
-
- function stopBatchScanPolling() {
- if (scanPollTimeout) {
- clearTimeout(scanPollTimeout);
- scanPollTimeout = null;
}
}
- function getScanningImages(): ImageRow[] {
+ const scanningImages = $derived.by(() => {
if (!vulnerabilityManagementEnabled) return [];
return (images.data ?? []).filter((item) => isVulnerabilityScanInProgress(item.vulnerabilityScan?.status));
- }
+ });
- let scanPollError = $state<string | null>(null);
+ // Polls only while rows are scanning; TanStack pauses it while the tab is hidden.
+ const scanSummariesQuery = createQuery(() => {
+ const scanning = scanningImages;
+ const scope = environmentStore.shownKey;
+ return {
+ queryKey: queryKeys.vulnerabilities.scanSummaries(scope, scanning.map(rowKey)),
+ queryFn: async () => {
+ const environments = environmentStore.shown.filter((env) => scanning.some((image) => image.environmentId === env.id));
+ // A failing environment is reported; the others' summaries still apply.
+ const { values, failures } = await collectAcrossEnvironments(environments, async (environmentId) => {
+ const ids = scanning.filter((image) => image.environmentId === environmentId).map((image) => image.id);
+ const response = await vulnerabilityService.getScanSummaries(ids, environmentId);
+ return scanning
+ .filter((image) => image.environmentId === environmentId && response?.summaries?.[image.id])
+ .map((image) => [rowKey(image), response.summaries[image.id]!] as const);
+ });
+ const summaries = Object.fromEntries(values.flatMap(({ value }) => value));
+ // Rows update from the fetch itself; a scope switch mid-request discards the result.
+ if (scope === environmentStore.shownKey) await applyScanSummaries(summaries);
+ return { summaries, failures };
+ },
+ enabled: scanning.length > 0,
+ refetchInterval: SCAN_POLL_INTERVAL_MS,
+ staleTime: 0,
+ gcTime: 0
+ };
+ });
- async function pollBatchScanSummaries() {
- const requestedScope = environmentStore.shownKey;
- const scanning = getScanningImages();
- if (scanning.length === 0) {
- stopBatchScanPolling();
- return;
- }
+ const scanPollError = $derived.by(() => {
+ if (scanSummariesQuery.error) return extractApiErrorMessage(scanSummariesQuery.error);
+ return (
+ scanSummariesQuery.data?.failures.map(({ environment, message }) => `${environment.name}: ${message}`).join('\n') || null
+ );
+ });
- if (scanPollInFlight) {
- scheduleBatchScanPolling();
- return;
- }
+ async function applyScanSummaries(summaries: Record<string, VulnerabilityScanSummary>) {
+ if (destroyed || Object.keys(summaries).length === 0 || !images.data?.length) return;
+ let changed = false;
+ let completed = false;
+ const nextScanRequestedAtByImage = { ...scanRequestedAtByImage };
+ const nextData = images.data.map((img) => {
+ const key = rowKey(img);
+ const summary = summaries[key];
+ if (!summary) return img;
+
+ if (
+ summary.status === 'failed' &&
+ isLikelyStaleFailedSummary(summary, nextScanRequestedAtByImage[key]) &&
+ isVulnerabilityScanInProgress(img.vulnerabilityScan?.status)
+ ) {
+ return img;
+ }
- scanPollInFlight = true;
- try {
- const operationResult = await tryCatch(
- (async () => {
- const environments = environmentStore.shown.filter((env) => scanning.some((image) => image.environmentId === env.id));
- // A failing environment is reported; the others' summaries still apply.
- const { values, failures } = await collectAcrossEnvironments(environments, async (environmentId) => {
- const ids = scanning.filter((image) => image.environmentId === environmentId).map((image) => image.id);
- const response = await vulnerabilityService.getScanSummaries(ids, environmentId);
- return scanning
- .filter((image) => image.environmentId === environmentId && response?.summaries?.[image.id])
- .map((image) => [rowKey(image), response.summaries[image.id]!] as const);
- });
- if (destroyed || !vulnerabilityManagementEnabled || requestedScope !== environmentStore.shownKey) return [];
- const summaries = Object.fromEntries(values.flatMap(({ value }) => value));
-
- if (Object.keys(summaries).length > 0 && images.data?.length) {
- let changed = false;
- let completed = false;
- const nextScanRequestedAtByImage = { ...scanRequestedAtByImage };
- const nextData = images.data.map((img) => {
- const key = rowKey(img);
- const summary = summaries[key];
- if (!summary) return img;
-
- if (
- summary.status === 'failed' &&
- isLikelyStaleFailedSummary(summary, nextScanRequestedAtByImage[key]) &&
- isVulnerabilityScanInProgress(img.vulnerabilityScan?.status)
- ) {
- return img;
- }
-
- changed = true;
- if (summary.status === 'completed' || summary.status === 'failed') {
- completed = true;
- delete nextScanRequestedAtByImage[key];
- }
- return { ...img, vulnerabilityScan: summary };
- });
- if (changed) {
- images = { ...images, data: nextData };
- scanRequestedAtByImage = nextScanRequestedAtByImage;
- }
- if (completed) {
- await onImageUpdated?.();
- }
- }
- return failures;
- })()
- );
- scanPollError =
- operationResult.error !== null
- ? extractApiErrorMessage(operationResult.error)
- : operationResult.data.map(({ environment, message }) => `${environment.name}: ${message}`).join('\n') || null;
- } finally {
- scanPollInFlight = false;
- if (!destroyed && getScanningImages().length > 0) {
- scheduleBatchScanPolling();
+ changed = true;
+ if (summary.status === 'completed' || summary.status === 'failed') {
+ completed = true;
+ delete nextScanRequestedAtByImage[key];
}
+ return { ...img, vulnerabilityScan: summary };
+ });
+ if (changed) {
+ images = { ...images, data: nextData };
+ scanRequestedAtByImage = nextScanRequestedAtByImage;
}
- }
-
- function scheduleBatchScanPolling() {
- stopBatchScanPolling();
- scanPollTimeout = setTimeout(() => void pollBatchScanSummaries(), SCAN_POLL_INTERVAL_MS);
- }
-
- function startBatchScanPolling() {
- if (scanPollTimeout) return;
- void pollBatchScanSummaries();
- }
-
- onMount(() => {
- const cache = queryClient.getQueryCache();
- function updateScanPolling() {
- if (destroyed) return;
- if (getScanningImages().length > 0) startBatchScanPolling();
- else stopBatchScanPolling();
+ if (completed) {
+ await onImageUpdated?.();
}
- updateScanPolling();
- return cache.subscribe((event) => {
- if (event.type !== 'updated' && event.type !== 'observerResultsUpdated') return;
- if (event.query !== cache.find({ queryKey: queryKeys.images.list(environmentStore.shownKey, requestOptions), exact: true }))
- return;
- void tick().then(updateScanPolling);
- });
- });
+ }
onDestroy(() => {
destroyed = true;
- stopBatchScanPolling();
});
const allColumns = $derived([
diff --git a/frontend/src/routes/(app)/projects/+page.svelte b/frontend/src/routes/(app)/projects/+page.svelte
index a15bef171..31b6c65ed 100644
--- a/frontend/src/routes/(app)/projects/+page.svelte
+++ b/frontend/src/routes/(app)/projects/+page.svelte
@@ -7,6 +7,7 @@
import { openConfirmDialog } from '#lib/components/confirm-dialog/index.js';
import { EmptyState } from '#lib/components/states/index.js';
+ import { TABLE_PAGE_SIZE_ALL } from '#lib/constants/table-pagination.js';
import { useEnvironmentRefresh } from '#lib/hooks/use-environment-refresh.svelte.js';
import { BoxIcon, ProjectsIcon, StartIcon, StopIcon } from '#lib/icons/index.js';
import { ResourcePageLayout, type ActionButton, type StatCardConfig } from '#lib/layouts/index.js';
@@ -123,34 +124,24 @@
});
async function updateEnvironmentProjects(requestedEnvId: string) {
- // Refresh update info for all images, then use the image->project usage
- // map to narrow the redeploy to projects that actually have updates.
- // This avoids hitting every project (and its registry) when nothing has
- // changed, which is especially expensive on instances with many projects.
+ // Refresh update info for all images, then redeploy only the projects the
+ // list reports as having updates. This avoids hitting every project (and
+ // its registry) when nothing has changed, which is especially expensive on
+ // instances with many projects.
const imageCheckResults = await imageService.checkAllImages(requestedEnvId);
- const images = await imageService.getImagesForEnvironment(requestedEnvId, {
- pagination: { page: 1, limit: 10000 }
+ // The updates filter is evaluated over every candidate in one pass, so a single unbounded request is cheapest.
+ const projectsWithUpdates = await projectService.getProjectsForEnvironment(requestedEnvId, {
+ filters: { updates: 'has_update' },
+ pagination: { page: 1, limit: TABLE_PAGE_SIZE_ALL }
});
- const projectIdsWithUpdates = new Set<string>();
- for (const img of images.data) {
- if (!img.updateInfo?.hasUpdate) continue;
- for (const user of img.usedBy ?? []) {
- if (user.type === 'project' && user.id) {
- projectIdsWithUpdates.add(user.id);
- }
- }
- }
+ // Untracked compose rows cannot be deployed by ID.
+ const projectsToUpdate = projectsWithUpdates.data.filter((p) => !p.isDiscovered);
- if (projectIdsWithUpdates.size === 0) {
+ if (projectsToUpdate.length === 0) {
return { updated: 0, activityId: extractActivityId(imageCheckResults) };
}
- const allProjects = await projectService.getProjectsForEnvironment(requestedEnvId, {
- pagination: { page: 1, limit: 1000 }
- });
- const projectsToUpdate = allProjects.data.filter((p) => projectIdsWithUpdates.has(p.id));
-
const results = await Promise.allSettled(
projectsToUpdate.map(async (proj) => {
// deployProject with pullPolicy 'always' already pulls fresh images,
diff --git a/frontend/src/routes/(app)/security/+page.svelte b/frontend/src/routes/(app)/security/+page.svelte
index ded723801..6da9e0929 100644
--- a/frontend/src/routes/(app)/security/+page.svelte
+++ b/frontend/src/routes/(app)/security/+page.svelte
@@ -7,7 +7,7 @@
import { EmptyState } from '#lib/components/states/index.js';
import { TabBar, type TabItem } from '#lib/components/tab-bar/index.js';
import * as Tabs from '#lib/components/ui/tabs/index.js';
- import { TABLE_PAGE_SIZE_ALL } from '#lib/constants/table-pagination.js';
+ import { BULK_FETCH_MAX_ITEMS, BULK_FETCH_PAGE_SIZE } from '#lib/constants/table-pagination.js';
import { useEnvironmentRefresh } from '#lib/hooks/use-environment-refresh.svelte.js';
import { useUrlTab } from '#lib/hooks/use-url-tab.svelte.js';
import { ActivityIcon, ImagesIcon, InspectIcon, ScanIcon, ShieldAlertIcon, ShieldCheckIcon } from '#lib/icons/index.js';
@@ -22,7 +22,7 @@
import type { Paginated, SearchPaginationSortRequest } from '#lib/types/shared.js';
import { extractApiErrorMessage, parallelRefresh } from '#lib/utils/api.js';
import { hasPermission } from '#lib/utils/auth.js';
- import { fetchAcrossEnvironments } from '#lib/utils/environments.js';
+ import { fetchAcrossEnvironments, withFailedEnvironments } from '#lib/utils/environments.js';
import { tryCatch } from '#lib/utils/try-catch.js';
import {
getSeveritySegments,
@@ -195,6 +195,11 @@
const tick = async () => {
if (destroyed || !vulnerabilityManagementEnabled) return;
+ // Hidden tabs skip the refresh without spending an attempt.
+ if (document.hidden) {
+ scanPollTimeout = setTimeout(tick, POLL_INTERVAL_MS);
+ return;
+ }
if (attempts >= MAX_ATTEMPTS) {
stopScanPolling();
return;
@@ -289,17 +294,26 @@
try {
const operationResult = await tryCatch(
(async () => {
- // Fetch all images with a high limit to get all of them; merged, every environment's whole list.
+ // Walk the merged image list in fixed-size pages, up to a total cap.
const environments = vulnerabilityEnvironments().filter((env) => hasPermission('vulnerabilities:scan', env.id));
- const limit = environments.length > 1 ? TABLE_PAGE_SIZE_ALL : 1000;
- const imagesResponse = await fetchAcrossEnvironments(
- environments,
- { pagination: { page: 1, limit } },
- (id, request, config) => imageService.getImagesForEnvironment(id, request, config)
- );
- const images = imagesResponse.data ?? [];
+ const fetchImagesPage = (page: number) =>
+ fetchAcrossEnvironments(environments, { pagination: { page, limit: BULK_FETCH_PAGE_SIZE } }, (id, request, config) =>
+ imageService.getImagesForEnvironment(id, request, config)
+ );
+ const firstPage = await fetchImagesPage(1);
+ const imagePages = [firstPage];
+ for (
+ let page = 2;
+ page <= firstPage.pagination.totalPages && imagePages.length * BULK_FETCH_PAGE_SIZE < BULK_FETCH_MAX_ITEMS;
+ page++
+ ) {
+ imagePages.push(await fetchImagesPage(page));
+ }
+ const images = imagePages.flatMap((imagePage) => imagePage.data ?? []);
// Environments whose images could not be listed were not scanned.
- const skipped = imagesResponse.failedEnvironments ?? [];
+ const skipped =
+ withFailedEnvironments(firstPage, ...imagePages.slice(1).map((imagePage) => imagePage.failedEnvironments))
+ .failedEnvironments ?? [];
for (const { environment, message } of skipped) {
toast.error(m.all_environments_request_failed({ environment: environment.name }), {
id: `environment-request-failed-${environment.id}`,
@@ -307,6 +321,10 @@
});
}
+ if (firstPage.pagination.totalItems > images.length) {
+ toast.warning(m.security_scan_all_capped({ count: images.length, total: firstPage.pagination.totalItems }));
+ }
+
if (images.length === 0) {
toast.info(m.security_no_images_to_scan());
isLoading.scanningAll = false;
diff --git a/frontend/src/routes/(app)/settings/api-keys/+page.ts b/frontend/src/routes/(app)/settings/api-keys/+page.ts
index ce9268b57..214b459c7 100644
--- a/frontend/src/routes/(app)/settings/api-keys/+page.ts
+++ b/frontend/src/routes/(app)/settings/api-keys/+page.ts
@@ -26,7 +26,7 @@ export const load: PageLoad = async ({ parent }) => {
queryFn: () => apiKeyService.getApiKeys(apiKeyRequestOptions)
}),
queryClient.query({
- queryKey: ['permissions', 'manifest'],
+ queryKey: queryKeys.roles.permissionsManifest(),
queryFn: () => roleService.getPermissionsManifest(),
staleTime: Infinity
})
diff --git a/frontend/src/routes/(app)/settings/authentication/+page.ts b/frontend/src/routes/(app)/settings/authentication/+page.ts
index 683d938c7..851516fc9 100644
--- a/frontend/src/routes/(app)/settings/authentication/+page.ts
+++ b/frontend/src/routes/(app)/settings/authentication/+page.ts
@@ -32,7 +32,7 @@ export const load: PageLoad = async ({ parent }) => {
queryFn: () => federatedCredentialService.list(federatedCredentialRequestOptions)
}),
queryClient.query({
- queryKey: ['roles', 'all'],
+ queryKey: queryKeys.roles.all(),
queryFn: () => roleService.getAll()
}),
queryClient.query({
diff --git a/frontend/src/routes/(app)/settings/builds/+page.svelte b/frontend/src/routes/(app)/settings/builds/+page.svelte
index 04f23263e..669825152 100644
--- a/frontend/src/routes/(app)/settings/builds/+page.svelte
+++ b/frontend/src/routes/(app)/settings/builds/+page.svelte
@@ -21,6 +21,7 @@
buildProvider: z.enum(['local', 'depot']).default('local'),
buildsDirectory: z.string().default(''),
buildTimeout: z.coerce.number().int().min(60).max(14400),
+ buildHistoryRetentionDays: z.coerce.number().int().min(0).max(3650),
depotProjectId: z.string().default(''),
depotToken: z.string().optional().default('')
});
@@ -35,6 +36,7 @@
buildProvider,
buildsDirectory: settings.buildsDirectory,
buildTimeout: settings.buildTimeout,
+ buildHistoryRetentionDays: settings.buildHistoryRetentionDays ?? 30,
depotProjectId: settings.depotProjectId,
depotToken: ''
};
@@ -50,6 +52,7 @@
if (!updated['depotToken']) {
delete updated['depotToken'];
}
+ if (!supportsBuildHistoryRetention) delete updated['buildHistoryRetentionDays'];
await settingsService.updateSettings(updated);
},
onSuccess: () => {
@@ -61,6 +64,8 @@
successMessage: m.build_settings_saved()
});
+ // Older agents don't report the setting; hide it and leave it out of saves.
+ const supportsBuildHistoryRetention = $derived(currentSettings.buildHistoryRetentionDays !== undefined);
const existingDepotProjectId = $derived((currentSettings.depotProjectId ?? '').trim());
const existingDepotToken = $derived((currentSettings.depotToken ?? '').trim());
const depotConfigured = $derived(Boolean(currentSettings.depotConfigured));
@@ -150,6 +155,23 @@
aria-invalid={!!formInputs.buildTimeout.error}
/>
</SettingsRow>
+ {#if supportsBuildHistoryRetention}
+ <SettingsRow
+ for="build-history-retention-days"
+ label={m.build_history_retention_days()}
+ description={m.build_history_retention_days_description()}
+ helpText={m.build_history_retention_days_help()}
+ error={formInputs.buildHistoryRetentionDays.error}
+ >
+ <Input
+ id="build-history-retention-days"
+ type="number"
+ placeholder={m.build_history_retention_days_placeholder()}
+ bind:value={formInputs.buildHistoryRetentionDays.value}
+ aria-invalid={!!formInputs.buildHistoryRetentionDays.error}
+ />
+ </SettingsRow>
+ {/if}
</SettingsSection>
<SettingsSection title={m.depot()}>
diff --git a/frontend/src/routes/(app)/settings/roles/+page.ts b/frontend/src/routes/(app)/settings/roles/+page.ts
index 97d6498c6..787629650 100644
--- a/frontend/src/routes/(app)/settings/roles/+page.ts
+++ b/frontend/src/routes/(app)/settings/roles/+page.ts
@@ -1,3 +1,4 @@
+import { queryKeys } from '#lib/query/query-keys.js';
import { roleService } from '#lib/services/role-service.js';
import type { SearchPaginationSortRequest } from '#lib/types/shared.js';
import { resolveInitialTableRequest } from '#lib/utils/tables.js';
@@ -19,7 +20,7 @@ export const load: PageLoad = async ({ parent }) => {
} satisfies SearchPaginationSortRequest);
const roles = await queryClient.query({
- queryKey: ['roles', 'list', rolesRequestOptions],
+ queryKey: queryKeys.roles.list(rolesRequestOptions),
queryFn: () => roleService.getRoles(rolesRequestOptions)
});
diff --git a/frontend/src/routes/(app)/settings/roles/[id]/+page.ts b/frontend/src/routes/(app)/settings/roles/[id]/+page.ts
index d812eb117..f822c68da 100644
--- a/frontend/src/routes/(app)/settings/roles/[id]/+page.ts
+++ b/frontend/src/routes/(app)/settings/roles/[id]/+page.ts
@@ -1,5 +1,6 @@
import { error, redirect } from '@sveltejs/kit';
+import { queryKeys } from '#lib/query/query-keys.js';
import { roleService } from '#lib/services/role-service.js';
import { userIsGlobalAdmin } from '#lib/utils/auth.js';
import { tryCatch } from '#lib/utils/try-catch.js';
@@ -19,12 +20,12 @@ export const load: PageLoad = async ({ parent, params }) => {
const [role, permissionsManifest] = await Promise.all([
tryCatch(
queryClient.query({
- queryKey: ['roles', 'detail', params.id],
+ queryKey: queryKeys.roles.detail(params.id),
queryFn: () => roleService.get(params.id)
})
).then((result) => (result.error ? null : result.data)),
queryClient.query({
- queryKey: ['roles', 'permissions-manifest'],
+ queryKey: queryKeys.roles.permissionsManifest(),
queryFn: () => roleService.getPermissionsManifest()
})
]);
diff --git a/frontend/src/routes/(app)/settings/roles/new/+page.ts b/frontend/src/routes/(app)/settings/roles/new/+page.ts
index d6d86a231..b846829f3 100644
--- a/frontend/src/routes/(app)/settings/roles/new/+page.ts
+++ b/frontend/src/routes/(app)/settings/roles/new/+page.ts
@@ -1,5 +1,6 @@
import { redirect } from '@sveltejs/kit';
+import { queryKeys } from '#lib/query/query-keys.js';
import { roleService } from '#lib/services/role-service.js';
import { userIsGlobalAdmin } from '#lib/utils/auth.js';
@@ -16,7 +17,7 @@ export const load: PageLoad = async ({ parent }) => {
}
const permissionsManifest = await queryClient.query({
- queryKey: ['roles', 'permissions-manifest'],
+ queryKey: queryKeys.roles.permissionsManifest(),
queryFn: () => roleService.getPermissionsManifest()
});
diff --git a/frontend/src/routes/(app)/settings/users/+page.ts b/frontend/src/routes/(app)/settings/users/+page.ts
index 6d1fc6238..bb66ac0c7 100644
--- a/frontend/src/routes/(app)/settings/users/+page.ts
+++ b/frontend/src/routes/(app)/settings/users/+page.ts
@@ -27,12 +27,12 @@ export const load: PageLoad = async ({ parent }) => {
queryFn: () => userService.getUsers(userRequestOptions)
}),
queryClient.query({
- queryKey: ['roles', 'all'],
+ queryKey: queryKeys.roles.all(),
queryFn: () => roleService.getAll(),
staleTime: 30_000
}),
queryClient.query({
- queryKey: ['environments', 'all-for-role-assignments'],
+ queryKey: queryKeys.environments.list({ pagination: { page: 1, limit: 1000 } }),
queryFn: () => environmentManagementService.getEnvironments({ pagination: { page: 1, limit: 1000 } }),
staleTime: 30_000
})
diff --git a/frontend/src/routes/(app)/swarm/stacks/[name]/+page.svelte b/frontend/src/routes/(app)/swarm/stacks/[name]/+page.svelte
index 5f8102fca..9d507a850 100644
--- a/frontend/src/routes/(app)/swarm/stacks/[name]/+page.svelte
+++ b/frontend/src/routes/(app)/swarm/stacks/[name]/+page.svelte
@@ -3,7 +3,6 @@
import { onMount } from 'svelte';
import { toast } from 'svelte-sonner';
- import CodeEditor from '#lib/components/code-editor/editor.svelte';
import { openConfirmDialog } from '#lib/components/confirm-dialog/index.js';
import EditorTabStrip from '#lib/components/editor-tab-strip.svelte';
import ResizableSplit from '#lib/components/resizable-split.svelte';
@@ -327,33 +326,39 @@
onClose={closeSourceTab}
/>
<div class="relative min-h-0 flex-1">
- {#key activeSourceTab}
- {#if activeSourceTab === 'compose'}
- <div class="absolute inset-0 min-h-0 w-full min-w-0">
- <CodeEditor
- value={stackSource.composeContent}
- language="yaml"
- readOnly={true}
- fontSize="13px"
- fileId={`swarm-stack-source:${stackName}:compose.yaml`}
- />
- </div>
- {:else if stackSource.envContent?.trim()}
- <div class="absolute inset-0 min-h-0 w-full min-w-0">
- <CodeEditor
- value={stackSource.envContent}
- language="env"
- readOnly={true}
- fontSize="13px"
- fileId={`swarm-stack-source:${stackName}:.env`}
- />
- </div>
- {:else}
- <div class="flex h-full items-center justify-center p-6 text-center text-sm text-muted-foreground">
- No saved `.env` file was stored for this stack.
- </div>
- {/if}
- {/key}
+ {#await import('#lib/components/code-editor/editor.svelte')}
+ <p class="p-4 text-sm text-muted-foreground">{m.common_loading()}</p>
+ {:then { default: CodeEditor }}
+ {#key activeSourceTab}
+ {#if activeSourceTab === 'compose'}
+ <div class="absolute inset-0 min-h-0 w-full min-w-0">
+ <CodeEditor
+ value={stackSource.composeContent}
+ language="yaml"
+ readOnly={true}
+ fontSize="13px"
+ fileId={`swarm-stack-source:${stackName}:compose.yaml`}
+ />
+ </div>
+ {:else if stackSource.envContent?.trim()}
+ <div class="absolute inset-0 min-h-0 w-full min-w-0">
+ <CodeEditor
+ value={stackSource.envContent}
+ language="env"
+ readOnly={true}
+ fontSize="13px"
+ fileId={`swarm-stack-source:${stackName}:.env`}
+ />
+ </div>
+ {:else}
+ <div class="flex h-full items-center justify-center p-6 text-center text-sm text-muted-foreground">
+ No saved `.env` file was stored for this stack.
+ </div>
+ {/if}
+ {/key}
+ {:catch}
+ <p class="p-4 text-sm text-destructive">{m.code_editor_load_failed()}</p>
+ {/await}
</div>
</div>
{/snippet}
diff --git a/frontend/src/routes/(app)/updates/+page.svelte b/frontend/src/routes/(app)/updates/+page.svelte
index b6df21906..7b5c57dee 100644
--- a/frontend/src/routes/(app)/updates/+page.svelte
+++ b/frontend/src/routes/(app)/updates/+page.svelte
@@ -111,9 +111,11 @@
async function invalidateContainerQueries() {
await Promise.all([
...listScopes(environmentStore.shown.map((env) => env.id)).map((scope) =>
- queryClient.invalidateQueries({ queryKey: ['containers', scope], refetchType: 'none' })
+ queryClient.invalidateQueries({ queryKey: queryKeys.containers.listPrefix(scope), refetchType: 'none' })
),
- ...environmentStore.shown.map((env) => queryClient.invalidateQueries({ queryKey: ['container', env.id] }))
+ ...environmentStore.shown.map((env) =>
+ queryClient.invalidateQueries({ queryKey: queryKeys.containers.environment(env.id) })
+ )
]);
}
@@ -125,12 +127,14 @@
toast.success(m.images_update_check_completed());
await Promise.all([
...listScopes(environments.map((env) => env.id)).flatMap((scope) =>
- ['containers', 'projects', 'images'].map((resource) => queryClient.invalidateQueries({ queryKey: [resource, scope] }))
+ [queryKeys.containers, queryKeys.projects, queryKeys.images].map((keys) =>
+ queryClient.invalidateQueries({ queryKey: keys.listPrefix(scope) })
+ )
),
...environments.flatMap((env) => [
- queryClient.invalidateQueries({ queryKey: ['container', env.id] }),
+ queryClient.invalidateQueries({ queryKey: queryKeys.containers.environment(env.id) }),
queryClient.invalidateQueries({ queryKey: queryKeys.projects.environment(env.id) }),
- queryClient.invalidateQueries({ queryKey: ['image', env.id] })
+ queryClient.invalidateQueries({ queryKey: queryKeys.images.environment(env.id) })
])
]);
},
diff --git a/frontend/src/routes/+layout.ts b/frontend/src/routes/+layout.ts
index 8453f5825..3ecbf8bc3 100644
--- a/frontend/src/routes/+layout.ts
+++ b/frontend/src/routes/+layout.ts
@@ -18,6 +18,8 @@ import { featureStore } from '#lib/stores/features.store.svelte.js';
import userStore from '#lib/stores/user-store.svelte.js';
import { versionStore } from '#lib/stores/version.store.svelte.js';
import type { PermissionsManifest, User } from '#lib/types/auth.js';
+import type { Environment } from '#lib/types/environment.js';
+import type { AppVersionInformation, Settings } from '#lib/types/settings.js';
import { isAuthRejectionError } from '#lib/utils/api.js';
import { getAuthRedirectPath, shownEnvironmentForRoute, userHasPermission } from '#lib/utils/auth.js';
import { getEffectiveLandingPage } from '#lib/utils/navigation.js';
@@ -49,6 +51,15 @@ const queryClient = new QueryClient({
let authenticatedUserId: string | null | undefined;
// Whether a signed-in page has loaded; later loads without a navigation to their path are preloads.
let opened = false;
+let telemetryAwaited = false;
+
+// Navigations reuse root data for this long; a refresh of the page on screen always refetches.
+const ROOT_STALE_TIME = 30_000;
+// Query results last pushed into the stores, so a cached repeat never overwrites newer local or streamed state.
+let appliedUser: User | undefined;
+let appliedEnvironments: Environment[] | undefined;
+let appliedSettings: Settings | undefined;
+let appliedVersion: AppVersionInformation | undefined;
function dataOrNull<T>(promise: Promise<T>): Promise<T | null> {
return tryCatch(promise).then((result) => (result.error ? null : result.data));
@@ -79,13 +90,24 @@ async function loadRoot({ url }: Parameters<LayoutLoad>[0], releaseSelection: ((
};
}
+ // Explicit refreshes (refreshAll, invalidateAll) re-run this load for the page on screen; navigations change the URL.
+ const rootQuery = { staleTime: url.href === page.url.href ? 0 : ROOT_STALE_TIME, retry: false };
let [user, versionInformation] = await Promise.all([
// Only an explicit rejection signs the user out; transient check failures keep the known user.
- tryCatch(userService.getCurrentUser()).then((result): User | null => {
+ tryCatch(
+ queryClient.query({ ...rootQuery, queryKey: queryKeys.auth.currentUser(), queryFn: () => userService.getCurrentUser() })
+ ).then((result): User | null => {
if (!result.error) return result.data;
- return isAuthRejectionError(result.error) || isSessionCancelledError(result.error) ? null : userStore.current;
+ if (!isAuthRejectionError(result.error) && !isSessionCancelledError(result.error)) return userStore.current;
+ // A rejected session must not be served from the cache by the next load.
+ queryClient.removeQueries({ queryKey: queryKeys.auth.currentUser() });
+ return null;
}),
- versionService.getVersionInformation()
+ queryClient.query({
+ ...rootQuery,
+ queryKey: queryKeys.system.appVersion(),
+ queryFn: () => versionService.getVersionInformation()
+ })
]);
const autoLoginConfig =
browser && versionInformation.enabledFeatures?.includes('autologin')
@@ -108,11 +130,12 @@ async function loadRoot({ url }: Parameters<LayoutLoad>[0], releaseSelection: ((
authService.resetAuthenticatedState(queryClient, { restartMountedStores: user !== null });
}
authenticatedUserId = nextAuthenticatedUserId;
- if (user) {
- await userStore.setUser(user);
- } else {
+ if (!user) {
userStore.clearUser();
+ } else if (user !== appliedUser || !userStore.current) {
+ await userStore.setUser(user);
}
+ appliedUser = user ?? undefined;
let settings = null;
let swarmEnabled: boolean | undefined;
@@ -122,11 +145,26 @@ async function loadRoot({ url }: Parameters<LayoutLoad>[0], releaseSelection: ((
const opening = !opened;
opened = true;
// Initialize environment store (required for settings service)
- const environmentsRequest = tryCatch(environmentManagementService.getEnvironments({ pagination: { page: 1, limit: -1 } }));
- const permissionsManifestRequest = dataOrNull(roleService.getPermissionsManifest());
+ const environmentListOptions = { pagination: { page: 1, limit: -1 } };
+ const environmentsRequest = tryCatch(
+ queryClient.query({
+ ...rootQuery,
+ queryKey: queryKeys.environments.list(environmentListOptions),
+ queryFn: () => environmentManagementService.getEnvironments(environmentListOptions)
+ })
+ );
+ const permissionsManifestRequest = dataOrNull(
+ queryClient.query({
+ ...rootQuery,
+ queryKey: queryKeys.roles.permissionsManifest(),
+ queryFn: () => roleService.getPermissionsManifest()
+ })
+ );
const environments = await environmentsRequest;
if (!environments.error) {
- await environmentStore.initialize(environments.data.data);
+ // The live environment stream keeps the store newer than a cached list.
+ if (environments.data.data !== appliedEnvironments) await environmentStore.initialize(environments.data.data);
+ appliedEnvironments = environments.data.data;
} else if (!environmentStore.isInitialized()) {
// A failed refetch on an already-initialised store keeps the current
// selection; re-initialising with [] would drop it to "No Environment".
@@ -150,12 +188,30 @@ async function loadRoot({ url }: Parameters<LayoutLoad>[0], releaseSelection: ((
}
releaseSelection?.();
- const settingsRequest = (
- userHasPermission(user, 'settings:read') ? dataOrNull(settingsService.getSettings()) : Promise.resolve(null)
- ).then((loaded) => loaded ?? dataOrNull(settingsService.getPublicSettings(environmentStore.selected?.id ?? '0')));
featureStore.connect(queryClient);
const currentEnvironmentId = await environmentStore.getCurrentEnvironmentId();
- const featuresRequest = featureStore.refresh(currentEnvironmentId);
+ const settingsRequest = (
+ userHasPermission(user, 'settings:read')
+ ? dataOrNull(
+ queryClient.query({
+ ...rootQuery,
+ queryKey: queryKeys.settings.byEnvironment(currentEnvironmentId),
+ queryFn: () => settingsService.getSettingsForEnvironmentMerged(currentEnvironmentId)
+ })
+ )
+ : Promise.resolve(null)
+ ).then(
+ (loaded) =>
+ loaded ??
+ dataOrNull(
+ queryClient.query({
+ ...rootQuery,
+ queryKey: queryKeys.settings.public(currentEnvironmentId),
+ queryFn: () => settingsService.getPublicSettings(currentEnvironmentId)
+ })
+ )
+ );
+ const featuresRequest = featureStore.load(currentEnvironmentId, rootQuery.staleTime === 0);
// The all environments preview shows navigation for features in any shown environment. The others load
// in the background, so an unreachable environment never stalls navigation.
for (const env of environmentStore.shown) {
@@ -163,7 +219,13 @@ async function loadRoot({ url }: Parameters<LayoutLoad>[0], releaseSelection: ((
}
// Optional discovery: skip it without swarm:read and never toast a denial.
const swarmStatusRequest = userHasPermission(user, 'swarm:read', currentEnvironmentId)
- ? dataOrNull(swarmService.getSwarmStatus(currentEnvironmentId, { suppressAccessDeniedToast: true }))
+ ? dataOrNull(
+ queryClient.query({
+ ...rootQuery,
+ queryKey: queryKeys.swarm.status(currentEnvironmentId),
+ queryFn: () => swarmService.getSwarmStatus(currentEnvironmentId, { suppressAccessDeniedToast: true })
+ })
+ )
: Promise.resolve(null);
const [loadedSettings, loadedSwarmStatus, loadedPermissionsManifest] = await Promise.all([
settingsRequest,
@@ -171,7 +233,15 @@ async function loadRoot({ url }: Parameters<LayoutLoad>[0], releaseSelection: ((
permissionsManifestRequest
]);
// Keep recovery/settings pages reachable while a selected agent is offline.
- settings = loadedSettings ?? (await dataOrNull(settingsService.getPublicSettings()));
+ settings =
+ loadedSettings ??
+ (await dataOrNull(
+ queryClient.query({
+ ...rootQuery,
+ queryKey: queryKeys.settings.public('0'),
+ queryFn: () => settingsService.getPublicSettings()
+ })
+ ));
await featuresRequest;
swarmEnabled = loadedSwarmStatus?.enabled;
permissionsManifest = loadedPermissionsManifest;
@@ -181,26 +251,40 @@ async function loadRoot({ url }: Parameters<LayoutLoad>[0], releaseSelection: ((
await environmentStore.initialize([]);
// Try to fetch public settings for login page configuration
- settings = await dataOrNull(settingsService.getPublicSettings());
+ settings = await dataOrNull(
+ queryClient.query({
+ ...rootQuery,
+ queryKey: queryKeys.settings.public('0'),
+ queryFn: () => settingsService.getPublicSettings()
+ })
+ );
}
- if (settings) {
+ // Settings saves and the version stream update the stores directly, so only new results replace them.
+ if (settings && settings !== appliedSettings) {
settingsStore.set(settings);
+ appliedSettings = settings;
+ }
+ if (versionInformation !== appliedVersion) {
+ versionStore.seed(versionInformation);
+ appliedVersion = versionInformation;
}
- versionStore.seed(versionInformation);
+ const telemetry = setTelemetry(
+ {
+ traces: settings?.frontendTracingEnabled ?? false,
+ metrics: settings?.frontendMetricsEnabled ?? false,
+ logs: settings?.frontendLogsEnabled ?? false
+ },
+ versionInformation.currentVersion
+ );
// Waiting lets the first page view be traced, but a stalled SDK download must never hold up the app.
- await Promise.race([
- setTelemetry(
- {
- traces: settings?.frontendTracingEnabled ?? false,
- metrics: settings?.frontendMetricsEnabled ?? false,
- logs: settings?.frontendLogsEnabled ?? false
- },
- versionInformation.currentVersion
- ),
- new Promise((resolve) => setTimeout(resolve, 2_000))
- ]);
+ if (!telemetryAwaited) {
+ telemetryAwaited = true;
+ let timeout: ReturnType<typeof setTimeout> | undefined;
+ await Promise.race([telemetry, new Promise((resolve) => (timeout = setTimeout(resolve, 2_000)))]);
+ clearTimeout(timeout);
+ }
const redirectPath = getAuthRedirectPath(
url.pathname,
@@ -215,7 +299,8 @@ async function loadRoot({ url }: Parameters<LayoutLoad>[0], releaseSelection: ((
}
return {
- user,
+ // The store holds local account edits made since the cached user was fetched.
+ user: user && userStore.current,
settings,
permissionsManifest,
permissionsManifestLoadFailed,
diff --git a/frontend/src/routes/layout.css b/frontend/src/routes/layout.css
index 9c92797b7..33686ee92 100644
--- a/frontend/src/routes/layout.css
+++ b/frontend/src/routes/layout.css
@@ -468,9 +468,7 @@ html.reduce-effects *::after {
}
.hover-lift {
- transition:
- transform 160ms ease,
- box-shadow 160ms ease;
+ transition: transform 160ms ease;
}
.hover-lift:hover {
diff --git a/tests/spec/security.spec.ts b/tests/spec/security.spec.ts
index ae1189dc4..0196164ac 100644
--- a/tests/spec/security.spec.ts
+++ b/tests/spec/security.spec.ts
@@ -151,7 +151,7 @@ test.describe('Security Page', () => {
const scanRequests: string[] = [];
await page.route(/\/api\/environments\/0\/images(?:\?.*)?$/, async (route) => {
const requestUrl = new URL(route.request().url());
- expect(requestUrl.searchParams.get('limit')).toBe('1000');
+ expect(requestUrl.searchParams.get('limit')).toBe('100');
await route.fulfill({
json: paginated([
{ id: 'scan-success', repoTags: ['example/success:latest'] },
diff --git a/types/settings/settings.go b/types/settings/settings.go
index 3f8d4ab4a..1ead8d8e6 100644
--- a/types/settings/settings.go
+++ b/types/settings/settings.go
@@ -560,6 +560,11 @@ type Update struct {
// Required: false
BuildTimeout *string `json:"buildTimeout,omitzero"`
+ // BuildHistoryRetentionDays is the number of days of completed and failed image build history to retain.
+ //
+ // Required: false
+ BuildHistoryRetentionDays *string `json:"buildHistoryRetentionDays,omitzero"`
+
// DepotProjectId is the Depot project identifier.
//
// Required: false