forked from mtusi2026/mtusi2026lab5
feat: add durable image processing pipeline
This commit is contained in:
@@ -2,6 +2,7 @@ package postgres_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"strings"
|
||||
@@ -159,6 +160,165 @@ func (s *PostgresSuite) TestAttachmentAccessAndMessageLinking() {
|
||||
s.False(deleted)
|
||||
}
|
||||
|
||||
func (s *PostgresSuite) TestAttachmentJobLifecycleAndOutboxFencing() {
|
||||
owner, chatID := createOwnerAndChat(s.T(), s.store)
|
||||
attachment, job := createQueuedAttachment(s.T(), s.store, owner.ID, chatID)
|
||||
s.Equal(model.AttachmentQueued, attachment.Status)
|
||||
|
||||
events, err := s.store.ClaimOutboxEvents(context.Background(), 10, time.Minute)
|
||||
s.Require().NoError(err)
|
||||
s.Require().NotEmpty(events)
|
||||
var event model.OutboxEvent
|
||||
for _, candidate := range events {
|
||||
if candidate.JobID == job.ID {
|
||||
event = candidate
|
||||
break
|
||||
}
|
||||
}
|
||||
s.Require().NotEmpty(event.ID)
|
||||
var message model.AttachmentJobMessage
|
||||
s.Require().NoError(json.Unmarshal(event.Payload, &message))
|
||||
s.Equal(job.ID, message.JobID)
|
||||
s.Equal(attachment.ID, message.AttachmentID)
|
||||
s.Equal(1, message.Attempt)
|
||||
published, err := s.store.MarkOutboxPublished(context.Background(), event.ID, event.LeaseToken)
|
||||
s.Require().NoError(err)
|
||||
s.True(published)
|
||||
|
||||
_, replay, err := s.store.CreateMessage(
|
||||
context.Background(), chatID, owner.ID, "queued attachment",
|
||||
[]string{attachment.ID}, uuid.NewString(), 24*time.Hour,
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
s.False(replay)
|
||||
|
||||
_, err = s.store.ClaimAttachmentJob(
|
||||
context.Background(), job.ID, uuid.NewString(), job.PipelineVersion, job.Attempt, "worker-invalid", time.Minute,
|
||||
)
|
||||
s.ErrorIs(err, model.ErrInvalidInput)
|
||||
|
||||
lease, err := s.store.ClaimAttachmentJob(
|
||||
context.Background(), job.ID, attachment.ID, job.PipelineVersion, job.Attempt, "worker-1", time.Minute,
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(model.AttachmentProcessing, lease.Attachment.Status)
|
||||
s.NotEmpty(lease.Attachment.StagingKey)
|
||||
|
||||
variants := []model.Variant{
|
||||
{
|
||||
Name: "original", ContentType: "image/png", SizeBytes: 128,
|
||||
SHA256: strings.Repeat("a", 64), Width: 320, Height: 200,
|
||||
ObjectKey: "attachments/" + attachment.ID + "/v1/original.png",
|
||||
},
|
||||
{
|
||||
Name: "thumbnail", ContentType: "image/jpeg", SizeBytes: 64,
|
||||
SHA256: strings.Repeat("b", 64), Width: 256, Height: 160,
|
||||
ObjectKey: "attachments/" + attachment.ID + "/v1/thumbnail.jpg",
|
||||
},
|
||||
}
|
||||
result, err := s.store.CompleteAttachmentJob(context.Background(), lease, variants)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(model.JobMutationApplied, result)
|
||||
result, err = s.store.CompleteAttachmentJob(context.Background(), lease, variants)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(model.JobMutationAlreadyComplete, result)
|
||||
|
||||
metadata, variant, err := s.store.AttachmentVariantForUser(
|
||||
context.Background(), attachment.ID, owner.ID, "thumbnail",
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(model.AttachmentReady, metadata.Status)
|
||||
s.Len(metadata.Variants, 1)
|
||||
s.Equal(variants[1].ObjectKey, variant.ObjectKey)
|
||||
|
||||
v2 := model.AttachmentJob{ID: uuid.NewString(), PipelineVersion: 2}
|
||||
queuedV2, err := s.store.QueueAttachmentPipelineVersion(
|
||||
context.Background(), attachment.ID, v2, "", "",
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(model.AttachmentQueued, queuedV2.Status)
|
||||
s.Equal(2, queuedV2.PipelineVersion)
|
||||
result, err = s.store.CompleteAttachmentJob(context.Background(), lease, variants)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(model.JobMutationStale, result)
|
||||
}
|
||||
|
||||
func (s *PostgresSuite) TestFailedAttachmentReplayIsAudited() {
|
||||
owner, chatID := createOwnerAndChat(s.T(), s.store)
|
||||
attachment, job := createQueuedAttachment(s.T(), s.store, owner.ID, chatID)
|
||||
lease, err := s.store.ClaimAttachmentJob(
|
||||
context.Background(), job.ID, attachment.ID, job.PipelineVersion, job.Attempt, "worker-1", time.Minute,
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
result, err := s.store.FailAttachmentJob(context.Background(), lease, "invalid_image")
|
||||
s.Require().NoError(err)
|
||||
s.Equal(model.JobMutationApplied, result)
|
||||
|
||||
_, _, err = s.store.CreateMessage(
|
||||
context.Background(), chatID, owner.ID, "failed attachment",
|
||||
[]string{attachment.ID}, uuid.NewString(), 24*time.Hour,
|
||||
)
|
||||
s.ErrorIs(err, postgres.ErrConflict)
|
||||
metadata, err := s.store.AttachmentForUser(context.Background(), attachment.ID, owner.ID)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(model.AttachmentFailed, metadata.Status)
|
||||
s.Require().NotNil(metadata.ErrorCode)
|
||||
s.Equal("invalid_image", *metadata.ErrorCode)
|
||||
|
||||
replayed, err := s.store.ReplayFailedAttachment(
|
||||
context.Background(), attachment.ID, "operator@example", "input repaired",
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(2, replayed.Attempt)
|
||||
s.Equal(model.AttachmentJobQueued, replayed.Status)
|
||||
|
||||
var auditCount int
|
||||
err = s.pool.QueryRow(
|
||||
context.Background(), `SELECT count(*) FROM attachment_replay_audit WHERE job_id = $1`, job.ID,
|
||||
).Scan(&auditCount)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(1, auditCount)
|
||||
}
|
||||
|
||||
func (s *PostgresSuite) TestExpiredLeaseRecoveryFencesOldWorker() {
|
||||
owner, chatID := createOwnerAndChat(s.T(), s.store)
|
||||
attachment, job := createQueuedAttachment(s.T(), s.store, owner.ID, chatID)
|
||||
lease, err := s.store.ClaimAttachmentJob(
|
||||
context.Background(), job.ID, attachment.ID, job.PipelineVersion, job.Attempt, "worker-1", time.Minute,
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
_, err = s.pool.Exec(
|
||||
context.Background(),
|
||||
`UPDATE attachment_jobs SET lease_until = clock_timestamp() - INTERVAL '1 second' WHERE id = $1`,
|
||||
job.ID,
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
|
||||
recovered, err := s.store.RecoverExpiredAttachmentJobs(context.Background(), 10, 5, time.Now())
|
||||
s.Require().NoError(err)
|
||||
s.Equal(1, recovered.Requeued)
|
||||
s.Zero(recovered.Failed)
|
||||
current, err := s.store.AttachmentJob(context.Background(), job.ID)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(2, current.Attempt)
|
||||
s.Equal(model.AttachmentJobQueued, current.Status)
|
||||
|
||||
result, err := s.store.CompleteAttachmentJob(context.Background(), lease, []model.Variant{
|
||||
{
|
||||
Name: "original", ContentType: "image/png", SizeBytes: 128,
|
||||
SHA256: strings.Repeat("a", 64), Width: 320, Height: 200,
|
||||
ObjectKey: "attachments/" + attachment.ID + "/v1/original.png",
|
||||
},
|
||||
{
|
||||
Name: "thumbnail", ContentType: "image/jpeg", SizeBytes: 64,
|
||||
SHA256: strings.Repeat("b", 64), Width: 256, Height: 160,
|
||||
ObjectKey: "attachments/" + attachment.ID + "/v1/thumbnail.jpg",
|
||||
},
|
||||
})
|
||||
s.Require().NoError(err)
|
||||
s.Equal(model.JobMutationStale, result)
|
||||
}
|
||||
|
||||
func (s *PostgresSuite) TestAttachmentDeletionClaimChecksLifecycleUnderLock() {
|
||||
owner, chatID := createOwnerAndChat(s.T(), s.store)
|
||||
pending, err := s.store.CreatePendingAttachment(context.Background(), model.Attachment{
|
||||
@@ -344,6 +504,35 @@ func createOwnerAndChat(t *testing.T, store *postgres.Store) (model.User, string
|
||||
return owner, chat.ID
|
||||
}
|
||||
|
||||
func createQueuedAttachment(
|
||||
t *testing.T,
|
||||
store *postgres.Store,
|
||||
ownerID string,
|
||||
chatID string,
|
||||
) (model.Attachment, model.AttachmentJob) {
|
||||
t.Helper()
|
||||
attachmentID := uuid.NewString()
|
||||
stagingKey := "staging/" + attachmentID + "/original"
|
||||
pending, err := store.CreatePendingAttachment(context.Background(), model.Attachment{
|
||||
ID: attachmentID, ChatID: chatID, OwnerID: ownerID, Filename: "photo.png",
|
||||
ContentType: "image/png", SizeBytes: 128, SHA256: strings.Repeat("a", 64),
|
||||
ObjectKey: stagingKey,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CreatePendingAttachment() error = %v", err)
|
||||
}
|
||||
job := model.AttachmentJob{
|
||||
ID: uuid.NewString(), AttachmentID: attachmentID, PipelineVersion: 1, Attempt: 1,
|
||||
}
|
||||
queued, err := store.QueueAttachmentWithJobAndOutbox(
|
||||
context.Background(), pending.ID, stagingKey, job, "", "",
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("QueueAttachmentWithJobAndOutbox() error = %v", err)
|
||||
}
|
||||
return queued, job
|
||||
}
|
||||
|
||||
func messageCount(t *testing.T, pool *pgxpool.Pool, chatID string) int {
|
||||
t.Helper()
|
||||
var count int
|
||||
|
||||
Reference in New Issue
Block a user