Skip to content

Commit 5b0bec6

Browse files
committed
[BUG] Replace faulty cleanup go-routine with cron job
1 parent 5d241de commit 5b0bec6

4 files changed

Lines changed: 24 additions & 9 deletions

File tree

cleanup.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package main
22

33
import (
44
"fmt"
5+
"github.com/robfig/cron/v3"
56
"os"
67
"path/filepath"
78
"time"
89
)
910

1011
func cleanupCache() {
1112
now := time.Now()
13+
fmt.Printf("Starting cleanup at %v\n", now)
1214

1315
err := filepath.Walk(skinCacheDir, func(path string, info os.FileInfo, err error) error {
1416
if err != nil {
@@ -35,14 +37,15 @@ func cleanupCache() {
3537
}
3638
}
3739

38-
func startCleanupRoutine() {
39-
ticker := time.NewTicker(2 * time.Hour)
40-
defer ticker.Stop()
40+
func startCleanupRoutine() *cron.Cron {
41+
c := cron.New()
4142

42-
for {
43-
select {
44-
case <-ticker.C:
45-
cleanupCache()
46-
}
43+
_, err := c.AddFunc("0 */2 * * *", cleanupCache)
44+
if err != nil {
45+
fmt.Printf("Failed to start cleanup routine: %v\n", err)
46+
return nil
4747
}
48+
49+
c.Start()
50+
return c
4851
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
require (
1515
github.com/cespare/xxhash/v2 v2.3.0 // indirect
1616
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
17+
github.com/robfig/cron/v3 v3.0.1 // indirect
1718
golang.org/x/net v0.38.0 // indirect
1819
golang.org/x/sys v0.31.0 // indirect
1920
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
2020
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
2121
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
2222
github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs=
23+
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
24+
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
2325
golang.org/x/net v0.38.0 h1:vRMAPTMaeGqVhG5QyLJHqNDwecKTomGeqbnfZyKlBI8=
2426
golang.org/x/net v0.38.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
2527
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=

main.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,17 @@ const renderDir = "cached_skins/render"
1212
func main() {
1313
os.MkdirAll(skinCacheDir, os.ModePerm)
1414
os.MkdirAll(renderDir, os.ModePerm)
15+
16+
// Cleanup Logic
17+
// Initial Cleanup
1518
cleanupCache()
16-
go startCleanupRoutine()
19+
20+
// Cron
21+
cleanup := startCleanupRoutine()
22+
if cleanup != nil {
23+
defer cleanup.Stop()
24+
}
25+
1726
initRedis()
1827
initHttp()
1928
}

0 commit comments

Comments
 (0)