2018-09-24 09:53:16 +02:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2019-06-03 22:58:15 +02:00
|
|
|
"context"
|
2019-06-30 05:38:39 +02:00
|
|
|
"fmt"
|
2019-06-03 22:58:15 +02:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2019-06-20 01:11:03 +02:00
|
|
|
"strconv"
|
2019-06-03 22:58:15 +02:00
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2021-09-22 19:33:41 +02:00
|
|
|
"github.com/sevlyar/go-daemon"
|
|
|
|
"github.com/urfave/cli"
|
2020-12-17 18:24:55 +01:00
|
|
|
|
2021-09-22 19:33:41 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/auto"
|
2022-09-28 09:01:17 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/mutex"
|
2021-09-22 19:33:41 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
2018-09-24 09:53:16 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/server"
|
2022-10-03 22:59:29 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/session"
|
2020-04-06 09:41:42 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/workers"
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2020-01-13 11:07:09 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2023-03-15 17:05:05 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/report"
|
2018-09-24 09:53:16 +02:00
|
|
|
)
|
|
|
|
|
2023-02-14 11:37:22 +01:00
|
|
|
// StartCommand configures the command name, flags, and action.
|
2018-09-24 09:53:16 +02:00
|
|
|
var StartCommand = cli.Command{
|
2020-01-31 17:29:55 +01:00
|
|
|
Name: "start",
|
2020-01-31 15:29:06 +01:00
|
|
|
Aliases: []string{"up"},
|
2022-10-19 05:09:09 +02:00
|
|
|
Usage: "Starts the Web server",
|
2020-01-31 17:29:55 +01:00
|
|
|
Flags: startFlags,
|
|
|
|
Action: startAction,
|
2018-09-24 09:53:16 +02:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// startFlags specifies the start command parameters.
|
2018-09-24 09:53:16 +02:00
|
|
|
var startFlags = []cli.Flag{
|
2019-06-30 05:38:39 +02:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "detach-server, d",
|
|
|
|
Usage: "detach from the console (daemon mode)",
|
|
|
|
EnvVar: "PHOTOPRISM_DETACH_SERVER",
|
|
|
|
},
|
2019-06-20 01:11:03 +02:00
|
|
|
cli.BoolFlag{
|
2019-12-11 14:10:20 +01:00
|
|
|
Name: "config, c",
|
|
|
|
Usage: "show config",
|
2019-06-20 01:11:03 +02:00
|
|
|
},
|
2018-09-24 09:53:16 +02:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// startAction starts the web server and initializes the daemon.
|
2018-11-17 06:21:39 +01:00
|
|
|
func startAction(ctx *cli.Context) error {
|
2022-10-04 12:27:40 +02:00
|
|
|
conf, err := InitConfig(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-30 05:38:39 +02:00
|
|
|
|
|
|
|
if ctx.IsSet("config") {
|
2022-10-19 05:09:09 +02:00
|
|
|
// Create config report.
|
|
|
|
cols := []string{"Name", "Value"}
|
|
|
|
rows := [][]string{
|
|
|
|
{"detach-server", fmt.Sprintf("%t", conf.DetachServer())},
|
|
|
|
{"http-mode", conf.HttpMode()},
|
|
|
|
{"http-compression", conf.HttpCompression()},
|
2023-03-20 20:37:07 +01:00
|
|
|
{"http-cache-maxage", fmt.Sprintf("%d", conf.HttpCacheMaxAge())},
|
2023-03-20 11:40:46 +01:00
|
|
|
{"http-cache-public", fmt.Sprintf("%t", conf.HttpCachePublic())},
|
2022-10-19 05:09:09 +02:00
|
|
|
{"http-host", conf.HttpHost()},
|
|
|
|
{"http-port", fmt.Sprintf("%d", conf.HttpPort())},
|
|
|
|
}
|
2019-07-02 18:25:46 +02:00
|
|
|
|
2022-10-19 05:09:09 +02:00
|
|
|
// Render config report.
|
|
|
|
opt := report.Options{Format: report.CliFormat(ctx), NoWrap: true}
|
|
|
|
result, _ := report.Render(rows, cols, opt)
|
|
|
|
fmt.Printf("\n%s\n", result)
|
2019-06-30 05:38:39 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-18 09:11:42 +01:00
|
|
|
if conf.HttpPort() < 1 || conf.HttpPort() > 65535 {
|
2019-06-30 05:38:39 +02:00
|
|
|
log.Fatal("server port must be a number between 1 and 65535")
|
2018-09-24 09:53:16 +02:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Pass this context down the chain.
|
2020-10-08 08:52:03 +02:00
|
|
|
cctx, cancel := context.WithCancel(context.Background())
|
2018-09-24 09:53:16 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Initialize the index database.
|
2020-04-30 20:07:03 +02:00
|
|
|
conf.InitDb()
|
2018-09-24 09:53:16 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Check if the daemon is running, if not, initialize the daemon.
|
2019-06-20 01:11:03 +02:00
|
|
|
dctx := new(daemon.Context)
|
2019-06-30 05:38:39 +02:00
|
|
|
dctx.LogFileName = conf.LogFilename()
|
|
|
|
dctx.PidFileName = conf.PIDFilename()
|
2019-06-20 01:11:03 +02:00
|
|
|
dctx.Args = ctx.Args()
|
2019-06-30 05:38:39 +02:00
|
|
|
|
|
|
|
if !daemon.WasReborn() && conf.DetachServer() {
|
2019-06-20 01:11:03 +02:00
|
|
|
conf.Shutdown()
|
|
|
|
cancel()
|
|
|
|
|
2019-06-30 05:38:39 +02:00
|
|
|
if pid, ok := childAlreadyRunning(conf.PIDFilename()); ok {
|
|
|
|
log.Infof("daemon already running with process id %v\n", pid)
|
2019-06-20 01:11:03 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
child, err := dctx.Reborn()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if child != nil {
|
2020-01-12 14:00:56 +01:00
|
|
|
if !fs.Overwrite(conf.PIDFilename(), []byte(strconv.Itoa(child.Pid))) {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Fatalf("failed writing process id to %s", clean.Log(conf.PIDFilename()))
|
2019-06-20 01:11:03 +02:00
|
|
|
}
|
|
|
|
|
2019-06-30 05:38:39 +02:00
|
|
|
log.Infof("daemon started with process id %v\n", child.Pid)
|
2019-06-20 01:11:03 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2019-06-30 05:38:39 +02:00
|
|
|
|
2019-07-02 23:01:56 +02:00
|
|
|
if conf.ReadOnly() {
|
2023-02-09 13:14:56 +01:00
|
|
|
log.Infof("config: enabled read-only mode")
|
2019-07-02 23:01:56 +02:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Start web server.
|
2019-06-03 22:58:15 +02:00
|
|
|
go server.Start(cctx, conf)
|
2018-09-24 09:53:16 +02:00
|
|
|
|
2021-04-20 08:40:39 +02:00
|
|
|
if count, err := photoprism.RestoreAlbums(conf.AlbumsPath(), false); err != nil {
|
2020-12-17 18:24:55 +01:00
|
|
|
log.Errorf("restore: %s", err)
|
|
|
|
} else if count > 0 {
|
|
|
|
log.Infof("%d albums restored", count)
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Start background workers.
|
2022-10-03 22:59:29 +02:00
|
|
|
session.Monitor(time.Hour)
|
2020-04-06 09:41:42 +02:00
|
|
|
workers.Start(conf)
|
2021-01-02 18:56:15 +01:00
|
|
|
auto.Start(conf)
|
2020-04-03 18:08:49 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Wait for signal to initiate server shutdown.
|
2019-06-03 22:58:15 +02:00
|
|
|
quit := make(chan os.Signal)
|
2023-03-27 21:21:34 +02:00
|
|
|
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM, syscall.SIGUSR1)
|
2018-09-24 09:53:16 +02:00
|
|
|
|
2023-03-27 21:21:34 +02:00
|
|
|
sig := <-quit
|
2020-04-06 09:41:42 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Stop all background activity.
|
2021-01-02 18:56:15 +01:00
|
|
|
auto.Stop()
|
2022-09-28 09:01:17 +02:00
|
|
|
workers.Stop()
|
2022-10-03 22:59:29 +02:00
|
|
|
session.Shutdown()
|
2022-09-28 09:01:17 +02:00
|
|
|
mutex.CancelAll()
|
2020-04-06 09:41:42 +02:00
|
|
|
|
2019-06-29 23:27:42 +02:00
|
|
|
log.Info("shutting down...")
|
2019-06-03 22:58:15 +02:00
|
|
|
cancel()
|
2019-06-30 05:38:39 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if err := dctx.Release(); err != nil {
|
2019-06-20 01:11:03 +02:00
|
|
|
log.Error(err)
|
|
|
|
}
|
2019-06-30 05:38:39 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Finally, close the DB connection after a short grace period.
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
conf.Shutdown()
|
2019-06-30 05:38:39 +02:00
|
|
|
|
2023-03-27 21:21:34 +02:00
|
|
|
// Don't exit with 0 if SIGUSR1 was received to avoid restarts.
|
|
|
|
if sig == syscall.SIGUSR1 {
|
|
|
|
os.Exit(1)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-24 09:53:16 +02:00
|
|
|
return nil
|
|
|
|
}
|