Render photo thumbnails in the background
C# · ASP.NET Core · advanced · modification
Moves thumbnail rendering out of the upload request. `ThumbnailQueue` (new) is a bounded in-memory channel of photo ids with room for 100, and `ThumbnailWorker` (new) is a hosted `BackgroundService` that reads it and renders each photo in its own DI scope, logging and skipping a photo that fails. `PhotosController.Upload` saves the photo as before, then queues its id and answers `201`; the controller no longer takes the renderer or a logger. `Program.cs` registers the queue as a singleton and the worker as a hosted service. `ThumbnailWorkerTests.cs` (new) checks that the upload answers before the thumbnail is rendered and that a failed thumbnail does not stop the next one.
Snapshots is a family photo album app. ASP.NET Core 10 controllers on .NET 10 (C# 14, nullable reference types enabled, implicit usings), EF Core 10 with Npgsql, xUnit tests. Existing and unchanged: `AddSnapshotsAuth` (cookie sign-in through the OIDC provider; `User.GetUserId()` reads the `sub` claim), `AddSnapshotsData` (registers the scoped `SnapshotsDbContext` on Npgsql) and `AddPhotoStorage` (registers `IPhotoStorage` as a singleton over the S3 bucket and `IThumbnailRenderer` as scoped). `IThumbnailRenderer.RenderAsync(photo, ct)` reads the original from storage, writes a 320-pixel JPEG to `thumbnails/{photo.Id}.jpg` (overwriting an earlier one), returns that key and honours the token; it throws on a corrupt image or a storage error, and it finishes or throws within 10 seconds (usually about 150 ms). `Photo` has `int Id`, `AlbumId`, `UploaderId`, `OriginalKey`, a nullable `ThumbnailKey` and `UploadedAt`; `PhotoDto.From` gives `thumbnailUrl: null` when `ThumbnailKey` is null. The photo menu's Regenerate thumbnail action (`POST /api/photos/{id}/thumbnail` in `PhotoActionsController`) renders inline and saves `ThumbnailKey`. Deleting a photo removes its row, and a nightly storage cleanup deletes objects that no photo row references. The API runs as two instances behind a reverse proxy that times requests out after 60 seconds; each instance has its own queue and renders the uploads it accepted. The generic host runs with its defaults (an exception that escapes a `BackgroundService` stops the host; a 30-second shutdown timeout). `SnapshotsApiFactory` (existing test host, unchanged) runs the app on a throwaway SQLite file and takes service overrides.
Requirements
- Uploading a photo (`POST /api/albums/{albumId}/photos`) no longer renders its thumbnail inside the request. The upload stores the original and the photo row as before and answers `201` with `thumbnailUrl: null`; the API process that accepted the upload then renders the thumbnail in the background. Until it is ready the web app shows its existing placeholder (it already does for a photo with no thumbnail) and picks the thumbnail up on the album's next refresh.
- Thumbnails are rendered one at a time, in the order the uploads were queued, each with the existing `IThumbnailRenderer`, which is registered as scoped like the `SnapshotsDbContext` the thumbnail key is saved with.
- At most 100 photos wait for a thumbnail in memory. When 100 are already waiting, an upload waits for room before it answers. If the upload request is aborted before its photo is queued (for instance while it waits: the client disconnects, or the proxy's 60-second timeout), the photo stays saved without a thumbnail, as a photo whose inline render failed did before.
- A failure for one photo (a corrupt image, a storage or database error, the photo deleted while its thumbnail is rendered) is logged as an error with the photo id and skips only that photo, which keeps the placeholder; the next photos are still rendered. A photo that is deleted, or given a thumbnail by the existing Regenerate thumbnail action, before its turn comes is skipped without rendering.
- When the API shuts down (a deploy, a restart), the render in progress is cancelled and the process stops without draining the queue; the photos not rendered yet keep the placeholder. Re-queuing them after a restart is out of scope: the photo menu's existing Regenerate thumbnail action renders any photo's thumbnail. Unchanged: only album members can list or upload (others get 404), and the existing upload checks stay as they are: the upload request is at most 20,000,000 bytes in all (`[RequestSizeLimit]`), and the file's content type is exactly `image/jpeg` or `image/png`, which is what the web app's uploader sends.
Files touched
- src/Snapshots.Api/Program.cs
- src/Snapshots.Api/Photos/PhotosController.cs
- src/Snapshots.Api/Thumbnails/ThumbnailQueue.cs
- src/Snapshots.Api/Thumbnails/ThumbnailWorker.cs
- tests/Snapshots.Api.Tests/ThumbnailWorkerTests.cs