Emergency fix, read/write saves to S3 from now on

beta
Pancakes 2 months ago
parent c2af4a07e9
commit e432d64acb
No known key found for this signature in database
GPG Key ID: 5792877BFA27DC8F

@ -163,7 +163,7 @@ func S3SaveMigration() error {
continue continue
} }
err = db.UpdateLocation(user, username) err = db.UpdateSystemSaveLocation(user)
if err != nil { if err != nil {
log.Printf("failed to update location for user %s: %s", username, err) log.Printf("failed to update location for user %s: %s", username, err)
continue continue

@ -22,7 +22,7 @@ import (
"context" "context"
"encoding/gob" "encoding/gob"
"encoding/json" "encoding/json"
"fmt" "os"
"github.com/klauspost/compress/zstd" "github.com/klauspost/compress/zstd"
"github.com/pagefaultgames/rogueserver/defs" "github.com/pagefaultgames/rogueserver/defs"
@ -60,21 +60,13 @@ func ReadSeedCompleted(uuid []byte, seed string) (bool, error) {
} }
func ReadSystemSaveData(uuid []byte) (defs.SystemSaveData, error) { func ReadSystemSaveData(uuid []byte) (defs.SystemSaveData, error) {
var system defs.SystemSaveData // get and return save from S3
system, err := GetSystemSaveFromS3(uuid)
isLocal, err := isSaveInLocalDb(uuid) if err == nil {
if err != nil { return system, nil
return system, err
}
if !isLocal {
// writes the data back into the database
err = RetrieveSystemSaveFromS3(uuid)
if err != nil {
return system, err
}
} }
// otherwise look in database and try to move it
var data []byte var data []byte
err = handle.QueryRow("SELECT data FROM systemSaveData WHERE uuid = ?", uuid).Scan(&data) err = handle.QueryRow("SELECT data FROM systemSaveData WHERE uuid = ?", uuid).Scan(&data)
if err != nil { if err != nil {
@ -99,24 +91,43 @@ func ReadSystemSaveData(uuid []byte) (defs.SystemSaveData, error) {
return system, err 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 return system, nil
} }
func StoreSystemSaveData(uuid []byte, data defs.SystemSaveData) error { func StoreSystemSaveData(uuid []byte, data defs.SystemSaveData) error {
var buf bytes.Buffer cfg, _ := config.LoadDefaultConfig(context.TODO())
err := gob.NewEncoder(&buf).Encode(data)
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 { if err != nil {
return err return err
} }
enc, err := zstd.NewWriter(nil) json, err := json.Marshal(data)
if err != nil { if err != nil {
return err return err
} }
defer enc.Close() _, err = client.PutObject(context.Background(), &s3.PutObjectInput{
Bucket: aws.String("pokerogue-system"),
_, err = handle.Exec("REPLACE INTO systemSaveData (uuid, data, timestamp) VALUES (?, ?, UTC_TIMESTAMP())", uuid, enc.EncodeAll(buf.Bytes(), nil)) Key: aws.String(username),
Body: bytes.NewReader(json),
})
if err != nil { if err != nil {
return err return err
} }
@ -214,25 +225,17 @@ func RetrievePlaytime(uuid []byte) (int, error) {
return playtime, nil return playtime, nil
} }
func isSaveInLocalDb(uuid []byte) (bool, error) { func GetSystemSaveFromS3(uuid []byte) (defs.SystemSaveData, error) {
var isLocal bool var system defs.SystemSaveData
err := handle.QueryRow("SELECT isInLocalDb FROM accounts WHERE uuid = ?", uuid).Scan(&isLocal)
if err != nil {
return false, err
}
return isLocal, nil
}
func RetrieveSystemSaveFromS3(uuid []byte) error {
username, err := FetchUsernameFromUUID(uuid) username, err := FetchUsernameFromUUID(uuid)
if err != nil { if err != nil {
return err return system, err
} }
cfg, err := config.LoadDefaultConfig(context.TODO()) cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil { if err != nil {
return err return system, err
} }
client := s3.NewFromConfig(cfg) client := s3.NewFromConfig(cfg)
@ -244,34 +247,16 @@ func RetrieveSystemSaveFromS3(uuid []byte) error {
resp, err := client.GetObject(context.TODO(), &s3Object) resp, err := client.GetObject(context.TODO(), &s3Object)
if err != nil { if err != nil {
return err return system, err
} }
var session defs.SystemSaveData var session defs.SystemSaveData
err = json.NewDecoder(resp.Body).Decode(&session) err = json.NewDecoder(resp.Body).Decode(&session)
if err != nil { if err != nil {
return err return system, 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 nil return session, nil
} }
func RetrieveOldAccounts() ([][]byte, error) { func RetrieveOldAccounts() ([][]byte, error) {
@ -296,7 +281,7 @@ func RetrieveOldAccounts() ([][]byte, error) {
return users, nil 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) _, err := handle.Exec("UPDATE accounts SET isInLocalDb = 0 WHERE uuid = ?", uuid)
if err != nil { if err != nil {
return err return err

Loading…
Cancel
Save