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-01-02 18:56:15 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/auto"
|
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2018-09-24 09:53:16 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/server"
|
2020-04-05 22:26:53 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
2020-04-06 09:41:42 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/workers"
|
2020-01-13 11:07:09 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2020-05-03 18:00:50 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2019-06-30 05:38:39 +02:00
|
|
|
"github.com/sevlyar/go-daemon"
|
2018-09-24 09:53:16 +02:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
2021-01-24 17:46:18 +01:00
|
|
|
// StartCommand registers the start cli command.
|
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"},
|
2020-01-31 17:29:55 +01:00
|
|
|
Usage: "Starts web server",
|
|
|
|
Flags: startFlags,
|
|
|
|
Action: startAction,
|
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
|
|
|
}
|
|
|
|
|
2020-02-18 23:42:51 +01:00
|
|
|
// startAction start the web server and initializes the daemon
|
2018-11-17 06:21:39 +01:00
|
|
|
func startAction(ctx *cli.Context) error {
|
2019-06-30 05:38:39 +02:00
|
|
|
conf := config.NewConfig(ctx)
|
2020-04-05 22:26:53 +02:00
|
|
|
service.SetConfig(conf)
|
2019-06-30 05:38:39 +02:00
|
|
|
|
|
|
|
if ctx.IsSet("config") {
|
|
|
|
fmt.Printf("NAME VALUE\n")
|
|
|
|
fmt.Printf("detach-server %t\n", conf.DetachServer())
|
|
|
|
|
2020-12-18 09:11:42 +01:00
|
|
|
fmt.Printf("http-host %s\n", conf.HttpHost())
|
|
|
|
fmt.Printf("http-port %d\n", conf.HttpPort())
|
|
|
|
fmt.Printf("http-mode %s\n", conf.HttpMode())
|
2019-07-02 18:25:46 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-10-08 08:52:03 +02:00
|
|
|
// pass this context down the chain
|
|
|
|
cctx, cancel := context.WithCancel(context.Background())
|
2018-09-24 09:53:16 +02:00
|
|
|
|
2020-10-08 08:52:03 +02:00
|
|
|
if err := conf.Init(); err != nil {
|
2019-06-03 22:58:15 +02:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-04-13 18:08:21 +02:00
|
|
|
|
2020-02-18 23:42:51 +01:00
|
|
|
// initialize the database
|
2020-04-30 20:07:03 +02:00
|
|
|
conf.InitDb()
|
2018-09-24 09:53:16 +02:00
|
|
|
|
2020-02-18 23:42:51 +01:00
|
|
|
// check if 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))) {
|
2020-05-03 18:00:50 +02:00
|
|
|
log.Fatalf("failed writing process id to %s", txt.Quote(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() {
|
2021-01-24 17:46:18 +01:00
|
|
|
log.Infof("start: read-only mode enabled")
|
2019-07-02 23:01:56 +02:00
|
|
|
}
|
|
|
|
|
2020-02-18 23:42:51 +01: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
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
if count, err := photoprism.RestoreAlbums(false); err != nil {
|
|
|
|
log.Errorf("restore: %s", err)
|
|
|
|
} else if count > 0 {
|
|
|
|
log.Infof("%d albums restored", count)
|
|
|
|
}
|
|
|
|
|
2020-04-06 09:41:42 +02:00
|
|
|
// start share & sync workers
|
|
|
|
workers.Start(conf)
|
2021-01-02 18:56:15 +01:00
|
|
|
auto.Start(conf)
|
2020-04-03 18:08:49 +02:00
|
|
|
|
2020-02-18 23:42:51 +01:00
|
|
|
// set up proper shutdown of daemon and web server
|
2019-06-03 22:58:15 +02:00
|
|
|
quit := make(chan os.Signal)
|
|
|
|
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
2018-09-24 09:53:16 +02:00
|
|
|
|
2019-06-03 22:58:15 +02:00
|
|
|
<-quit
|
2020-04-06 09:41:42 +02:00
|
|
|
|
|
|
|
// stop share & sync workers
|
|
|
|
workers.Stop()
|
2021-01-02 18:56:15 +01:00
|
|
|
auto.Stop()
|
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
|
|
|
conf.Shutdown()
|
|
|
|
cancel()
|
2019-06-20 01:11:03 +02:00
|
|
|
err := dctx.Release()
|
2019-06-30 05:38:39 +02:00
|
|
|
|
2019-06-20 01:11:03 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
2019-06-30 05:38:39 +02:00
|
|
|
|
2019-06-03 22:58:15 +02:00
|
|
|
time.Sleep(3 * time.Second)
|
2019-06-30 05:38:39 +02:00
|
|
|
|
2018-09-24 09:53:16 +02:00
|
|
|
return nil
|
|
|
|
}
|