75 lines
2.9 KiB
Go
75 lines
2.9 KiB
Go
package repositories
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"github.com/google/uuid"
|
|
"strings"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
// VariantObjectKey separates effects of different pipelines, including retries.
|
|
func VariantObjectKey(id string, version int, name string) string {
|
|
if version < 1 {
|
|
version = 1
|
|
}
|
|
extension := ".jpg"
|
|
if name == "optimized" {
|
|
extension = ".webp"
|
|
}
|
|
return fmt.Sprintf("variants/%s/v%d/%s%s", id, version, name, extension)
|
|
}
|
|
|
|
// UpgradePipeline is an explicit operator action. It never downgrades a result.
|
|
// The row lock serializes this change with publication, replay and cleanup.
|
|
func (s *AttachmentStore) UpgradePipeline(ctx context.Context, id string, version int, reason string, exists func(context.Context, string) error) (bool, error) {
|
|
reason = strings.TrimSpace(reason)
|
|
if reason == "" || utf8.RuneCountInString(reason) > 200 {
|
|
return false, errors.New("upgrade reason must contain 1-200 characters")
|
|
}
|
|
tx, err := s.db.Begin(ctx)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
defer tx.Rollback(context.Background())
|
|
var current int
|
|
var status, original, source string
|
|
err = tx.QueryRow(ctx, `SELECT a.pipeline_version,a.status,a.object_key,j.staging_key FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id WHERE a.id=$1 AND j.pipeline_version=a.pipeline_version FOR UPDATE OF j,a`, id).Scan(¤t, &status, &original, &source)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if version <= current {
|
|
return false, nil
|
|
}
|
|
if version != current+1 || version > 2 {
|
|
return false, errors.New("only sequential upgrade to pipeline 2 is supported")
|
|
}
|
|
|
|
if status == "ready" {
|
|
source = original
|
|
}
|
|
if source != "staging/jobs/"+id && source != "originals/"+id {
|
|
return false, errors.New("invalid pipeline source")
|
|
}
|
|
if err = exists(ctx, source); err != nil {
|
|
return false, errors.New("pipeline source unavailable")
|
|
}
|
|
if _, err = tx.Exec(ctx, `UPDATE attachment_jobs SET superseded_at=clock_timestamp(),lease_token=NULL,lease_until=NULL WHERE attachment_id=$1 AND pipeline_version<$2`, id, version); err != nil {
|
|
return false, err
|
|
}
|
|
if _, err = tx.Exec(ctx, `UPDATE attachment_job_attempts SET finished_at=clock_timestamp(),error_code='superseded' WHERE job_id IN (SELECT id FROM attachment_jobs WHERE attachment_id=$1 AND pipeline_version<$2) AND finished_at IS NULL`, id, version); err != nil {
|
|
return false, err
|
|
}
|
|
if _, err = tx.Exec(ctx, `INSERT INTO attachment_jobs(id,attachment_id,staging_key,pipeline_version) VALUES($1,$2,$3,$4)`, uuid.NewString(), id, source, version); err != nil {
|
|
return false, err
|
|
}
|
|
if _, err = tx.Exec(ctx, `UPDATE attachments SET pipeline_version=$2,status='queued',error_code=NULL,updated_at=clock_timestamp() WHERE id=$1`, id, version); err != nil {
|
|
return false, err
|
|
}
|
|
if _, err = tx.Exec(ctx, `INSERT INTO attachment_pipeline_upgrades(attachment_id,from_version,to_version,reason) VALUES($1,$2,$3,$4)`, id, current, version, reason); err != nil {
|
|
return false, err
|
|
}
|
|
return true, tx.Commit(ctx)
|
|
}
|