chore: print a message periodically indicating that VACUUM is still being executed (#838)

This commit is contained in:
richΛrd 2023-10-30 09:22:50 -04:00 committed by GitHub
parent db222a24ef
commit 279752344f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 6 deletions

View File

@ -1,8 +1,10 @@
package postgres package postgres
import ( import (
"context"
"database/sql" "database/sql"
"fmt" "fmt"
"time"
"github.com/golang-migrate/migrate/v4/database" "github.com/golang-migrate/migrate/v4/database"
"github.com/golang-migrate/migrate/v4/database/pgx" "github.com/golang-migrate/migrate/v4/database/pgx"
@ -15,10 +17,35 @@ import (
func executeVacuum(db *sql.DB, logger *zap.Logger) error { func executeVacuum(db *sql.DB, logger *zap.Logger) error {
logger.Info("starting PostgreSQL database vacuuming") logger.Info("starting PostgreSQL database vacuuming")
_, err := db.Exec("VACUUM FULL")
if err != nil { ctx, cancel := context.WithCancel(context.Background())
return err defer cancel()
errCh := make(chan error)
go func() {
defer cancel()
_, err := db.Exec("VACUUM FULL")
if err != nil {
errCh <- err
}
}()
t := time.NewTicker(2 * time.Minute)
defer t.Stop()
loop:
for {
select {
case <-ctx.Done():
break loop
case err := <-errCh:
return err
case <-t.C:
logger.Info("still vacuuming...")
}
} }
logger.Info("finished PostgreSQL database vacuuming") logger.Info("finished PostgreSQL database vacuuming")
return nil return nil
} }

View File

@ -1,9 +1,11 @@
package sqlite package sqlite
import ( import (
"context"
"database/sql" "database/sql"
"fmt" "fmt"
"strings" "strings"
"time"
"github.com/golang-migrate/migrate/v4/database" "github.com/golang-migrate/migrate/v4/database"
"github.com/golang-migrate/migrate/v4/database/sqlite3" "github.com/golang-migrate/migrate/v4/database/sqlite3"
@ -32,10 +34,35 @@ func addSqliteURLDefaults(dburl string) string {
func executeVacuum(db *sql.DB, logger *zap.Logger) error { func executeVacuum(db *sql.DB, logger *zap.Logger) error {
logger.Info("starting sqlite database vacuuming") logger.Info("starting sqlite database vacuuming")
_, err := db.Exec("VACUUM")
if err != nil { ctx, cancel := context.WithCancel(context.Background())
return err defer cancel()
errCh := make(chan error)
go func() {
defer cancel()
_, err := db.Exec("VACUUM")
if err != nil {
errCh <- err
}
}()
t := time.NewTicker(2 * time.Minute)
defer t.Stop()
loop:
for {
select {
case <-ctx.Done():
break loop
case err := <-errCh:
return err
case <-t.C:
logger.Info("still vacuuming...")
}
} }
logger.Info("finished sqlite database vacuuming") logger.Info("finished sqlite database vacuuming")
return nil return nil
} }