A pretty basic pastes cleanup procedure.

Fixes #16.
This commit is contained in:
Stanislav Nikitin 2021-01-09 06:09:26 +05:00
parent 3ebff29113
commit 164a37d41f
Signed by: pztrn
GPG Key ID: 1E944A0F0568B550
10 changed files with 124 additions and 8 deletions

View File

@ -27,6 +27,7 @@ package database
import (
// stdlib
"database/sql"
"time"
// local
"go.dev.pztrn.name/fastpastebin/internal/database/dialects/flatfiles"
@ -44,6 +45,46 @@ type Database struct {
db dialectinterface.Interface
}
// Database cleanup function. Executes once per hour, hardcoded for now and is
// a subject of change in future.
func (db *Database) cleanup() {
for {
c.Logger.Info().Msg("Starting pastes cleanup procedure...")
pages := db.db.GetPastesPages()
var pasteIDsToRemove []int
for i := 0; i < pages; i++ {
pastes, err := db.db.GetPagedPastes(i)
if err != nil {
c.Logger.Error().Err(err).Int("page", i).Msg("Failed to perform database cleanup")
}
for _, paste := range pastes {
if paste.IsExpired() {
pasteIDsToRemove = append(pasteIDsToRemove, paste.ID)
}
}
}
for _, pasteID := range pasteIDsToRemove {
err := db.DeletePaste(pasteID)
if err != nil {
c.Logger.Error().Err(err).Int("paste", pasteID).Msg("Failed to delete paste!")
}
}
c.Logger.Info().Msg("Pastes cleanup done.")
time.Sleep(time.Hour)
}
}
func (db *Database) DeletePaste(pasteID int) error {
return db.db.DeletePaste(pasteID)
}
func (db *Database) GetDatabaseConnection() *sql.DB {
if db.db != nil {
return db.db.GetDatabaseConnection()
@ -77,6 +118,8 @@ func (db *Database) Initialize() {
} else {
c.Logger.Fatal().Str("type", c.Config.Database.Type).Msg("Unknown database type")
}
go db.cleanup()
}
func (db *Database) RegisterDialect(di dialectinterface.Interface) {

View File

@ -41,11 +41,42 @@ import (
)
type FlatFiles struct {
pastesIndex []*Index
path string
pastesIndex []Index
writeMutex sync.Mutex
}
// DeletePaste deletes paste from disk and index.
func (ff *FlatFiles) DeletePaste(pasteID int) error {
// Delete from disk.
err := os.Remove(filepath.Join(ff.path, "pastes", strconv.Itoa(pasteID)+".json"))
if err != nil {
c.Logger.Error().Err(err).Msg("Failed to delete paste!")
return err
}
// Delete from index.
ff.writeMutex.Lock()
defer ff.writeMutex.Unlock()
pasteIndex := -1
for idx, paste := range ff.pastesIndex {
if paste.ID == pasteID {
pasteIndex = idx
break
}
}
if pasteIndex != -1 {
ff.pastesIndex = append(ff.pastesIndex[:pasteIndex], ff.pastesIndex[pasteIndex+1:]...)
}
return nil
}
func (ff *FlatFiles) GetDatabaseConnection() *sql.DB {
return nil
}
@ -77,13 +108,13 @@ func (ff *FlatFiles) GetPaste(pasteID int) (*structs.Paste, error) {
func (ff *FlatFiles) GetPagedPastes(page int) ([]structs.Paste, error) {
// Pagination.
var startPagination = 0
startPagination := 0
if page > 1 {
startPagination = (page - 1) * c.Config.Pastes.Pagination
}
// Iteration one - get only public pastes.
var publicPastes []*Index
var publicPastes []Index
for _, paste := range ff.pastesIndex {
if !paste.Private {
@ -134,7 +165,7 @@ func (ff *FlatFiles) GetPagedPastes(page int) ([]structs.Paste, error) {
func (ff *FlatFiles) GetPastesPages() int {
// Get public pastes count.
var publicPastes []*Index
var publicPastes []Index
ff.writeMutex.Lock()
for _, paste := range ff.pastesIndex {
@ -192,7 +223,7 @@ func (ff *FlatFiles) Initialize() {
}
// Load pastes index.
ff.pastesIndex = []*Index{}
ff.pastesIndex = []Index{}
if _, err := os.Stat(filepath.Join(ff.path, "pastes", "index.json")); err != nil {
c.Logger.Warn().Msg("Pastes index file does not exist, will create new one")
} else {
@ -232,7 +263,7 @@ func (ff *FlatFiles) SavePaste(p *structs.Paste) (int64, error) {
}
// Add it to cache.
indexData := &Index{}
indexData := Index{}
indexData.ID = pasteID
indexData.Private = p.Private
ff.pastesIndex = append(ff.pastesIndex, indexData)

View File

@ -34,6 +34,10 @@ import (
type Handler struct{}
func (dbh Handler) DeletePaste(pasteID int) error {
return f.DeletePaste(pasteID)
}
func (dbh Handler) GetDatabaseConnection() *sql.DB {
return f.GetDatabaseConnection()
}

View File

@ -33,6 +33,7 @@ import (
)
type Interface interface {
DeletePaste(int) error
GetDatabaseConnection() *sql.DB
GetPaste(pasteID int) (*structs.Paste, error)
GetPagedPastes(page int) ([]structs.Paste, error)

View File

@ -34,6 +34,10 @@ import (
type Handler struct{}
func (dbh Handler) DeletePaste(pasteID int) error {
return d.DeletePaste(pasteID)
}
func (dbh Handler) GetDatabaseConnection() *sql.DB {
return d.GetDatabaseConnection()
}

View File

@ -56,6 +56,18 @@ func (db *Database) check() {
}
}
// DeletePaste deletes paste from database.
func (db *Database) DeletePaste(pasteID int) error {
db.check()
_, err := db.db.Exec(db.db.Rebind("DELETE FROM pastes WHERE id=?"), pasteID)
if err != nil {
return err
}
return nil
}
func (db *Database) GetDatabaseConnection() *sql.DB {
db.check()
@ -89,7 +101,7 @@ func (db *Database) GetPagedPastes(page int) ([]structs.Paste, error) {
)
// Pagination.
var startPagination = 0
startPagination := 0
if page > 1 {
startPagination = (page - 1) * c.Config.Pastes.Pagination
}

View File

@ -34,6 +34,10 @@ import (
type Handler struct{}
func (dbh Handler) DeletePaste(pasteID int) error {
return d.DeletePaste(pasteID)
}
func (dbh Handler) GetDatabaseConnection() *sql.DB {
return d.GetDatabaseConnection()
}

View File

@ -58,6 +58,18 @@ func (db *Database) check() {
}
}
// DeletePaste deletes paste from database.
func (db *Database) DeletePaste(pasteID int) error {
db.check()
_, err := db.db.Exec(db.db.Rebind("DELETE FROM pastes WHERE id=?"), pasteID)
if err != nil {
return err
}
return nil
}
func (db *Database) GetDatabaseConnection() *sql.DB {
db.check()
@ -98,7 +110,7 @@ func (db *Database) GetPagedPastes(page int) ([]structs.Paste, error) {
)
// Pagination.
var startPagination = 0
startPagination := 0
if page > 1 {
startPagination = (page - 1) * c.Config.Pastes.Pagination
}

View File

@ -38,6 +38,10 @@ import (
// to Database structure.
type Handler struct{}
func (dbh Handler) DeletePaste(pasteID int) error {
return d.DeletePaste(pasteID)
}
func (dbh Handler) GetDatabaseConnection() *sql.DB {
return d.GetDatabaseConnection()
}

View File

@ -36,6 +36,7 @@ import (
// Interface represents database interface which is available to all
// parts of application and registers with context.Context.
type Interface interface {
DeletePaste(int) error
GetDatabaseConnection() *sql.DB
GetPaste(pasteID int) (*structs.Paste, error)
GetPagedPastes(page int) ([]structs.Paste, error)