package postgres import ( "backend/internal/adapter/repository/postgres/queries" "backend/internal/domain/model" "context" "encoding/json" "errors" "github.com/google/uuid" "github.com/jackc/pgx/v5" "time" ) func job(r queries.AttachmentJob) model.Job { return model.Job{ID: r.ID, AttachmentID: r.AttachmentID, PipelineVersion: r.PipelineVersion, Attempt: r.Attempt, CreatedAt: r.CreatedAt, Traceparent: r.Traceparent, Tracestate: r.Tracestate} } func (s *Store) QueueAttachment(ctx context.Context, a model.Attachment, tp, ts string) (model.Attachment, error) { tx, e := s.pool.Begin(ctx) if e != nil { return model.Attachment{}, mapErr(e) } defer func() { _ = tx.Rollback(ctx) }() q := s.q.WithTx(tx) r, e := q.QueueAttachment(ctx, a.ID) if e != nil { return model.Attachment{}, mapErr(e) } if e = q.InsertJob(ctx, queries.InsertJobParams{JobID: uuid.New(), AttachmentID: r.ID, PipelineVersion: r.PipelineVersion, Traceparent: tp, Tracestate: ts}); e != nil { return model.Attachment{}, mapErr(e) } if e = tx.Commit(ctx); e != nil { return model.Attachment{}, mapErr(e) } return attachment(r), nil } func (s *Store) PendingJobs(ctx context.Context) ([]model.Job, error) { rows, e := s.q.PendingJobs(ctx) if e != nil { return nil, mapErr(e) } out := make([]model.Job, len(rows)) for i, r := range rows { out[i] = job(r) } return out, nil } func (s *Store) Published(ctx context.Context, id uuid.UUID) error { return mapErr(s.q.Published(ctx, id)) } func (s *Store) AcquireJob(ctx context.Context, j model.Job, lease uuid.UUID, d time.Duration) (model.Job, model.Attachment, bool, error) { tx, e := s.pool.Begin(ctx) if e != nil { return j, model.Attachment{}, false, mapErr(e) } defer func() { _ = tx.Rollback(ctx) }() q := s.q.WithTx(tx) r, e := q.AcquireJob(ctx, queries.AcquireJobParams{LeaseID: lease, LeaseSeconds: int32(d / time.Second), JobID: j.ID, PipelineVersion: j.PipelineVersion}) if errors.Is(e, pgx.ErrNoRows) { return j, model.Attachment{}, false, nil } if e != nil { return j, model.Attachment{}, false, mapErr(e) } a, e := q.ProcessingAttachment(ctx, queries.ProcessingAttachmentParams{AttachmentID: r.AttachmentID, PipelineVersion: r.PipelineVersion}) if e != nil { return j, model.Attachment{}, false, mapErr(e) } if e = tx.Commit(ctx); e != nil { return j, model.Attachment{}, false, mapErr(e) } return job(r), attachment(a), true, nil } func (s *Store) CompleteJob(ctx context.Context, j model.Job, lease uuid.UUID, v []model.Variant) (bool, error) { raw, e := json.Marshal(v) if e != nil { return false, e } tx, e := s.pool.Begin(ctx) if e != nil { return false, mapErr(e) } defer func() { _ = tx.Rollback(ctx) }() q := s.q.WithTx(tx) n, e := q.CompleteAttachment(ctx, queries.CompleteAttachmentParams{Variants: raw, JobID: j.ID, LeaseID: lease}) if e != nil { return false, mapErr(e) } if n != 1 { return false, nil } if e = q.CompleteJob(ctx, queries.CompleteJobParams{JobID: j.ID, LeaseID: lease}); e != nil { return false, mapErr(e) } return true, mapErr(tx.Commit(ctx)) } func (s *Store) RetryJob(ctx context.Context, j model.Job, lease uuid.UUID, code string, delay time.Duration, failed bool) error { status := "queued" if failed { status = "failed" } tx, e := s.pool.Begin(ctx) if e != nil { return mapErr(e) } defer func() { _ = tx.Rollback(ctx) }() q := s.q.WithTx(tx) n, e := q.RetryAttachment(ctx, queries.RetryAttachmentParams{NextStatus: status, ErrorCode: code, JobID: j.ID, LeaseID: lease}) if e != nil { return mapErr(e) } if n == 0 { return nil } if e = q.RetryJob(ctx, queries.RetryJobParams{DelayMs: int32(delay / time.Millisecond), Completed: failed, JobID: j.ID, LeaseID: lease}); e != nil { return mapErr(e) } return mapErr(tx.Commit(ctx)) } func (s *Store) ReplayJob(ctx context.Context, id uuid.UUID) (bool, error) { tx, e := s.pool.Begin(ctx) if e != nil { return false, mapErr(e) } defer func() { _ = tx.Rollback(ctx) }() q := s.q.WithTx(tx) a, e := q.ReplayAttachment(ctx, id) if errors.Is(e, pgx.ErrNoRows) { return false, nil } if e != nil { return false, mapErr(e) } if e = q.ReplayJobs(ctx, queries.ReplayJobsParams{AttachmentID: id, PipelineVersion: a.PipelineVersion}); e != nil { return false, mapErr(e) } return true, mapErr(tx.Commit(ctx)) } func (s *Store) AdvancePipeline(ctx context.Context, id uuid.UUID) (bool, error) { tx, e := s.pool.Begin(ctx) if e != nil { return false, mapErr(e) } defer func() { _ = tx.Rollback(ctx) }() q := s.q.WithTx(tx) a, e := q.AdvancePipeline(ctx, id) if errors.Is(e, pgx.ErrNoRows) { return false, nil } if e != nil { return false, mapErr(e) } if e = q.InsertJob(ctx, queries.InsertJobParams{JobID: uuid.New(), AttachmentID: id, PipelineVersion: a.PipelineVersion}); e != nil { return false, mapErr(e) } return true, mapErr(tx.Commit(ctx)) } func (s *Store) StagingCleanup(ctx context.Context) ([]model.Attachment, error) { rows, e := s.q.StagingCleanup(ctx) if e != nil { return nil, mapErr(e) } out := make([]model.Attachment, len(rows)) for i, r := range rows { out[i] = attachment(r) } return out, nil } func (s *Store) StagingCleaned(ctx context.Context, id uuid.UUID) error { return mapErr(s.q.StagingCleaned(ctx, id)) }