Skip to content

Commit

Permalink
Get rid of the S3 scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
patapancakes committed Oct 22, 2024
1 parent c09d277 commit 56ef175
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions api/daily/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ import (
const secondsPerDay = 60 * 60 * 24

var (
scheduler = cron.New(cron.WithLocation(time.UTC))
s3scheduler = cron.New(cron.WithLocation(time.UTC))
secret []byte
scheduler = cron.New(cron.WithLocation(time.UTC))
secret []byte
)

func Init() error {
Expand Down Expand Up @@ -92,12 +91,14 @@ func Init() error {
scheduler.Start()

if os.Getenv("AWS_ENDPOINT_URL_S3") != "" {
_, err = s3scheduler.AddFunc("@hourly", S3SaveMigration)
if err != nil {
return err
}

s3scheduler.Start()
go func() {
for {
err = S3SaveMigration()
if err != nil {
return
}
}
}()
}

return nil
Expand All @@ -116,7 +117,7 @@ func deriveSeed(seedTime time.Time) []byte {
return hashedSeed[:]
}

func S3SaveMigration() {
func S3SaveMigration() error {
cfg, _ := config.LoadDefaultConfig(context.TODO())

svc := s3.NewFromConfig(cfg, func(o *s3.Options) {
Expand All @@ -128,13 +129,12 @@ func S3SaveMigration() {
Bucket: aws.String("pokerogue-system"),
})
if err != nil {
log.Printf("error while creating bucket: %s", err)
log.Printf("error while creating bucket (already exists?): %s", err)
}

accounts, err := db.RetrieveOldAccounts()
if err != nil {
log.Printf("failed to retrieve old accounts")
return
return fmt.Errorf("failed to retrieve old accounts: %s", err)
}

for _, user := range accounts {
Expand Down Expand Up @@ -165,10 +165,12 @@ func S3SaveMigration() {

err = db.UpdateLocation(user, username)
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
}

log.Printf("Saved data in S3 for user %s", username)
log.Printf("saved data in S3 for user %s", username)
}

return nil
}

0 comments on commit 56ef175

Please sign in to comment.