feat(worker): lab5 add retries, DLQ registry and manual replay endpoint
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user