2020-12-05 00:13:44 +01:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2021-09-22 19:33:41 +02:00
|
|
|
"github.com/urfave/cli"
|
2020-12-05 00:13:44 +01:00
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
2021-09-22 19:33:41 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/workers"
|
2020-12-05 00:13:44 +01:00
|
|
|
)
|
|
|
|
|
2021-01-24 17:46:18 +01:00
|
|
|
// OptimizeCommand registers the index cli command.
|
2020-12-05 00:13:44 +01:00
|
|
|
var OptimizeCommand = cli.Command{
|
|
|
|
Name: "optimize",
|
|
|
|
Usage: "Starts metadata check and optimization",
|
|
|
|
Action: optimizeAction,
|
|
|
|
}
|
|
|
|
|
|
|
|
// optimizeAction starts metadata check and optimization.
|
|
|
|
func optimizeAction(ctx *cli.Context) error {
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
conf := config.NewConfig(ctx)
|
|
|
|
service.SetConfig(conf)
|
|
|
|
|
|
|
|
_, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := conf.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.InitDb()
|
|
|
|
|
|
|
|
if conf.ReadOnly() {
|
2021-01-24 17:46:18 +01:00
|
|
|
log.Infof("optimize: read-only mode enabled")
|
2020-12-05 00:13:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
worker := workers.NewMeta(conf)
|
|
|
|
|
2020-12-11 22:09:11 +01:00
|
|
|
if err := worker.Start(time.Second * 15); err != nil {
|
2020-12-05 00:13:44 +01:00
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
|
|
|
log.Infof("completed in %s", elapsed)
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.Shutdown()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|