forked from mtusi2026/mtusi2026lab5
ЛР 5: асинхронная обработка изображений и идемпотентность pipeline
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/minio/minio-go/v7"
|
||||
)
|
||||
|
||||
func (a *App) cleanStaging(ctx context.Context, retention int) error {
|
||||
listCtx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
for entry := range a.s3.ListObjects(listCtx, a.bucket, minio.ListObjectsOptions{Prefix: "staging/", Recursive: true}) {
|
||||
if entry.Err != nil {
|
||||
return entry.Err
|
||||
}
|
||||
id := strings.TrimPrefix(entry.Key, "staging/")
|
||||
if normalized, err := normalizeID(id); err != nil || normalized != id {
|
||||
continue
|
||||
}
|
||||
if err := a.cleanStagedImage(ctx, id, entry.LastModified, retention); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
func (a *App) cleanStagedImage(ctx context.Context, id string, modified time.Time, retention int) error {
|
||||
tx, err := a.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rollbackAttachment(tx)
|
||||
var locked bool
|
||||
if err = tx.QueryRow(ctx, "SELECT pg_try_advisory_xact_lock(hashtextextended($1,0))", "attachment:"+id).Scan(&locked); err != nil || !locked {
|
||||
return err
|
||||
}
|
||||
var removable bool
|
||||
err = tx.QueryRow(ctx, `SELECT result_version>0 OR
|
||||
(status='failed' AND updated_at<=clock_timestamp()-$2::bigint*interval '1 second')
|
||||
FROM attachments WHERE id=$1`, id, retention).Scan(&removable)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
removable = time.Since(modified) > time.Hour
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
if removable {
|
||||
return a.s3.RemoveObject(ctx, a.bucket, stagingKey(id), minio.RemoveObjectOptions{})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user