feat: pseudo cron to delete posts after 3 days
This commit is contained in:
parent
8d86bdd33d
commit
c867fb74e9
4 changed files with 41 additions and 1 deletions
2
go.mod
2
go.mod
|
@ -3,7 +3,7 @@ module git.sr.ht/~erock/pastes.sh
|
||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
require (
|
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/alecthomas/chroma v0.10.0
|
||||||
github.com/charmbracelet/wish v0.5.0
|
github.com/charmbracelet/wish v0.5.0
|
||||||
github.com/gliderlabs/ssh v0.3.4
|
github.com/gliderlabs/ssh v0.3.4
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -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-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 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-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 h1:7XDcGkCQopCNKjZHfYrNLraA+M7e0fMiJ/Mfikbfjek=
|
||||||
github.com/alecthomas/chroma v0.10.0/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s=
|
github.com/alecthomas/chroma v0.10.0/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s=
|
||||||
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8=
|
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8=
|
||||||
|
|
|
@ -482,6 +482,8 @@ func StartApiServer() {
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
logger := cfg.Logger
|
logger := cfg.Logger
|
||||||
|
|
||||||
|
go CronDeleteExpiredPosts(db)
|
||||||
|
|
||||||
staticRoutes := createStaticRoutes()
|
staticRoutes := createStaticRoutes()
|
||||||
mainRoutes := createMainRoutes(staticRoutes)
|
mainRoutes := createMainRoutes(staticRoutes)
|
||||||
subdomainRoutes := createSubdomainRoutes(staticRoutes)
|
subdomainRoutes := createSubdomainRoutes(staticRoutes)
|
||||||
|
|
36
internal/cron.go
Normal file
36
internal/cron.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue