From e432d64acb7a9d011a247d47b23a75e74c19be66 Mon Sep 17 00:00:00 2001 From: Pancakes Date: Tue, 29 Oct 2024 21:15:10 -0400 Subject: [PATCH] Emergency fix, read/write saves to S3 from now on --- api/daily/common.go | 2 +- db/savedata.go | 93 +++++++++++++++++++-------------------------- 2 files changed, 40 insertions(+), 55 deletions(-) diff --git a/api/daily/common.go b/api/daily/common.go index 521bf53..187db34 100644 --- a/api/daily/common.go +++ b/api/daily/common.go @@ -163,7 +163,7 @@ func S3SaveMigration() error { continue } - err = db.UpdateLocation(user, username) + err = db.UpdateSystemSaveLocation(user) if err != nil { log.Printf("failed to update location for user %s: %s", username, err) continue diff --git a/db/savedata.go b/db/savedata.go index 4974c4c..e5de022 100644 --- a/db/savedata.go +++ b/db/savedata.go @@ -22,7 +22,7 @@ import ( "context" "encoding/gob" "encoding/json" - "fmt" + "os" "github.com/klauspost/compress/zstd" "github.com/pagefaultgames/rogueserver/defs" @@ -60,21 +60,13 @@ func ReadSeedCompleted(uuid []byte, seed string) (bool, error) { } func ReadSystemSaveData(uuid []byte) (defs.SystemSaveData, error) { - var system defs.SystemSaveData - - isLocal, err := isSaveInLocalDb(uuid) - if err != nil { - return system, err - } - - if !isLocal { - // writes the data back into the database - err = RetrieveSystemSaveFromS3(uuid) - if err != nil { - return system, err - } + // get and return save from S3 + system, err := GetSystemSaveFromS3(uuid) + if err == nil { + return system, nil } + // otherwise look in database and try to move it var data []byte err = handle.QueryRow("SELECT data FROM systemSaveData WHERE uuid = ?", uuid).Scan(&data) if err != nil { @@ -99,24 +91,43 @@ func ReadSystemSaveData(uuid []byte) (defs.SystemSaveData, error) { return system, err } + // put it in S3 + err = StoreSystemSaveData(uuid, system) + if err != nil { + return system, err + } + + // delete the one in db + err = UpdateSystemSaveLocation(uuid) + if err != nil { + return system, err + } + return system, nil } func StoreSystemSaveData(uuid []byte, data defs.SystemSaveData) error { - var buf bytes.Buffer - err := gob.NewEncoder(&buf).Encode(data) + cfg, _ := config.LoadDefaultConfig(context.TODO()) + + client := s3.NewFromConfig(cfg, func(o *s3.Options) { + o.BaseEndpoint = aws.String(os.Getenv("AWS_ENDPOINT_URL_S3")) + }) + + username, err := FetchUsernameFromUUID(uuid) if err != nil { return err } - enc, err := zstd.NewWriter(nil) + json, err := json.Marshal(data) if err != nil { return err } - defer enc.Close() - - _, err = handle.Exec("REPLACE INTO systemSaveData (uuid, data, timestamp) VALUES (?, ?, UTC_TIMESTAMP())", uuid, enc.EncodeAll(buf.Bytes(), nil)) + _, err = client.PutObject(context.Background(), &s3.PutObjectInput{ + Bucket: aws.String("pokerogue-system"), + Key: aws.String(username), + Body: bytes.NewReader(json), + }) if err != nil { return err } @@ -214,25 +225,17 @@ func RetrievePlaytime(uuid []byte) (int, error) { return playtime, nil } -func isSaveInLocalDb(uuid []byte) (bool, error) { - var isLocal bool - err := handle.QueryRow("SELECT isInLocalDb FROM accounts WHERE uuid = ?", uuid).Scan(&isLocal) - if err != nil { - return false, err - } - - return isLocal, nil -} +func GetSystemSaveFromS3(uuid []byte) (defs.SystemSaveData, error) { + var system defs.SystemSaveData -func RetrieveSystemSaveFromS3(uuid []byte) error { username, err := FetchUsernameFromUUID(uuid) if err != nil { - return err + return system, err } cfg, err := config.LoadDefaultConfig(context.TODO()) if err != nil { - return err + return system, err } client := s3.NewFromConfig(cfg) @@ -244,34 +247,16 @@ func RetrieveSystemSaveFromS3(uuid []byte) error { resp, err := client.GetObject(context.TODO(), &s3Object) if err != nil { - return err + return system, err } var session defs.SystemSaveData err = json.NewDecoder(resp.Body).Decode(&session) if err != nil { - return err - } - - err = StoreSystemSaveData(uuid, session) - if err != nil { - return fmt.Errorf("failed to store system save data from S3 for user %s: %s", username, err) - } - - _, err = handle.Exec("UPDATE accounts SET isInLocalDb = 1 WHERE uuid = ?", uuid) - if err != nil { - return err - } - - _, err = client.DeleteObject(context.TODO(), &s3.DeleteObjectInput{ - Bucket: aws.String("pokerogue-system"), - Key: aws.String(username), - }) - if err != nil { - return fmt.Errorf("failed to delete object %s from S3: %s", username, err) + return system, err } - return nil + return session, nil } func RetrieveOldAccounts() ([][]byte, error) { @@ -296,7 +281,7 @@ func RetrieveOldAccounts() ([][]byte, error) { return users, nil } -func UpdateLocation(uuid []byte, username string) error { +func UpdateSystemSaveLocation(uuid []byte) error { _, err := handle.Exec("UPDATE accounts SET isInLocalDb = 0 WHERE uuid = ?", uuid) if err != nil { return err