mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-26 06:37:07 +00:00
fix(metadata): wait for the aggregation worker before closing the store
This commit is contained in:
@@ -0,0 +1,58 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/lukaszraczylo/gohoarder/pkg/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
func listTree(t *testing.T, dir string) string {
|
||||||
|
t.Helper()
|
||||||
|
var names []string
|
||||||
|
err := filepath.Walk(dir, func(p string, _ os.FileInfo, err error) error {
|
||||||
|
if err == nil {
|
||||||
|
names = append(names, p)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
return strings.Join(names, "\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Shutdown must leave nothing running against the data directory: a late SQLite close deletes
|
||||||
|
// its -wal/-shm files after Shutdown returned and breaks the removal of the directory.
|
||||||
|
func TestShutdownLeavesDataDirectoryStable(t *testing.T) {
|
||||||
|
for i := 0; i < 40; i++ {
|
||||||
|
dir := t.TempDir()
|
||||||
|
cfg := config.Default()
|
||||||
|
cfg.Storage.Backend = "filesystem"
|
||||||
|
cfg.Storage.Path = filepath.Join(dir, "store")
|
||||||
|
cfg.Metadata.Backend = "sqlite"
|
||||||
|
cfg.Metadata.SQLite.Path = filepath.Join(dir, "meta", "meta.db")
|
||||||
|
if err := os.MkdirAll(filepath.Dir(cfg.Metadata.SQLite.Path), 0o750); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
cfg.Security.Enabled = false
|
||||||
|
cfg.Prewarming.Enabled = false
|
||||||
|
cfg.Auth.Enabled = false
|
||||||
|
a, err := New(cfg)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("New: %v", err)
|
||||||
|
}
|
||||||
|
doRequest(t, a, "/api/stats", nil)
|
||||||
|
if err := a.Shutdown(); err != nil {
|
||||||
|
t.Fatalf("Shutdown: %v", err)
|
||||||
|
}
|
||||||
|
before := listTree(t, dir)
|
||||||
|
time.Sleep(20 * time.Millisecond)
|
||||||
|
if after := listTree(t, dir); before != after {
|
||||||
|
t.Fatalf("iteration %d: data directory changed after Shutdown:\nbefore:\n%s\nafter:\n%s", i, before, after)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package gormstore
|
package gormstore
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
@@ -17,6 +18,8 @@ type AggregationWorker struct {
|
|||||||
stopChan chan struct{}
|
stopChan chan struct{}
|
||||||
ticker *time.Ticker
|
ticker *time.Ticker
|
||||||
now func() time.Time // nil means time.Now
|
now func() time.Time // nil means time.Now
|
||||||
|
wg sync.WaitGroup
|
||||||
|
stopOnce sync.Once
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewAggregationWorker creates a new aggregation worker
|
// NewAggregationWorker creates a new aggregation worker
|
||||||
@@ -36,6 +39,15 @@ func (w *AggregationWorker) clock() time.Time {
|
|||||||
return time.Now()
|
return time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StartAsync runs Start in a goroutine that Stop waits for.
|
||||||
|
func (w *AggregationWorker) StartAsync() {
|
||||||
|
w.wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer w.wg.Done()
|
||||||
|
w.Start()
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
// Start begins the aggregation worker
|
// Start begins the aggregation worker
|
||||||
func (w *AggregationWorker) Start() {
|
func (w *AggregationWorker) Start() {
|
||||||
log.Info().Msg("Starting aggregation worker")
|
log.Info().Msg("Starting aggregation worker")
|
||||||
@@ -68,9 +80,10 @@ func (w *AggregationWorker) Start() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop stops the aggregation worker
|
// Stop stops the aggregation worker and waits for a started one to exit. Safe to call repeatedly.
|
||||||
func (w *AggregationWorker) Stop() {
|
func (w *AggregationWorker) Stop() {
|
||||||
close(w.stopChan)
|
w.stopOnce.Do(func() { close(w.stopChan) })
|
||||||
|
w.wg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
// AggregateHourly aggregates download events into hourly stats
|
// AggregateHourly aggregates download events into hourly stats
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package gormstore
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestCloseIsIdempotent(t *testing.T) {
|
||||||
|
store := openFileStore(t, true)
|
||||||
|
if err := store.Close(); err != nil {
|
||||||
|
t.Fatalf("first Close: %v", err)
|
||||||
|
}
|
||||||
|
if err := store.Close(); err != nil {
|
||||||
|
t.Fatalf("second Close: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -128,7 +128,7 @@ func NewV2(cfg Config) (*GORMStoreV2, error) {
|
|||||||
// Start aggregation worker (skip for in-memory databases used in tests)
|
// Start aggregation worker (skip for in-memory databases used in tests)
|
||||||
if !strings.Contains(cfg.DSN, ":memory:") {
|
if !strings.Contains(cfg.DSN, ":memory:") {
|
||||||
store.aggregationWorker = NewAggregationWorker(db)
|
store.aggregationWorker = NewAggregationWorker(db)
|
||||||
go store.aggregationWorker.Start()
|
store.aggregationWorker.StartAsync()
|
||||||
} else {
|
} else {
|
||||||
// For tests: create worker but don't start it
|
// For tests: create worker but don't start it
|
||||||
store.aggregationWorker = NewAggregationWorker(db)
|
store.aggregationWorker = NewAggregationWorker(db)
|
||||||
|
|||||||
Reference in New Issue
Block a user