From 94778893904ef5846fbdaf5946bb3d0beeec4ae8 Mon Sep 17 00:00:00 2001 From: maru Date: Tue, 16 Apr 2024 21:01:24 -0400 Subject: [PATCH] Use better cron package --- api/daily/common.go | 14 ++++++++++---- api/savedata/clear.go | 1 + api/stats.go | 25 ++++++++++++++++--------- go.mod | 9 ++------- go.sum | 36 ------------------------------------ 5 files changed, 29 insertions(+), 56 deletions(-) diff --git a/api/daily/common.go b/api/daily/common.go index ee900c3..9f54566 100644 --- a/api/daily/common.go +++ b/api/daily/common.go @@ -10,14 +10,14 @@ import ( "os" "time" - "github.com/go-co-op/gocron" "github.com/pagefaultgames/pokerogue-server/db" + "github.com/robfig/cron/v3" ) const secondsPerDay = 60 * 60 * 24 var ( - scheduler = gocron.NewScheduler(time.UTC) + scheduler = cron.New() secret []byte ) @@ -51,8 +51,14 @@ func Init() error { log.Printf("Daily Run Seed: %s", Seed()) - scheduler.Every(1).Day().At("00:00").Do(recordNewDaily()) - scheduler.StartAsync() + scheduler.AddFunc("@daily", func() { + err := recordNewDaily() + if err != nil { + log.Printf("error while recording new daily: %s", err) + } + }) + + scheduler.Start() return nil } diff --git a/api/savedata/clear.go b/api/savedata/clear.go index 879243f..9e6426d 100644 --- a/api/savedata/clear.go +++ b/api/savedata/clear.go @@ -34,6 +34,7 @@ func Clear(uuid []byte, slot int, seed string, save defs.SessionSaveData) (Clear if !sessionCompleted { waveCompleted-- } + err = db.AddOrUpdateAccountDailyRun(uuid, save.Score, waveCompleted) if err != nil { log.Printf("failed to add or update daily run record: %s", err) diff --git a/api/stats.go b/api/stats.go index 4d8ac8c..10f5bb7 100644 --- a/api/stats.go +++ b/api/stats.go @@ -2,38 +2,45 @@ package api import ( "log" - "time" - "github.com/go-co-op/gocron" "github.com/pagefaultgames/pokerogue-server/db" + "github.com/robfig/cron/v3" ) var ( - statScheduler = gocron.NewScheduler(time.UTC) + scheduler = cron.New() playerCount int battleCount int classicSessionCount int ) func scheduleStatRefresh() { - statScheduler.Every(10).Second().Do(updateStats) - statScheduler.StartAsync() + scheduler.AddFunc("@every 10s", func() { + err := updateStats() + if err != nil { + log.Printf("failed to update stats: %s", err) + } + }) + + scheduler.Start() } -func updateStats() { +func updateStats() error { var err error playerCount, err = db.FetchPlayerCount() if err != nil { - log.Print(err) + return err } battleCount, err = db.FetchBattleCount() if err != nil { - log.Print(err) + return err } classicSessionCount, err = db.FetchClassicSessionCount() if err != nil { - log.Print(err) + return err } + + return nil } diff --git a/go.mod b/go.mod index c71b209..6675c3e 100644 --- a/go.mod +++ b/go.mod @@ -3,15 +3,10 @@ module github.com/pagefaultgames/pokerogue-server go 1.22 require ( - github.com/go-co-op/gocron v1.37.0 github.com/go-sql-driver/mysql v1.7.1 github.com/klauspost/compress v1.17.4 + github.com/robfig/cron/v3 v3.0.1 golang.org/x/crypto v0.16.0 ) -require ( - github.com/google/uuid v1.4.0 // indirect - github.com/robfig/cron/v3 v3.0.1 // indirect - go.uber.org/atomic v1.9.0 // indirect - golang.org/x/sys v0.15.0 // indirect -) +require golang.org/x/sys v0.15.0 // indirect diff --git a/go.sum b/go.sum index cf32cb2..88cd836 100644 --- a/go.sum +++ b/go.sum @@ -1,46 +1,10 @@ -github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/go-co-op/gocron v1.37.0 h1:ZYDJGtQ4OMhTLKOKMIch+/CY70Brbb1dGdooLEhh7b0= -github.com/go-co-op/gocron v1.37.0/go.mod h1:3L/n6BkO7ABj+TrfSVXLRzsP26zmikL4ISkLQ0O8iNY= github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= -github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4= -github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4= github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= -github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= -github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= -github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= -github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= -go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY= golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= -gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=