diff --git a/backend/internal/domain/failed_jobs.go b/backend/internal/domain/failed_job.go similarity index 100% rename from backend/internal/domain/failed_jobs.go rename to backend/internal/domain/failed_job.go diff --git a/backend/internal/domain/repository.go b/backend/internal/domain/repository.go index 2e39cef..dc07f3a 100644 --- a/backend/internal/domain/repository.go +++ b/backend/internal/domain/repository.go @@ -45,6 +45,7 @@ type AttachmentRepository interface { ExistingKeys(ctx context.Context, keys []string) ([]string, error) DeletePending(ctx context.Context, id string) error CreateInTx(ctx context.Context, tx Tx, a *Attachment) error + ResetForReplay(ctx context.Context, tx Tx, id string) error } type OutboxRepository interface { @@ -69,4 +70,5 @@ type AttachmentVariantRepository interface { type FailedJobRepository interface { Record(ctx context.Context, f *FailedJob) error Resolve(ctx context.Context, attachmentID string, resolution string) error + ResolveInTx(ctx context.Context, tx Tx, id string) error } diff --git a/backend/internal/httpapi/handler/attachment.go b/backend/internal/httpapi/handler/attachment.go index 24e9574..3032c47 100644 --- a/backend/internal/httpapi/handler/attachment.go +++ b/backend/internal/httpapi/handler/attachment.go @@ -92,3 +92,20 @@ func (h *AttachmentHandler) Content(c *gin.Context) { c.DataFromReader(http.StatusOK, size, contentType, rc, nil) } + +func (h *AttachmentHandler) Replay(c *gin.Context) { + me := middleware.CurrentUser(c) + + attachmentID, err := parseUUIDPathParam(c, "attachment_id") + if err != nil { + respondError(c, err) + return + } + + if err := h.svc.Replay(c.Request.Context(), me.ID, attachmentID); err != nil { + respondError(c, err) + return + } + + c.Status(http.StatusAccepted) +} diff --git a/backend/internal/httpapi/router.go b/backend/internal/httpapi/router.go index a614a01..e381f2b 100644 --- a/backend/internal/httpapi/router.go +++ b/backend/internal/httpapi/router.go @@ -50,6 +50,7 @@ func NewRouter(h Handlers, auth gin.HandlerFunc) *gin.Engine { protected.POST("/chats/:chat_id/attachments", h.Attachment.Upload) protected.GET("/attachments/:attachment_id", h.Attachment.Get) protected.GET("/attachments/:attachment_id/content", h.Attachment.Content) + protected.POST("/attachments/:attachment_id/replay", h.Attachment.Replay) } } diff --git a/backend/internal/service/attachment.go b/backend/internal/service/attachment.go index 358ed47..b0bd3a5 100644 --- a/backend/internal/service/attachment.go +++ b/backend/internal/service/attachment.go @@ -29,6 +29,7 @@ type AttachmentService struct { chats domain.ChatRepository storage domain.ObjectStorage outbox domain.OutboxRepository + failedJobs domain.FailedJobRepository tx txRunner maxBytes int64 maxPixels int64 @@ -39,6 +40,7 @@ func NewAttachmentService( chats domain.ChatRepository, storage domain.ObjectStorage, outbox domain.OutboxRepository, + failedJobs domain.FailedJobRepository, tx txRunner, maxBytes, maxPixels int64, ) *AttachmentService { @@ -47,6 +49,7 @@ func NewAttachmentService( chats: chats, storage: storage, outbox: outbox, + failedJobs: failedJobs, tx: tx, maxBytes: maxBytes, maxPixels: maxPixels, @@ -190,6 +193,49 @@ func (s *AttachmentService) Download( return nil, 0, "", &domain.NotFoundError{Entity: "variant", ID: variantName} } +// Replay позволяет оператору перезапустить упавшую задачу. +func (s *AttachmentService) Replay(ctx context.Context, userID, attachmentID string) error { + a, err := s.Get(ctx, userID, attachmentID) + if err != nil { + return err + } + + if a.Status != domain.AttachmentStatusFailed { + return &domain.ConflictError{Reason: "перезапустить можно только failed-вложение"} + } + + // Формируем новое сообщение + payload, err := json.Marshal(domain.JobMessage{ + AttachmentID: a.ID, + PipelineVersion: a.PipelineVersion, + Attempt: 1, // Сбрасываем счетчик попыток + }) + if err != nil { + return err + } + + err = s.tx.WithTx(ctx, func(tx domain.Tx) error { + if err := s.attachments.ResetForReplay(ctx, tx, a.ID); err != nil { + return err + } + // Помечаем в реестре упавших задач (внутри транзакции) + if err := s.failedJobs.ResolveInTx(ctx, tx, a.ID); err != nil { + return err + } + return s.outbox.Enqueue(ctx, tx, &domain.OutboxEntry{ + AggregateType: domain.OutboxAggregateAttachment, + AggregateID: a.ID, + EventType: domain.OutboxEventProcess, + Payload: payload, + }) + }) + if err != nil { + return err + } + + return nil +} + func (s *AttachmentService) ensureMember(ctx context.Context, userID, chatID string) error { c, err := s.chats.GetByID(ctx, chatID) if err != nil { diff --git a/backend/internal/storage/postgres/attachments.go b/backend/internal/storage/postgres/attachments.go index 4b78dd8..3289216 100644 --- a/backend/internal/storage/postgres/attachments.go +++ b/backend/internal/storage/postgres/attachments.go @@ -307,4 +307,13 @@ func (r *AttachmentRepo) ScheduleRetry(ctx context.Context, id string, nextAttem return mapError(err) } +func (r *AttachmentRepo) ResetForReplay(ctx context.Context, tx domain.Tx, id string) error { + const q = ` + UPDATE attachments + SET status = 'queued', attempts = 0, error_code = NULL, locked_until = NULL, next_attempt_at = NULL, updated_at = now() + WHERE id = $1::uuid AND status = 'failed' + ` + return tx.Exec(ctx, q, id) +} + var _ domain.AttachmentRepository = (*AttachmentRepo)(nil) diff --git a/backend/internal/storage/postgres/failed_jobs.go b/backend/internal/storage/postgres/failed_jobs.go index 9ddcbc4..7006d3f 100644 --- a/backend/internal/storage/postgres/failed_jobs.go +++ b/backend/internal/storage/postgres/failed_jobs.go @@ -39,4 +39,13 @@ func (r *FailedJobRepo) Resolve(ctx context.Context, attachmentID string, resolu return mapError(err) } +func (r *FailedJobRepo) ResolveInTx(ctx context.Context, tx domain.Tx, id string) error { + const q = ` + UPDATE failed_jobs + SET resolved_at = now(), resolution = 'manual_replay' + WHERE attachment_id = $1::uuid AND resolved_at IS NULL + ` + return tx.Exec(ctx, q, id) +} + var _ domain.FailedJobRepository = (*FailedJobRepo)(nil)