feat(db): lab5 add failed_jobs table and repository
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
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)
|
||||
}
|
||||
|
||||
var _ domain.FailedJobRepository = (*FailedJobRepo)(nil)
|
||||
@@ -19,3 +19,4 @@ func (s *Storage) Sessions() *SessionRepo { return &SessionRepo{pool:
|
||||
func (s *Storage) Attachments() *AttachmentRepo { return &AttachmentRepo{pool: s.pool} }
|
||||
func (s *Storage) Outbox() *OutboxRepo { return &OutboxRepo{pool: s.pool} }
|
||||
func (s *Storage) Variants() *AttachmentVariantRepo { return &AttachmentVariantRepo{pool: s.pool} }
|
||||
func (s *Storage) FailedJobs() *FailedJobRepo { return &FailedJobRepo{pool: s.pool} }
|
||||
|
||||
Reference in New Issue
Block a user