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
+29 -45
View File
@@ -25,10 +25,10 @@ import (
type Repository interface {
ChatForUser(context.Context, string, string) (model.Chat, error)
CreatePendingAttachment(context.Context, model.Attachment) (model.Attachment, error)
MarkAttachmentReady(context.Context, string, string) (model.Attachment, error)
QueueAttachmentWithJobAndOutbox(context.Context, string, string, model.AttachmentJob, string, string) (model.Attachment, error)
DeletePendingAttachment(context.Context, string, string) error
AttachmentByStorage(context.Context, string, string) (model.Attachment, error)
AttachmentForUser(context.Context, string, string) (model.Attachment, error)
AttachmentVariantForUser(context.Context, string, string, string) (model.Attachment, model.Variant, error)
}
type ObjectStore interface {
@@ -43,6 +43,7 @@ type Config struct {
BodyMemoryBytes int64
DecodeMemoryBytes int64
CleanupTimeout time.Duration
PipelineVersion int
}
type Upload struct {
@@ -52,10 +53,13 @@ type Upload struct {
ContentType string
ContentLength int64
Body io.Reader
Traceparent string
Tracestate string
}
type Content struct {
Attachment model.Attachment
Variant model.Variant
Body io.ReadCloser
}
@@ -69,7 +73,7 @@ type Service struct {
}
func NewService(logger *slog.Logger, repository Repository, objects ObjectStore, config Config) (*Service, error) {
if config.MaxBytes < 1 || config.MaxPixels < 1 || config.MaxPixels > math.MaxInt64/8 || config.CleanupTimeout <= 0 {
if config.MaxBytes < 1 || config.MaxPixels < 1 || config.MaxPixels > math.MaxInt64/8 || config.CleanupTimeout <= 0 || config.PipelineVersion < 1 {
return nil, errors.New("invalid attachment service configuration")
}
if config.BodyMemoryBytes < config.MaxBytes {
@@ -96,58 +100,38 @@ func (s *Service) Upload(ctx context.Context, input Upload) (model.Attachment, e
defer release()
attachmentID := uuid.NewString()
objectKey := "attachments/" + uuid.NewString() + "/original"
stagingKey := "staging/" + attachmentID + "/" + uuid.NewString()
digest := sha256.Sum256(data)
pending, err := s.repository.CreatePendingAttachment(ctx, model.Attachment{
ID: attachmentID, ChatID: input.ChatID, OwnerID: input.OwnerID, Filename: input.Filename,
ContentType: contentType, SizeBytes: int64(len(data)), SHA256: hex.EncodeToString(digest[:]),
ObjectKey: objectKey, Status: model.AttachmentPending,
ObjectKey: stagingKey, StagingKey: stagingKey, Status: model.AttachmentPending,
PipelineVersion: s.config.PipelineVersion,
})
if err != nil {
return model.Attachment{}, mapDependencyError(err)
}
if err := s.objects.Put(ctx, objectKey, contentType, data); err != nil {
s.deletePending(pending.ID, objectKey)
if err := s.objects.Put(ctx, stagingKey, contentType, data); err != nil {
s.logger.Error("attachment staging write failed", "attachment_id", pending.ID, "error", err)
s.deletePending(pending.ID, stagingKey)
return model.Attachment{}, model.ErrUnavailable
}
attachment, err := s.repository.MarkAttachmentReady(ctx, pending.ID, objectKey)
if err == nil {
return attachment, nil
createdAt := time.Now().UTC()
queued, err := s.repository.QueueAttachmentWithJobAndOutbox(ctx, pending.ID, stagingKey, model.AttachmentJob{
ID: uuid.NewString(), AttachmentID: pending.ID, PipelineVersion: s.config.PipelineVersion,
Attempt: 1, Status: model.AttachmentJobQueued, AvailableAt: createdAt, CreatedAt: createdAt,
}, input.Traceparent, input.Tracestate)
if err != nil {
s.logger.Error("attachment queue transaction failed", "attachment_id", pending.ID, "error", err)
return model.Attachment{}, mapDependencyError(err)
}
return s.resolveReadyOutcome(pending.ID, objectKey)
return queued, nil
}
func (s *Service) resolveReadyOutcome(attachmentID, objectKey string) (model.Attachment, error) {
func (s *Service) deletePending(attachmentID, stagingKey string) {
cleanupCtx, cancel := context.WithTimeout(context.Background(), s.config.CleanupTimeout)
defer cancel()
stored, err := s.repository.AttachmentByStorage(cleanupCtx, attachmentID, objectKey)
if err == nil && stored.Status == model.AttachmentReady {
return stored, nil
}
if err == nil && stored.Status == model.AttachmentPending {
if deleteErr := s.objects.Delete(cleanupCtx, objectKey); deleteErr != nil {
s.logger.Error("attachment compensation failed", "object_key", objectKey, "error", deleteErr)
return model.Attachment{}, model.ErrUnavailable
}
if deleteErr := s.repository.DeletePendingAttachment(cleanupCtx, attachmentID, objectKey); deleteErr != nil {
s.logger.Error("pending attachment cleanup failed", "attachment_id", attachmentID, "error", deleteErr)
}
return model.Attachment{}, model.ErrUnavailable
}
if errors.Is(err, model.ErrNotFound) {
if deleteErr := s.objects.Delete(cleanupCtx, objectKey); deleteErr != nil {
s.logger.Error("orphan attachment compensation failed", "object_key", objectKey, "error", deleteErr)
}
return model.Attachment{}, model.ErrUnavailable
}
s.logger.Error("attachment outcome is unknown", "attachment_id", attachmentID, "error", err)
return model.Attachment{}, model.ErrUnavailable
}
func (s *Service) deletePending(attachmentID, objectKey string) {
cleanupCtx, cancel := context.WithTimeout(context.Background(), s.config.CleanupTimeout)
defer cancel()
if err := s.repository.DeletePendingAttachment(cleanupCtx, attachmentID, objectKey); err != nil {
if err := s.repository.DeletePendingAttachment(cleanupCtx, attachmentID, stagingKey); err != nil {
s.logger.Error("pending attachment cleanup failed", "attachment_id", attachmentID, "error", err)
}
}
@@ -160,16 +144,16 @@ func (s *Service) Metadata(ctx context.Context, attachmentID, userID string) (mo
return attachment, nil
}
func (s *Service) Content(ctx context.Context, attachmentID, userID string) (Content, error) {
attachment, err := s.Metadata(ctx, attachmentID, userID)
func (s *Service) Content(ctx context.Context, attachmentID, userID, variantName string) (Content, error) {
attachment, variant, err := s.repository.AttachmentVariantForUser(ctx, attachmentID, userID, variantName)
if err != nil {
return Content{}, err
return Content{}, mapDependencyError(err)
}
body, err := s.objects.Get(ctx, attachment.ObjectKey)
body, err := s.objects.Get(ctx, variant.ObjectKey)
if err != nil {
return Content{}, model.ErrUnavailable
}
return Content{Attachment: attachment, Body: body}, nil
return Content{Attachment: attachment, Variant: variant, Body: body}, nil
}
func (s *Service) readImage(input Upload) ([]byte, string, func(), error) {