Files
mtusi2026lab7/internal/worker/upgrade.go
T
sxdees 2f2047909f
Skeleton contracts / contracts (pull_request) Blocked by required conditions
ЛР7 на 4: HTTPS, mTLS, OpenBao AppRole и ротация leaf.
Smoke/security/grade4/persistence/failover пройдены; REPORT и DEFENSE.
2026-12-02 23:08:27 +03:00

63 lines
1.3 KiB
Go

package worker
import (
"context"
"errors"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"gavrilenko-sxdees/lab7/internal/db"
s3store "gavrilenko-sxdees/lab7/internal/s3"
)
var (
errAttachmentNotUpgradeable = errors.New("attachment is not upgradeable: expected v1 queued or processing with staging")
errUpgradeNotUpdated = errors.New("attachment upgrade did not update one row")
)
func Upgrade(ctx context.Context, pool txBeginner, storage *s3store.Client, id uuid.UUID) error {
tx, err := pool.Begin(ctx)
if err != nil {
return err
}
defer tx.Rollback(context.Background())
q := db.New(tx)
row, err := q.LockAttachmentForUpgrade(ctx, id)
if errors.Is(err, pgx.ErrNoRows) {
return errAttachmentNotUpgradeable
}
if err != nil {
return err
}
if row.JobID == nil {
return errMissingPipelineJob
}
if err := verify(ctx, storage, *row.StagingKey, row.SizeBytes, row.Sha256); err != nil {
return err
}
n, err := q.UpgradeAttachmentPipeline(ctx, id)
if err != nil {
return err
}
if n != 1 {
return errUpgradeNotUpdated
}
if _, err := q.CreateOutboxEntry(ctx, db.CreateOutboxEntryParams{
AttachmentID: id,
Attempt: row.Attempts + 1,
AvailableAt: time.Now(),
Traceparent: "",
Tracestate: "",
}); err != nil {
return err
}
return tx.Commit(ctx)
}