88 lines
2.7 KiB
Go
88 lines
2.7 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"highload/internal/database/sqlc"
|
|
"highload/internal/repository"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
func (r *AttachmentRepository) createPipelineJob(ctx context.Context, q *sqlc.Queries, att sqlc.Attachment) (pgtype.UUID, error) {
|
|
job, err := q.CreateAttachmentJob(ctx, sqlc.CreateAttachmentJobParams{
|
|
AttachmentID: att.ID, PipelineVersion: att.PipelineVersion,
|
|
AvailableAt: att.UpdatedAt, MaxAttempts: r.maxAttempts,
|
|
})
|
|
if err != nil {
|
|
return pgtype.UUID{}, convertDBError(err)
|
|
}
|
|
if err := q.CreateInitialOutboxEvent(ctx, job.ID); err != nil {
|
|
return pgtype.UUID{}, convertDBError(err)
|
|
}
|
|
return job.ID, nil
|
|
}
|
|
|
|
// UpgradePipeline serializes the version switch with version 1's finalization,
|
|
// retries and staging cleanup on its job row. The version 2 job starts fresh.
|
|
func (r *AttachmentRepository) UpgradePipeline(ctx context.Context, attachmentID string, checkSource func(context.Context, string) error) (string, bool, error) {
|
|
id, err := getUUIDFromString(attachmentID)
|
|
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)
|
|
oldJob, err := q.LockAttachmentJobForPipeline(txCtx, sqlc.LockAttachmentJobForPipelineParams{
|
|
AttachmentID: *id, PipelineVersion: 1,
|
|
})
|
|
if err != nil {
|
|
return "", false, convertDBError(err)
|
|
}
|
|
att, err := q.GetAttachmentByID(txCtx, *id)
|
|
if err != nil {
|
|
return "", false, convertDBError(err)
|
|
}
|
|
if att.PipelineVersion == 2 {
|
|
jobID, err := q.GetPipelineJobID(txCtx, sqlc.GetPipelineJobIDParams{AttachmentID: *id, PipelineVersion: 2})
|
|
if err != nil {
|
|
return "", false, convertDBError(err)
|
|
}
|
|
return jobID.String(), false, nil
|
|
}
|
|
if att.PipelineVersion != 1 {
|
|
return "", false, fmt.Errorf("cannot upgrade pipeline version %d", att.PipelineVersion)
|
|
}
|
|
source := att.StagingKey
|
|
if att.Status == "ready" {
|
|
source = att.ObjectKey
|
|
}
|
|
if !source.Valid {
|
|
return "", false, fmt.Errorf("original has expired: %w", repository.ErrNotFound)
|
|
}
|
|
if err := checkSource(txCtx, source.String); err != nil {
|
|
return "", false, err
|
|
}
|
|
if err := q.SupersedeAttachmentJob(txCtx, oldJob.ID); err != nil {
|
|
return "", false, convertDBError(err)
|
|
}
|
|
upgraded, err := q.QueueAttachmentUpgrade(txCtx, sqlc.QueueAttachmentUpgradeParams{ID: *id, SourceKey: source.String})
|
|
if err != nil {
|
|
return "", false, convertDBError(err)
|
|
}
|
|
jobID, err := r.createPipelineJob(txCtx, q, upgraded)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
if err := tx.Commit(txCtx); err != nil {
|
|
return "", false, convertDBError(err)
|
|
}
|
|
return jobID.String(), true, nil
|
|
}
|