feat: add durable image processing pipeline

This commit is contained in:
2026-10-22 17:22:28 +03:00
parent 8c2ac690a6
commit 42336ce7c2
39 changed files with 4379 additions and 126 deletions
+26 -24
View File
@@ -27,8 +27,8 @@ func TestUploadLifecycle(t *testing.T) {
result, err := service.Upload(context.Background(), upload(data, "image/png"))
require.NoError(t, err)
require.Equal(t, model.AttachmentReady, result.Status)
require.Equal(t, []string{"pending", "put", "ready"}, events)
require.Equal(t, model.AttachmentQueued, result.Status)
require.Equal(t, []string{"pending", "put", "queued"}, events)
}
func TestUploadValidation(t *testing.T) {
@@ -86,7 +86,7 @@ func TestNewServiceRejectsDecodeBudgetOverflow(t *testing.T) {
slog.New(slog.NewTextHandler(io.Discard, nil)), &repositoryStub{}, &objectStub{},
Config{
MaxBytes: 1024, MaxPixels: math.MaxInt64/8 + 1, BodyMemoryBytes: 1024,
DecodeMemoryBytes: math.MaxInt64, CleanupTimeout: time.Second,
DecodeMemoryBytes: math.MaxInt64, CleanupTimeout: time.Second, PipelineVersion: 1,
},
)
require.Error(t, err)
@@ -103,16 +103,16 @@ func TestUploadCleansPendingMetadataWhenObjectWriteFails(t *testing.T) {
require.True(t, repository.pendingDeleted)
}
func TestUploadReturnsCommittedMetadataAfterAmbiguousReadyUpdate(t *testing.T) {
func TestUploadKeepsStagingAfterQueueFailure(t *testing.T) {
data := pngFixture(t, 2, 2)
repository := &repositoryStub{markErr: errors.New("connection lost"), lookupReady: true}
repository := &repositoryStub{queueErr: errors.New("connection lost")}
objects := &objectStub{}
service := newTestService(t, repository, objects, 1024, 4)
result, err := service.Upload(context.Background(), upload(data, "image/png"))
require.NoError(t, err)
require.Equal(t, model.AttachmentReady, result.Status)
_, err := service.Upload(context.Background(), upload(data, "image/png"))
require.ErrorIs(t, err, model.ErrUnavailable)
require.False(t, objects.deleted)
require.False(t, repository.pendingDeleted)
}
func newTestService(t *testing.T, repository Repository, objects ObjectStore, maxBytes, maxPixels int64) *Service {
@@ -121,7 +121,7 @@ func newTestService(t *testing.T, repository Repository, objects ObjectStore, ma
slog.New(slog.NewTextHandler(io.Discard, nil)), repository, objects,
Config{
MaxBytes: maxBytes, MaxPixels: maxPixels, BodyMemoryBytes: maxBytes,
DecodeMemoryBytes: maxPixels * 8, CleanupTimeout: time.Second,
DecodeMemoryBytes: maxPixels * 8, CleanupTimeout: time.Second, PipelineVersion: 1,
},
)
require.NoError(t, err)
@@ -137,8 +137,7 @@ func upload(data []byte, contentType string) Upload {
type repositoryStub struct {
events *[]string
markErr error
lookupReady bool
queueErr error
pendingDeleted bool
pending model.Attachment
}
@@ -154,12 +153,19 @@ func (s *repositoryStub) CreatePendingAttachment(_ context.Context, attachment m
return attachment, nil
}
func (s *repositoryStub) MarkAttachmentReady(context.Context, string, string) (model.Attachment, error) {
if s.markErr != nil {
return model.Attachment{}, s.markErr
func (s *repositoryStub) QueueAttachmentWithJobAndOutbox(
context.Context,
string,
string,
model.AttachmentJob,
string,
string,
) (model.Attachment, error) {
if s.queueErr != nil {
return model.Attachment{}, s.queueErr
}
s.pending.Status = model.AttachmentReady
s.record("ready")
s.pending.Status = model.AttachmentQueued
s.record("queued")
return s.pending, nil
}
@@ -168,18 +174,14 @@ func (s *repositoryStub) DeletePendingAttachment(context.Context, string, string
return nil
}
func (s *repositoryStub) AttachmentByStorage(context.Context, string, string) (model.Attachment, error) {
if s.lookupReady {
s.pending.Status = model.AttachmentReady
return s.pending, nil
}
return model.Attachment{}, model.ErrNotFound
}
func (s *repositoryStub) AttachmentForUser(context.Context, string, string) (model.Attachment, error) {
return s.pending, nil
}
func (s *repositoryStub) AttachmentVariantForUser(context.Context, string, string, string) (model.Attachment, model.Variant, error) {
return s.pending, model.Variant{}, nil
}
func (s *repositoryStub) record(event string) {
if s.events != nil {
*s.events = append(*s.events, event)