forked from mtusi2026/mtusi2026lab5
ЛР 5 — асинхронная обработка и версии pipeline
This commit is contained in:
@@ -0,0 +1,167 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"highload/internal/database/sqlc"
|
||||
"highload/internal/repository"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
func (r *AttachmentRepository) ScheduleRetry(ctx context.Context, jobID string, generation int32, code string, at time.Time) (bool, error) {
|
||||
id, err := getUUIDFromString(jobID)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
n, err := r.queries.ScheduleRetry(ctx, sqlc.ScheduleRetryParams{
|
||||
JobID: *id, LeaseGeneration: generation, ErrorCode: code,
|
||||
AvailableAt: pgtype.Timestamptz{Time: at, Valid: true},
|
||||
})
|
||||
if err != nil {
|
||||
return false, convertDBError(err)
|
||||
}
|
||||
return n == 1, nil
|
||||
}
|
||||
|
||||
func (r *AttachmentRepository) FailJob(ctx context.Context, jobID string, generation int32, code string) (bool, error) {
|
||||
id, err := getUUIDFromString(jobID)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
n, err := r.queries.FailJob(ctx, sqlc.FailJobParams{
|
||||
JobID: *id, LeaseGeneration: generation, ErrorCode: code,
|
||||
})
|
||||
if err != nil {
|
||||
return false, convertDBError(err)
|
||||
}
|
||||
return n == 1, nil
|
||||
}
|
||||
|
||||
func (r *AttachmentRepository) EnqueueExpiredLeases(ctx context.Context, batchSize int32) (int, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, time.Second)
|
||||
defer cancel()
|
||||
rows, err := r.queries.EnqueueExpiredLeases(ctx, batchSize)
|
||||
if err != nil {
|
||||
return 0, convertDBError(err)
|
||||
}
|
||||
return len(rows), nil
|
||||
}
|
||||
|
||||
// PublishPending holds the outbox row until the broker confirms delivery.
|
||||
func (r *AttachmentRepository) PublishPending(ctx context.Context, publish func(context.Context, sqlc.Outbox) error) (bool, error) {
|
||||
txCtx, cancel := context.WithTimeout(ctx, 8*time.Second)
|
||||
defer cancel()
|
||||
tx, err := r.pool.Begin(txCtx)
|
||||
if err != nil {
|
||||
return false, convertDBError(err)
|
||||
}
|
||||
defer rollbackTx(ctx, tx)
|
||||
q := r.queries.WithTx(tx)
|
||||
row, err := q.NextPendingOutboxEvent(txCtx)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, convertDBError(err)
|
||||
}
|
||||
if err := publish(txCtx, row); err != nil {
|
||||
return false, err
|
||||
}
|
||||
n, err := q.MarkOutboxEventSent(txCtx, row.ID)
|
||||
if err != nil {
|
||||
return false, convertDBError(err)
|
||||
}
|
||||
if n != 1 {
|
||||
return false, fmt.Errorf("outbox row disappeared during publish")
|
||||
}
|
||||
if err := tx.Commit(txCtx); err != nil {
|
||||
return false, convertDBError(err)
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (r *AttachmentRepository) ListFailedJobs(ctx context.Context) ([]sqlc.ListFailedAttachmentJobsRow, error) {
|
||||
return r.queries.ListFailedAttachmentJobs(ctx, 100)
|
||||
}
|
||||
|
||||
// ReplayFailed checks the staged object while holding the same job lock as cleanup.
|
||||
func (r *AttachmentRepository) ReplayFailed(ctx context.Context, jobID, requestID, operator, reason string, extra int32, check func(context.Context, string) error) (bool, error) {
|
||||
id, err := getUUIDFromString(jobID)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
request, err := getUUIDFromString(requestID)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
txCtx, cancel := context.WithTimeout(ctx, attachmentTransactionTimeout)
|
||||
defer cancel()
|
||||
tx, err := r.pool.Begin(txCtx)
|
||||
if err != nil {
|
||||
return false, convertDBError(err)
|
||||
}
|
||||
defer rollbackTx(ctx, tx)
|
||||
q := r.queries.WithTx(tx)
|
||||
job, err := q.LockAttachmentJobForReplay(txCtx, *id)
|
||||
if err != nil {
|
||||
return false, convertDBError(err)
|
||||
}
|
||||
att, err := q.GetAttachmentByID(txCtx, job.AttachmentID)
|
||||
if err != nil {
|
||||
return false, convertDBError(err)
|
||||
}
|
||||
if att.Status != "failed" || att.PipelineVersion != job.PipelineVersion {
|
||||
return false, nil
|
||||
}
|
||||
if !att.StagingKey.Valid {
|
||||
return false, fmt.Errorf("staging has expired: %w", repository.ErrNotFound)
|
||||
}
|
||||
if err := check(txCtx, att.StagingKey.String); err != nil {
|
||||
return false, err
|
||||
}
|
||||
rows, err := q.ReplayFailed(txCtx, sqlc.ReplayFailedParams{
|
||||
JobID: *id, RequestID: *request, OperatorID: operator, Reason: reason, ExtraAttempts: extra,
|
||||
})
|
||||
if err != nil {
|
||||
return false, convertDBError(err)
|
||||
}
|
||||
if rows == 0 {
|
||||
return false, nil
|
||||
}
|
||||
if err := tx.Commit(txCtx); err != nil {
|
||||
return false, convertDBError(err)
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (r *AttachmentRepository) PublishedObjectKeys(ctx context.Context, attachmentID string, version int32) ([]string, error) {
|
||||
id, err := getUUIDFromString(attachmentID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
original, err := r.queries.GetPublishedOriginalKey(ctx, sqlc.GetPublishedOriginalKeyParams{
|
||||
AttachmentID: *id, PipelineVersion: version,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
variants, err := r.queries.ListAttachmentVariants(ctx, sqlc.ListAttachmentVariantsParams{
|
||||
AttachmentID: *id, PipelineVersion: version,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
keys := []string{original.String}
|
||||
for _, v := range variants {
|
||||
keys = append(keys, v.ObjectKey)
|
||||
}
|
||||
if len(keys) != 4 {
|
||||
return nil, fmt.Errorf("ready job has an incomplete variant set")
|
||||
}
|
||||
return keys, nil
|
||||
}
|
||||
Reference in New Issue
Block a user