feat(worker): lab5 add retries, DLQ registry and manual replay endpoint

This commit is contained in:
Голиков Maximillian Антон
2026-11-10 19:08:15 +03:00
parent dce767a156
commit f748c09e10
7 changed files with 84 additions and 0 deletions
+46
View File
@@ -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 {