mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 20:34:37 +03:00
Add database optimise task (#3929)
* Add database optimise task * Wrap errors * US internationalisation --------- Co-authored-by: WithoutPants <53250216+WithoutPants@users.noreply.github.com>
This commit is contained in:
@@ -208,3 +208,8 @@ func (r *mutationResolver) AnonymiseDatabase(ctx context.Context, input Anonymis
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (r *mutationResolver) OptimiseDatabase(ctx context.Context) (string, error) {
|
||||
jobID := manager.GetInstance().OptimiseDatabase(ctx)
|
||||
return strconv.Itoa(jobID), nil
|
||||
}
|
||||
|
||||
@@ -265,6 +265,14 @@ func (s *Manager) Clean(ctx context.Context, input CleanMetadataInput) int {
|
||||
return s.JobManager.Add(ctx, "Cleaning...", &j)
|
||||
}
|
||||
|
||||
func (s *Manager) OptimiseDatabase(ctx context.Context) int {
|
||||
j := OptimiseDatabaseJob{
|
||||
Optimiser: s.Database,
|
||||
}
|
||||
|
||||
return s.JobManager.Add(ctx, "Optimising database...", &j)
|
||||
}
|
||||
|
||||
func (s *Manager) MigrateHash(ctx context.Context) int {
|
||||
j := job.MakeJobExec(func(ctx context.Context, progress *job.Progress) {
|
||||
fileNamingAlgo := config.GetInstance().GetVideoFileNamingAlgorithm()
|
||||
|
||||
56
internal/manager/task_optimise.go
Normal file
56
internal/manager/task_optimise.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package manager
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/stashapp/stash/pkg/job"
|
||||
"github.com/stashapp/stash/pkg/logger"
|
||||
)
|
||||
|
||||
type Optimiser interface {
|
||||
Analyze(ctx context.Context) error
|
||||
Vacuum(ctx context.Context) error
|
||||
}
|
||||
|
||||
type OptimiseDatabaseJob struct {
|
||||
Optimiser Optimiser
|
||||
}
|
||||
|
||||
func (j *OptimiseDatabaseJob) Execute(ctx context.Context, progress *job.Progress) {
|
||||
logger.Info("Optimising database")
|
||||
progress.SetTotal(2)
|
||||
|
||||
start := time.Now()
|
||||
|
||||
var err error
|
||||
|
||||
progress.ExecuteTask("Analyzing database", func() {
|
||||
err = j.Optimiser.Analyze(ctx)
|
||||
progress.Increment()
|
||||
})
|
||||
if job.IsCancelled(ctx) {
|
||||
logger.Info("Stopping due to user request")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
logger.Errorf("Error analyzing database: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
progress.ExecuteTask("Vacuuming database", func() {
|
||||
err = j.Optimiser.Vacuum(ctx)
|
||||
progress.Increment()
|
||||
})
|
||||
if job.IsCancelled(ctx) {
|
||||
logger.Info("Stopping due to user request")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
logger.Errorf("Error vacuuming database: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
elapsed := time.Since(start)
|
||||
logger.Infof("Finished optimising database after %s", elapsed)
|
||||
}
|
||||
Reference in New Issue
Block a user