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
|
|
|
|
2021-11-20 19:14:00 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
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()
|
|
|
|
|
2022-10-04 12:27:40 +02:00
|
|
|
conf, err := InitConfig(ctx)
|
2020-12-05 00:13:44 +01:00
|
|
|
|
|
|
|
_, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2022-10-04 12:27:40 +02:00
|
|
|
if err != nil {
|
2020-12-05 00:13:44 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.InitDb()
|
2022-09-28 09:01:17 +02:00
|
|
|
defer conf.Shutdown()
|
2020-12-05 00:13:44 +01:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|