feat: pseudo cron to delete posts after 3 days

This commit is contained in:
Eric Bower 2022-07-13 22:58:50 -04:00
parent 8d86bdd33d
commit c867fb74e9
No known key found for this signature in database
GPG Key ID: 7A51A47D76D3FD02
4 changed files with 41 additions and 1 deletions

2
go.mod
View File

@ -3,7 +3,7 @@ module git.sr.ht/~erock/pastes.sh
go 1.18
require (
git.sr.ht/~erock/wish v0.0.0-20220713141740-64595ee518ac
git.sr.ht/~erock/wish v0.0.0-20220714010731-7947bf10c388
github.com/alecthomas/chroma v0.10.0
github.com/charmbracelet/wish v0.5.0
github.com/gliderlabs/ssh v0.3.4

2
go.sum
View File

@ -2,6 +2,8 @@ git.sr.ht/~erock/wish v0.0.0-20220707194507-66e938674d95 h1:q3G01ELBZt3EZEOiYWfw
git.sr.ht/~erock/wish v0.0.0-20220707194507-66e938674d95/go.mod h1:QZKk7m9jc9iXah90daPGhQkSfNfxSVvpb6nfVeI+MM0=
git.sr.ht/~erock/wish v0.0.0-20220713141740-64595ee518ac h1:d+q9VPi+kaZpYpZOoXPSEx2KtZ3Gcmkz+x/lpb/V6bU=
git.sr.ht/~erock/wish v0.0.0-20220713141740-64595ee518ac/go.mod h1:QZKk7m9jc9iXah90daPGhQkSfNfxSVvpb6nfVeI+MM0=
git.sr.ht/~erock/wish v0.0.0-20220714010731-7947bf10c388 h1:goL6fa09OPdg+LB2ceJGlx4QkPO+42QI/zqB6tbaKAA=
git.sr.ht/~erock/wish v0.0.0-20220714010731-7947bf10c388/go.mod h1:QZKk7m9jc9iXah90daPGhQkSfNfxSVvpb6nfVeI+MM0=
github.com/alecthomas/chroma v0.10.0 h1:7XDcGkCQopCNKjZHfYrNLraA+M7e0fMiJ/Mfikbfjek=
github.com/alecthomas/chroma v0.10.0/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s=
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8=

View File

@ -482,6 +482,8 @@ func StartApiServer() {
defer db.Close()
logger := cfg.Logger
go CronDeleteExpiredPosts(db)
staticRoutes := createStaticRoutes()
mainRoutes := createMainRoutes(staticRoutes)
subdomainRoutes := createSubdomainRoutes(staticRoutes)

36
internal/cron.go Normal file
View File

@ -0,0 +1,36 @@
package internal
import (
"time"
"git.sr.ht/~erock/wish/cms/db"
)
func deleteExpiredPosts(dbpool db.DB) error {
now := time.Now()
// delete posts that are older than three days
expired := now.AddDate(0, 0, -3)
posts, err := dbpool.FindPostsBeforeDate(&expired)
if err != nil {
return err
}
postIds := []string{}
for _, post := range posts {
postIds = append(postIds, post.ID)
}
err = dbpool.RemovePosts(postIds)
if err != nil {
return err
}
return nil
}
func CronDeleteExpiredPosts(dbpool db.DB) {
for {
deleteExpiredPosts(dbpool)
time.Sleep(1 * time.Hour)
}
}