2018-09-24 09:53:16 +02:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2019-06-03 22:58:15 +02:00
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2019-06-20 01:11:03 +02:00
|
|
|
"strconv"
|
2019-06-03 22:58:15 +02:00
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
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"
|
2019-06-20 01:11:03 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/util"
|
|
|
|
daemon "github.com/sevlyar/go-daemon"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2018-09-24 09:53:16 +02:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
2018-11-17 13:57:19 +01:00
|
|
|
// Starts web server (user interface)
|
2018-09-24 09:53:16 +02:00
|
|
|
var StartCommand = cli.Command{
|
|
|
|
Name: "start",
|
|
|
|
Usage: "Starts web server",
|
|
|
|
Flags: startFlags,
|
|
|
|
Action: startAction,
|
|
|
|
}
|
|
|
|
|
|
|
|
var startFlags = []cli.Flag{
|
|
|
|
cli.IntFlag{
|
2018-12-21 03:11:06 +01:00
|
|
|
Name: "http-port, p",
|
2018-09-24 09:53:16 +02:00
|
|
|
Usage: "HTTP server port",
|
|
|
|
Value: 80,
|
2018-12-21 03:11:06 +01:00
|
|
|
EnvVar: "PHOTOPRISM_HTTP_PORT",
|
2018-09-24 09:53:16 +02:00
|
|
|
},
|
|
|
|
cli.StringFlag{
|
2018-12-21 03:11:06 +01:00
|
|
|
Name: "http-host, i",
|
2018-09-24 09:53:16 +02:00
|
|
|
Usage: "HTTP server host",
|
|
|
|
Value: "",
|
2018-12-21 03:11:06 +01:00
|
|
|
EnvVar: "PHOTOPRISM_HTTP_HOST",
|
2018-09-24 09:53:16 +02:00
|
|
|
},
|
|
|
|
cli.StringFlag{
|
2018-12-21 03:11:06 +01:00
|
|
|
Name: "http-mode, m",
|
2018-09-24 09:53:16 +02:00
|
|
|
Usage: "debug, release or test",
|
|
|
|
Value: "",
|
2018-12-21 03:11:06 +01:00
|
|
|
EnvVar: "PHOTOPRISM_HTTP_MODE",
|
2018-09-24 09:53:16 +02:00
|
|
|
},
|
2019-06-20 01:11:03 +02:00
|
|
|
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "daemonize, d",
|
|
|
|
Usage: "run Photoprism as Daemon",
|
|
|
|
EnvVar: "PHOTOPRISM_DAEMON_MODE",
|
|
|
|
},
|
2018-09-24 09:53:16 +02:00
|
|
|
}
|
|
|
|
|
2018-11-17 06:21:39 +01:00
|
|
|
func startAction(ctx *cli.Context) error {
|
2019-06-03 22:58:15 +02:00
|
|
|
// pass this context down the chain
|
|
|
|
cctx, cancel := context.WithCancel(context.Background())
|
2019-05-06 23:18:10 +02:00
|
|
|
conf := config.NewConfig(ctx)
|
|
|
|
if conf.HttpServerPort() < 1 {
|
2019-05-02 14:10:05 +02:00
|
|
|
log.Fatal("server port must be a positive integer")
|
2018-09-24 09:53:16 +02:00
|
|
|
}
|
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
if err := conf.CreateDirectories(); err != nil {
|
2018-09-24 09:53:16 +02:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-06-03 22:58:15 +02:00
|
|
|
if err := conf.Init(cctx); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-05-06 23:18:10 +02:00
|
|
|
conf.MigrateDb()
|
2018-09-24 09:53:16 +02:00
|
|
|
|
2019-06-20 01:11:03 +02:00
|
|
|
dctx := new(daemon.Context)
|
|
|
|
dctx.LogFileName = conf.DaemonLogPath()
|
|
|
|
dctx.PidFileName = conf.DaemonPIDPath()
|
|
|
|
dctx.Args = ctx.Args()
|
|
|
|
if !daemon.WasReborn() && conf.ShouldDaemonize() {
|
|
|
|
conf.Shutdown()
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
if pid, ok := childAlreadyRunning(conf.DaemonPIDPath()); ok {
|
|
|
|
log.Infof("Daemon already running with PID[%v]\n", pid)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
child, err := dctx.Reborn()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if child != nil {
|
|
|
|
if !util.Overwrite(conf.DaemonPIDPath(), []byte(strconv.Itoa(child.Pid))) {
|
|
|
|
log.Fatal("failed to write PID to file")
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Daemon started with PID: %v\n", child.Pid)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2019-05-06 23:18:10 +02:00
|
|
|
log.Infof("starting web server at %s:%d", conf.HttpServerHost(), conf.HttpServerPort())
|
2019-06-03 22:58:15 +02:00
|
|
|
go server.Start(cctx, conf)
|
2018-09-24 09:53:16 +02:00
|
|
|
|
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
|
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()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
2019-06-03 22:58:15 +02:00
|
|
|
time.Sleep(3 * time.Second)
|
2018-09-24 09:53:16 +02:00
|
|
|
return nil
|
|
|
|
}
|
2019-06-20 01:11:03 +02:00
|
|
|
|
|
|
|
func childAlreadyRunning(filePath string) (pid int, running bool) {
|
|
|
|
if !util.Exists(filePath) {
|
|
|
|
return pid, false
|
|
|
|
}
|
|
|
|
|
|
|
|
pid, err := daemon.ReadPidFile(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return pid, false
|
|
|
|
}
|
|
|
|
|
|
|
|
process, err := os.FindProcess(int(pid))
|
|
|
|
if err != nil {
|
|
|
|
return pid, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return pid, process.Signal(syscall.Signal(0)) == nil
|
|
|
|
}
|