forked from mtusi2026/mtusi2026lab6
119 lines
3.1 KiB
Go
119 lines
3.1 KiB
Go
package messenger
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"github.com/minio/minio-go/v7"
|
|
"mtusi-messenger/internal/objectstore"
|
|
)
|
|
|
|
type CleanupResult struct {
|
|
RemovedObjects int `json:"removed_objects"`
|
|
RemovedIntents int64 `json:"removed_intents"`
|
|
KeptReady int `json:"kept_ready"`
|
|
SkippedRecent int `json:"skipped_recent"`
|
|
SkippedBusy int `json:"skipped_busy"`
|
|
}
|
|
|
|
// Cleanup removes abandoned uploads. Accepted pipeline jobs protect staging.
|
|
func Cleanup(ctx context.Context, db *pgxpool.Pool, store *objectstore.Store, grace time.Duration) (CleanupResult, error) {
|
|
result := CleanupResult{}
|
|
cutoff := time.Now().Add(-grace)
|
|
clean := func(key string) error {
|
|
if !strings.HasPrefix(key, "originals/") && !strings.HasPrefix(key, "staging/") {
|
|
return nil
|
|
}
|
|
tx, err := db.Begin(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer rollbackUpload(tx)
|
|
var locked bool
|
|
if err = tx.QueryRow(ctx, `SELECT pg_try_advisory_xact_lock(hashtextextended($1,4))`, key).Scan(&locked); err != nil {
|
|
return err
|
|
}
|
|
if !locked {
|
|
result.SkippedBusy++
|
|
return nil
|
|
}
|
|
var ready, recent bool
|
|
if err = tx.QueryRow(ctx, `SELECT (EXISTS(SELECT 1 FROM attachments WHERE object_key=$1) OR EXISTS(SELECT 1 FROM attachment_jobs WHERE staging_key=$1)),EXISTS(SELECT 1 FROM attachment_uploads WHERE object_key=$1 AND created_at>$2)`, key, cutoff).Scan(&ready, &recent); err != nil {
|
|
return err
|
|
}
|
|
if ready {
|
|
result.KeptReady++
|
|
return nil
|
|
}
|
|
if recent {
|
|
result.SkippedRecent++
|
|
return nil
|
|
}
|
|
info, err := store.Client.StatObject(ctx, store.Bucket, key, minio.StatObjectOptions{})
|
|
exists := err == nil
|
|
if err != nil && minio.ToErrorResponse(err).Code != "NoSuchKey" {
|
|
return err
|
|
}
|
|
if exists && info.LastModified.After(cutoff) {
|
|
result.SkippedRecent++
|
|
return nil
|
|
}
|
|
if exists {
|
|
if err = store.Delete(ctx, key); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
command, err := tx.Exec(ctx, `DELETE FROM attachment_uploads WHERE object_key=$1`, key)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err = tx.Commit(ctx); err != nil {
|
|
return err
|
|
}
|
|
result.RemovedIntents += command.RowsAffected()
|
|
if exists {
|
|
result.RemovedObjects++
|
|
}
|
|
return nil
|
|
}
|
|
rows, err := db.Query(ctx, `SELECT object_key FROM attachment_uploads WHERE created_at<$1 ORDER BY created_at LIMIT 1000`, cutoff)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
keys := []string{}
|
|
for rows.Next() {
|
|
var key string
|
|
if err = rows.Scan(&key); err != nil {
|
|
rows.Close()
|
|
return result, err
|
|
}
|
|
keys = append(keys, key)
|
|
}
|
|
err = rows.Err()
|
|
rows.Close()
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
for _, key := range keys {
|
|
if err = clean(key); err != nil {
|
|
return result, err
|
|
}
|
|
}
|
|
listctx, cancel := context.WithCancel(ctx)
|
|
defer cancel()
|
|
for object := range store.Client.ListObjects(listctx, store.Bucket, minio.ListObjectsOptions{Recursive: true}) {
|
|
if object.Err != nil {
|
|
return result, fmt.Errorf("S3 cleanup listing: %w", object.Err)
|
|
}
|
|
if object.LastModified.Before(cutoff) {
|
|
if err = clean(object.Key); err != nil {
|
|
return result, err
|
|
}
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|