Skip to content

fix(classes): defer superseded image cleanup until after commit with retry recovery - #213

Merged
theosiemensrhodes merged 5 commits into
mainfrom
fix/neuron-n10-image-cleanup
Nov 26, 2026
Merged

theosiemensrhodes merged 5 commits into
mainfrom
fix/neuron-n10-image-cleanup

Conversation

@theosiemensrhodes

@theosiemensrhodes theosiemensrhodes commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

ClassService.updateClass previously called imageService.deleteImage(oldImageKey) inside the Postgres transaction before later schedule/recurrence validation and writes, and triggered deletion whenever both originalClass.image and dbClassUpdate.image were truthy (including same-key updates). If later validation failed and rolled back, the database restored the image key while the MinIO object was already gone; errors during deletion were swallowed with no retry recovery, and clearing an image to null or deleting a class did not clean up superseded objects.

Change

  • Enqueue superseded class image cleanup (jobs.delete-image) inside the Postgres transaction via JobService.run(..., { transaction: tx }) in ClassService.updateClass and ClassService.deleteClass, locking the course row (FOR UPDATE) and only enqueueing when originalClass.image is non-empty and differs from the committed updatedImage (preserving unchanged/same-key updates and handling null clears and class deletions).
  • Register jobs.delete-image with exponential backoff retries (retryLimit: 8, retryDelay: 60, retryBackoff: true); before calling imageService.deleteImage, the handler validates the payload type/non-blankness without mutating the key and verifies in a single Postgres EXISTS query that neither course.image nor user.image still references the exact stored key.
  • Fix ImageService bucket initialization (ensureBucketReady) so transient MinIO startup failures clear the cached promise and recover on retry, and apply the bucket read policy idempotently so partial failures between makeBucket and setBucketPolicy also heal on retry.
  • Scope note: cascade-deleted course images from TermService.deleteTerm and bucket-wide orphan sweeping (jobs.cleanup-orphaned-images) are intentionally out of scope for this PR.

Verification

  • pnpm exec prettier --check on all 6 touched files: passed
  • SKIP_ENV_VALIDATION=1 pnpm exec next typegen && pnpm typecheck: passed (0 errors)
  • pnpm vitest run src/test/integration/class-service.test.ts src/test/unit/ against a disposable database (neuron_test_n10_image_cleanup, dropped after run): 9/9 test files, 77/77 tests passed (including an end-to-end ClassService -> real JobService/pg-boss -> deleteImageJob commit & rollback test and ImageService bucket-readiness recovery tests).
  • eslint currently fails repo-wide on main due to a pre-existing eslint-config-next / @eslint/eslintrc flat-config circular JSON error.

@theosiemensrhodes
theosiemensrhodes marked this pull request as draft November 23, 2026 06:30
@theosiemensrhodes
theosiemensrhodes force-pushed the fix/neuron-n10-image-cleanup branch 2 times, most recently from ccf3187 to e6c3a74 Compare November 25, 2026 06:28
@theosiemensrhodes
theosiemensrhodes force-pushed the fix/neuron-n10-image-cleanup branch from e6c3a74 to 1424fb3 Compare November 25, 2026 06:35
@theosiemensrhodes
theosiemensrhodes marked this pull request as ready for review November 26, 2026 02:09
Signed-off-by: Theo Siemens-Rhodes <112789743+theosiemensrhodes@users.noreply.github.com>
@theosiemensrhodes
theosiemensrhodes merged commit 9e6f05f into main Nov 26, 2026
5 checks passed
@theosiemensrhodes
theosiemensrhodes deleted the fix/neuron-n10-image-cleanup branch November 26, 2026 02:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant