mirror of
https://github.com/sigmasternchen/threadule
synced 2025-03-15 08:09:01 +00:00
refactored: moved cleanup stuff to extra file + added db cleanup schedule
This commit is contained in:
parent
2687d439b3
commit
211f7b42a8
5 changed files with 36 additions and 4 deletions
|
@ -6,6 +6,8 @@ import (
|
|||
)
|
||||
|
||||
type Data interface {
|
||||
Cleanup() error
|
||||
|
||||
CountUsers() (int64, error)
|
||||
AddUser(user *models.User) error
|
||||
GetUserByUsername(username string) (*models.User, error)
|
||||
|
|
11
backend/internal/data/cleanup.go
Normal file
11
backend/internal/data/cleanup.go
Normal file
|
@ -0,0 +1,11 @@
|
|||
package data
|
||||
|
||||
import "threadule/backend/internal/data/models"
|
||||
|
||||
func (d *Data) Cleanup() error {
|
||||
if err := d.db.Unscoped().Where("deleted_at IS NOT NULL").Delete(&models.Tweet{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -7,7 +7,7 @@ import (
|
|||
|
||||
const sessionDuration = 7 * 24 * time.Hour
|
||||
|
||||
func (l *Logic) scheduleTriggerAuth() {
|
||||
func (l *Logic) cleanupSessions() {
|
||||
err := l.ctx.Data.CleanupSessions()
|
||||
if err != nil {
|
||||
l.ctx.Log.Errorf("couldn't clean up sessions: %v", err)
|
||||
|
|
12
backend/internal/logic/cleanup.go
Normal file
12
backend/internal/logic/cleanup.go
Normal file
|
@ -0,0 +1,12 @@
|
|||
package logic
|
||||
|
||||
func (l *Logic) scheduledCleanup() {
|
||||
l.ctx.Log.Info("doing cleanup...")
|
||||
|
||||
l.cleanupSessions()
|
||||
|
||||
err := l.ctx.Data.Cleanup()
|
||||
if err != nil {
|
||||
l.ctx.Log.Errorf("couldn't clean up database: %v", err)
|
||||
}
|
||||
}
|
|
@ -3,13 +3,20 @@ package logic
|
|||
import "time"
|
||||
|
||||
func (l *Logic) startScheduler() {
|
||||
ticker := time.NewTicker(time.Minute)
|
||||
twitterTicker := time.NewTicker(time.Minute)
|
||||
cleanupTicker := time.NewTicker(time.Hour * 24)
|
||||
|
||||
go func() {
|
||||
for {
|
||||
_ = <-ticker.C
|
||||
_ = <-twitterTicker.C
|
||||
l.scheduleTriggerTwitter()
|
||||
l.scheduleTriggerAuth()
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
for {
|
||||
_ = <-cleanupTicker.C
|
||||
l.scheduledCleanup()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue