Commit cea8d211 authored by Las Zenow's avatar Las Zenow

Clean up old submissions

parent 60379ba1
......@@ -75,6 +75,7 @@ func Init(options Options) (DB, error) {
return nil, err
}
go db.frontPageUpdater()
go db.submissionsCleaner()
return db, nil
}
......
package database
// TODO: clean up old submissions
import (
"fmt"
"time"
"github.com/prometheus/common/log"
)
const (
daysExpireSubmission = 30
)
type Submission struct {
ID int `sql:"type:serial"`
......@@ -8,6 +17,7 @@ type Submission struct {
Filename string
Status string
Comment string
LastModified time.Time
UserID int `sql:"type:integer,unique"`
User *User
BookID string `sql:"type:varchar(16),unique"`
......@@ -21,6 +31,7 @@ func (db *pgDB) AddSubmission(submission Submission, userName string) (id int, e
submission.UserID = user.ID
}
}
submission.LastModified = time.Now()
err = db.sql.Insert(&submission)
return submission.ID, err
}
......@@ -29,6 +40,7 @@ func (db *pgDB) UpdateSubmission(id int, status string, book *Book) error {
_, err := db.sql.Model(&Submission{}).
Set("status = ?", status).
Set("book_id = ?", extractID(book)).
Set("last_modified = CURRENT_TIMESTAMP").
Where("id = ?", id).
Update()
return err
......@@ -38,6 +50,7 @@ func (db *pgDB) UpdateSubmissionByBook(bookID string, status string, book *Book)
_, err := db.sql.Model(&Submission{}).
Set("status = ?", status).
Set("book_id = ?", extractID(book)).
Set("last_modified = CURRENT_TIMESTAMP").
Where("book_id = ?", bookID).
Update()
return err
......@@ -75,11 +88,26 @@ func (db *pgDB) GetUserSubmissions(userName string) (submission []Submission, er
err = db.sql.Model(&submission).
Column("Book", "User").
Where("username = ?", userName).
Order("id DESC").
Order("last_modified DESC").
Select()
return
}
func (db *pgDB) submissionsCleaner() {
periodicity := 60 * time.Minute
for true {
time.Sleep(periodicity)
_, err := db.sql.Model(&Submission{}).
Where("book_id is null OR book_id in (SELECT id FROM books WHERE active is true)").
Where("last_modified < CURRENT_TIMESTAMP - interval ?", fmt.Sprintf("%d days", daysExpireSubmission)).
Delete()
if err != nil {
log.Error("Error deleting submissions: ", err)
}
}
}
func extractID(book *Book) interface{} {
if book == nil {
return nil
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment