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"
|
2021-11-20 19:14:00 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2020-12-05 00:13:44 +01:00
|
|
|
"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{
|
2021-11-18 03:47:25 +01:00
|
|
|
Name: "optimize",
|
2021-11-20 19:25:57 +01:00
|
|
|
Usage: "Maintains titles, estimates, and other metadata",
|
2021-11-18 03:47:25 +01:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "force, f",
|
2021-11-20 19:25:57 +01:00
|
|
|
Usage: "update all, including recently optimized",
|
2021-11-18 03:47:25 +01:00
|
|
|
},
|
|
|
|
},
|
2020-12-05 00:13:44 +01:00
|
|
|
Action: optimizeAction,
|
|
|
|
}
|
|
|
|
|
2021-11-20 19:25:57 +01:00
|
|
|
// optimizeAction updates titles, estimates, and other metadata.
|
2020-12-05 00:13:44 +01:00
|
|
|
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-11-18 03:02:10 +01:00
|
|
|
log.Infof("config: read-only mode enabled")
|
2020-12-05 00:13:44 +01:00
|
|
|
}
|
|
|
|
|
2021-11-18 03:47:25 +01:00
|
|
|
force := ctx.Bool("force")
|
2020-12-05 00:13:44 +01:00
|
|
|
worker := workers.NewMeta(conf)
|
|
|
|
|
2021-11-20 19:14:00 +01:00
|
|
|
delay := 15 * time.Second
|
|
|
|
interval := entity.MetadataUpdateInterval
|
|
|
|
|
|
|
|
if force {
|
|
|
|
delay = 0
|
|
|
|
interval = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := worker.Start(delay, interval, force); err != nil {
|
2020-12-05 00:13:44 +01:00
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
2021-10-02 15:34:41 +02:00
|
|
|
log.Infof("completed in %s", elapsed)
|
2020-12-05 00:13:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
conf.Shutdown()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|