refactor(service): RecomputeWorkspaceSize takes ID, writes only size_bytes#353
Open
Adam-D-Lewis wants to merge 2 commits into
Open
refactor(service): RecomputeWorkspaceSize takes ID, writes only size_bytes#353Adam-D-Lewis wants to merge 2 commits into
Adam-D-Lewis wants to merge 2 commits into
Conversation
db.Save(ws) writes every column from the in-memory struct, so any field that diverged from the DB between job-start load and this call gets clobbered. #294 was caused by exactly this pattern overwriting the path column with "" — fixed in #306 with an in-memory sync, but the underlying footgun (full-row save of a stale struct) remained. Switch to a column-scoped UPDATE on size_bytes so the function only writes the column it owns, and check the error (previously discarded). Set ws.SizeBytes in-memory only after the write succeeds.
✅ Deploy Preview for nebi-docs canceled.
|
Match the SetWorkspace{Status,Path} shape: take wsID instead of a
*models.Workspace, load fresh inside the function, and return an error.
The function is fundamentally a read-compute-write (it measures the
on-disk directory then writes one column), so an internal fresh load
makes it self-contained — callers can no longer pass a stale struct
that ends up clobbering anything.
Renamed from UpdateWorkspaceSize because "Update" reads like a pure
setter; "Recompute" better describes that the function measures the
size and persists the result.
All 5 worker callsites now log warnings on failure, matching the
existing pattern around SyncPackagesFromWorkspace / CreateVersionSnapshot.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up to #306. That PR fixed the symptom of #294 (workspace
pathclobbered after creation) by syncing the in-memory struct beforedb.Save(ws)runs. This PR removes the underlying footgun so the class of bug can't recur for any other column.Two changes
1. Column-scoped write.
UpdateWorkspaceSizepreviously calleddb.Save(ws)— a full-row write driven by the in-memory struct. If anything else updated the row in the DB betweenwsbeing loaded at job start and this call (e.g.SetWorkspacePath, a future targeted update on another column, a concurrent admin write), the stale value onwsoverwrote it. Switched todb.Model(...).Where("id = ?", wsID).Update("size_bytes", sizeBytes)so the function writes only the column it owns.2. Signature + rename to match the rest of the file.
SetWorkspaceStatus(wsID, status)andSetWorkspacePath(wsID, path)take an ID and return an error. The oldUpdateWorkspaceSize(ws *Workspace)took a full struct and silently dropped errors. NowRecomputeWorkspaceSize(wsID uuid.UUID) error:The five worker callsites now log a warning on failure, matching the existing pattern around
SyncPackagesFromWorkspaceandCreateVersionSnapshot.Impact on #306
The in-memory
ws.Path = resolvedPathline added there becomes defense-in-depth rather than load-bearing — the targeted update meansRecomputeWorkspaceSizecannot clobberpath(or any other column) even with a stale struct. Worth keeping for in-memory/DB consistency, but no longer the only thing preventing the regression.How to test
go test ./internal/service/... ./internal/worker/...— all existing tests pass, including the #294 regression test added in #306.go build ./...clean.