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"
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2020-02-18 23:42:51 +01:00
|
|
|
// StartCommand is used to register 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 err := conf.CreateDirectories(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.IsSet("config") {
|
|
|
|
fmt.Printf("NAME VALUE\n")
|
|
|
|
fmt.Printf("detach-server %t\n", conf.DetachServer())
|
|
|
|
|
2019-07-02 18:25:46 +02:00
|
|
|
fmt.Printf("http-host %s\n", conf.HttpServerHost())
|
|
|
|
fmt.Printf("http-port %d\n", conf.HttpServerPort())
|
|
|
|
fmt.Printf("http-mode %s\n", conf.HttpServerMode())
|
|
|
|
|
2019-06-30 05:38:39 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-03 22:58:15 +02:00
|
|
|
// pass this context down the chain
|
|
|
|
cctx, cancel := context.WithCancel(context.Background())
|
2019-06-30 05:38:39 +02:00
|
|
|
|
|
|
|
if conf.HttpServerPort() < 1 || conf.HttpServerPort() > 65535 {
|
|
|
|
log.Fatal("server port must be a number between 1 and 65535")
|
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)
|
|
|
|
}
|
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() {
|
|
|
|
log.Infof("read-only mode enabled")
|
|
|
|
}
|
|
|
|
|
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-04-06 09:41:42 +02:00
|
|
|
// start share & sync workers
|
|
|
|
workers.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()
|
|
|
|
|
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
|
|
|
|
}
|