From 53531373b934a4957d87b95f36b659160fae0329 Mon Sep 17 00:00:00 2001 From: Caj Larsson Date: Wed, 4 May 2022 20:01:58 +0800 Subject: [PATCH] Delete expired files --- dataswamp/services.go | 2 -- server/bog.go | 16 ++++++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/dataswamp/services.go b/dataswamp/services.go index ec89fa3..0455b3e 100644 --- a/dataswamp/services.go +++ b/dataswamp/services.go @@ -124,9 +124,7 @@ func (s SwampFileService) CleanUpExpiredFiles() error { expiry := time.Now().Add(-ns.AllowanceDuration) dfs, err := s.swamp_file_repo.DeleteOlderThan(strconv.FormatInt(ns.ID, 10), expiry) - // panic(errors.New(fmt.Sprintf("%+v", dfs))) for _, df := range dfs { - ns.FileQuota.Remove(df.Size) } diff --git a/server/bog.go b/server/bog.go index 3f65a57..fa3bd61 100644 --- a/server/bog.go +++ b/server/bog.go @@ -4,7 +4,7 @@ import ( "fmt" "net/http" "strconv" - // "io" + "time" "caj-larsson/bog/dataswamp" "caj-larsson/bog/dataswamp/namespace" "caj-larsson/bog/dataswamp/swampfile" @@ -31,7 +31,7 @@ func buildFileDataRepository(config FileConfig) swampfile.Repository { return r } -func buildUserAgentRepository(config DatabaseConfig) namespace.Repository { +func buildNamespaceRepository(config DatabaseConfig) namespace.Repository { if config.Backend != "sqlite" { panic("Can only handle sqlite") } @@ -84,15 +84,22 @@ func (b *Bog) routes() { b.router.HandleFunc("/", b.fileHandler) } +func (b *Bog) cleanNamespaces(){ + for true { + b.file_service.CleanUpExpiredFiles() + time.Sleep(time.Minute * 10) + } +} + func New(config *Configuration) *Bog { b := new(Bog) b.address = config.bindAddress() fsSwampRepo := buildFileDataRepository(config.File) - uaRepo := buildUserAgentRepository(config.Database) + nsRepo := buildNamespaceRepository(config.Database) b.file_service = dataswamp.NewSwampFileService( - uaRepo, fsSwampRepo, config.Quota.ParsedSizeBytes(), config.Quota.ParsedDuration(), + nsRepo, fsSwampRepo, config.Quota.ParsedSizeBytes(), config.Quota.ParsedDuration(), ) b.router = new(http.ServeMux) @@ -101,5 +108,6 @@ func New(config *Configuration) *Bog { } func (b *Bog) Run() { + go b.cleanNamespaces() http.ListenAndServe(b.address, b.router) }