Manager: periodically run the SQL VACUUM
command
This commit is contained in:
parent
e7a5e0565d
commit
22ea599554
@ -75,6 +75,8 @@ func main() {
|
|||||||
|
|
||||||
// Construct the services.
|
// Construct the services.
|
||||||
persist := openDB(*configService)
|
persist := openDB(*configService)
|
||||||
|
go persist.PeriodicMaintenanceLoop(mainCtx)
|
||||||
|
|
||||||
flamenco := buildFlamencoAPI(configService, persist)
|
flamenco := buildFlamencoAPI(configService, persist)
|
||||||
e := buildWebService(flamenco, persist, ssdp)
|
e := buildWebService(flamenco, persist, ssdp)
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ package persistence
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
@ -13,6 +14,8 @@ import (
|
|||||||
"github.com/glebarez/sqlite"
|
"github.com/glebarez/sqlite"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const vacuumPeriod = 1 * time.Hour
|
||||||
|
|
||||||
// DB provides the database interface.
|
// DB provides the database interface.
|
||||||
type DB struct {
|
type DB struct {
|
||||||
gormDB *gorm.DB
|
gormDB *gorm.DB
|
||||||
@ -57,3 +60,27 @@ func openDBWithConfig(uri string, config *gorm.Config) (*DB, error) {
|
|||||||
}
|
}
|
||||||
return &db, nil
|
return &db, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PeriodicMaintenanceLoop periodically vacuums the database.
|
||||||
|
// This function only returns when the context is done.
|
||||||
|
func (db *DB) PeriodicMaintenanceLoop(ctx context.Context) {
|
||||||
|
log.Debug().Msg("periodic database maintenance loop starting")
|
||||||
|
defer log.Debug().Msg("periodic database maintenance loop stopping")
|
||||||
|
|
||||||
|
var waitTime time.Duration
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case <-time.After(waitTime):
|
||||||
|
waitTime = vacuumPeriod
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debug().Msg("vacuuming database")
|
||||||
|
tx := db.gormDB.Exec("vacuum")
|
||||||
|
if tx.Error != nil {
|
||||||
|
log.Error().Err(tx.Error).Msg("error vacuuming database")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user