mirror of
https://github.com/lus/pasty.git
synced 2023-08-10 21:13:09 +03:00
implement cleanup task
This commit is contained in:
parent
5b0ba721b8
commit
e8a88e21ae
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"github.com/lus/pasty/internal/cleanup"
|
||||||
"github.com/lus/pasty/internal/config"
|
"github.com/lus/pasty/internal/config"
|
||||||
"github.com/lus/pasty/internal/meta"
|
"github.com/lus/pasty/internal/meta"
|
||||||
"github.com/lus/pasty/internal/reports"
|
"github.com/lus/pasty/internal/reports"
|
||||||
@ -83,6 +84,21 @@ func main() {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
// Schedule the cleanup task if configured
|
||||||
|
if cfg.Cleanup.Enabled {
|
||||||
|
task := &cleanup.Task{
|
||||||
|
Interval: cfg.Cleanup.TaskInterval,
|
||||||
|
MaxPasteAge: cfg.Cleanup.PasteLifetime,
|
||||||
|
Repository: driver.Pastes(),
|
||||||
|
}
|
||||||
|
log.Info().Msg("Scheduling the cleanup task...")
|
||||||
|
task.Start()
|
||||||
|
defer func() {
|
||||||
|
log.Info().Msg("Shutting down the cleanup task...")
|
||||||
|
task.Stop()
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
// Start the web server
|
// Start the web server
|
||||||
log.Info().Str("address", cfg.Address).Msg("Starting the web server...")
|
log.Info().Str("address", cfg.Address).Msg("Starting the web server...")
|
||||||
webServer := &web.Server{
|
webServer := &web.Server{
|
||||||
|
48
internal/cleanup/task.go
Normal file
48
internal/cleanup/task.go
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
package cleanup
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"github.com/lus/pasty/internal/pastes"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Task struct {
|
||||||
|
Interval time.Duration
|
||||||
|
MaxPasteAge time.Duration
|
||||||
|
Repository pastes.Repository
|
||||||
|
|
||||||
|
running bool
|
||||||
|
stop chan struct{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (task *Task) Start() {
|
||||||
|
if task.running {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
task.stop = make(chan struct{}, 1)
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-time.After(task.Interval):
|
||||||
|
n, err := task.Repository.DeleteOlderThan(context.Background(), task.MaxPasteAge)
|
||||||
|
if err != nil {
|
||||||
|
log.Err(err).Msg("Could not clean up expired pastes.")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
log.Debug().Int("amount", n).Msg("Cleaned up expired pastes.")
|
||||||
|
case <-task.stop:
|
||||||
|
task.running = false
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
task.running = true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (task *Task) Stop() {
|
||||||
|
if !task.running {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
close(task.stop)
|
||||||
|
}
|
@ -31,6 +31,9 @@ var keyRedirects = map[string][]string{
|
|||||||
"PASTY_REPORTS_ENABLED": {"PASTY_REPORTS_ENABLED"},
|
"PASTY_REPORTS_ENABLED": {"PASTY_REPORTS_ENABLED"},
|
||||||
"PASTY_REPORTS_WEBHOOK_URL": {"PASTY_REPORT_WEBHOOK"},
|
"PASTY_REPORTS_WEBHOOK_URL": {"PASTY_REPORT_WEBHOOK"},
|
||||||
"PASTY_REPORTS_WEBHOOK_TOKEN": {"PASTY_REPORT_WEBHOOK_TOKEN"},
|
"PASTY_REPORTS_WEBHOOK_TOKEN": {"PASTY_REPORT_WEBHOOK_TOKEN"},
|
||||||
|
"PASTY_CLEANUP_ENABLED": {"PASTY_AUTODELETE"},
|
||||||
|
"PASTY_CLEANUP_PASTE_LIFETIME": {"PASTY_AUTODELETE_LIFETIME"},
|
||||||
|
"PASTY_CLEANUP_TASK_INTERVAL": {"PASTY_AUTODELETE_TASK_INTERVAL"},
|
||||||
"PASTY_MODIFICATION_TOKENS_ENABLED": {"PASTY_MODIFICATION_TOKENS", "PASTY_DELETION_TOKENS"},
|
"PASTY_MODIFICATION_TOKENS_ENABLED": {"PASTY_MODIFICATION_TOKENS", "PASTY_DELETION_TOKENS"},
|
||||||
"PASTY_MODIFICATION_TOKEN_CHARSET": {"PASTY_MODIFICATION_TOKEN_CHARACTERS"},
|
"PASTY_MODIFICATION_TOKEN_CHARSET": {"PASTY_MODIFICATION_TOKEN_CHARACTERS"},
|
||||||
"PASTY_MODIFICATION_TOKEN_MASTER": {"PASTY_DELETION_TOKEN_MASTER"},
|
"PASTY_MODIFICATION_TOKEN_MASTER": {"PASTY_DELETION_TOKEN_MASTER"},
|
||||||
|
@ -7,27 +7,27 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
LogLevel string `default:"info" split_words:"true"`
|
LogLevel string `default:"info" split_words:"true"`
|
||||||
Address string `default:":8080" split_words:"true"`
|
Address string `default:":8080" split_words:"true"`
|
||||||
StorageDriver string `default:"sqlite" split_words:"true"`
|
StorageDriver string `default:"sqlite" split_words:"true"`
|
||||||
PasteIDLength int `default:"6" split_words:"true"`
|
PasteIDLength int `default:"6" split_words:"true"`
|
||||||
PasteIDCharset string `default:"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" split_words:"true"`
|
PasteIDCharset string `default:"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" split_words:"true"`
|
||||||
ModificationTokensEnabled bool `default:"true" split_words:"true"`
|
ModificationTokensEnabled bool `default:"true" split_words:"true"`
|
||||||
ModificationTokenMaster string `split_words:"true"`
|
ModificationTokenMaster string `split_words:"true"`
|
||||||
ModificationTokenLength int `default:"12" split_words:"true"`
|
ModificationTokenLength int `default:"12" split_words:"true"`
|
||||||
ModificationTokenCharset string `default:"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" split_words:"true"`
|
ModificationTokenCharset string `default:"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" split_words:"true"`
|
||||||
RateLimit string `default:"30-M" split_words:"true"`
|
RateLimit string `default:"30-M" split_words:"true"`
|
||||||
PasteLengthCap int `default:"50000" split_words:"true"`
|
PasteLengthCap int `default:"50000" split_words:"true"`
|
||||||
AutoDelete *AutoDeleteConfig `split_words:"true"`
|
Cleanup *CleanupConfig
|
||||||
Reports *ReportConfig
|
Reports *ReportConfig
|
||||||
Postgres *PostgresConfig
|
Postgres *PostgresConfig
|
||||||
SQLite *SQLiteConfig
|
SQLite *SQLiteConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
type AutoDeleteConfig struct {
|
type CleanupConfig struct {
|
||||||
Enabled bool `default:"false"`
|
Enabled bool `default:"false"`
|
||||||
Lifetime time.Duration `default:"720h"`
|
PasteLifetime time.Duration `default:"720h" split_words:"true"`
|
||||||
TaskInterval time.Duration `default:"5m" split_words:"true"`
|
TaskInterval time.Duration `default:"5m" split_words:"true"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ReportConfig struct {
|
type ReportConfig struct {
|
||||||
|
Loading…
Reference in New Issue
Block a user