Files
mtusi2026lab5-fork/backend/internal/storage/postgres/failed_jobs.go
T

52 lines
1.4 KiB
Go

package postgres
import (
"context"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgxpool"
"high-loud-apps-labs/internal/domain"
)
type FailedJobRepo struct {
pool *pgxpool.Pool
}
func NewFailedJobRepo(pool *pgxpool.Pool) *FailedJobRepo {
return &FailedJobRepo{pool: pool}
}
func (r *FailedJobRepo) Record(ctx context.Context, f *domain.FailedJob) error {
if f.ID == "" {
f.ID = uuid.NewString()
}
if f.LastErrorAt.IsZero() {
f.LastErrorAt = time.Now().UTC()
}
const q = `
INSERT INTO failed_jobs (id, attachment_id, reason_code, attempts, last_error_at, created_at)
VALUES ($1::uuid, $2::uuid, $3, $4, $5, $5)
`
_, err := r.pool.Exec(ctx, q, f.ID, f.AttachmentID, f.ReasonCode, f.Attempts, f.LastErrorAt)
return mapError(err)
}
func (r *FailedJobRepo) Resolve(ctx context.Context, attachmentID string, resolution string) error {
const q = `UPDATE failed_jobs SET resolved_at = now(), resolution = $2 WHERE attachment_id = $1::uuid AND resolved_at IS NULL`
_, err := r.pool.Exec(ctx, q, attachmentID, resolution)
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)