2019-06-20 01:11:03 +02:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"syscall"
|
|
|
|
|
2019-06-30 05:38:39 +02:00
|
|
|
"github.com/sevlyar/go-daemon"
|
2019-06-20 01:11:03 +02:00
|
|
|
"github.com/urfave/cli"
|
2021-09-22 19:33:41 +02:00
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2019-06-20 01:11:03 +02:00
|
|
|
)
|
|
|
|
|
2021-01-24 17:46:18 +01:00
|
|
|
// StopCommand registers the stop cli command.
|
2019-06-20 01:11:03 +02:00
|
|
|
var StopCommand = cli.Command{
|
2020-01-31 17:29:55 +01:00
|
|
|
Name: "stop",
|
2020-01-31 15:29:06 +01:00
|
|
|
Aliases: []string{"down"},
|
2021-10-17 14:25:29 +02:00
|
|
|
Usage: "Stops the web server in daemon mode",
|
2020-01-31 17:29:55 +01:00
|
|
|
Action: stopAction,
|
2019-06-20 01:11:03 +02:00
|
|
|
}
|
|
|
|
|
2020-02-18 23:42:51 +01:00
|
|
|
// stopAction stops the daemon if it is running.
|
2019-06-20 01:11:03 +02:00
|
|
|
func stopAction(ctx *cli.Context) error {
|
|
|
|
conf := config.NewConfig(ctx)
|
2019-06-30 05:38:39 +02:00
|
|
|
|
2021-12-14 17:07:38 +01:00
|
|
|
log.Infof("looking for pid in %s", txt.LogParam(conf.PIDFilename()))
|
2019-06-30 05:38:39 +02:00
|
|
|
|
2019-06-20 01:11:03 +02:00
|
|
|
dcxt := new(daemon.Context)
|
2019-06-30 05:38:39 +02:00
|
|
|
dcxt.PidFileName = conf.PIDFilename()
|
2019-06-20 01:11:03 +02:00
|
|
|
child, err := dcxt.Search()
|
2019-06-30 05:38:39 +02:00
|
|
|
|
2019-06-20 01:11:03 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = child.Signal(syscall.SIGTERM)
|
2019-06-30 05:38:39 +02:00
|
|
|
|
2019-06-20 01:11:03 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
st, err := child.Wait()
|
2019-06-30 05:38:39 +02:00
|
|
|
|
2019-06-20 01:11:03 +02:00
|
|
|
if err != nil {
|
2019-06-30 05:38:39 +02:00
|
|
|
log.Info("daemon exited successfully")
|
2019-06-20 01:11:03 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-30 05:38:39 +02:00
|
|
|
log.Infof("daemon[%v] exited[%v]? successfully[%v]?\n", st.Pid(), st.Exited(), st.Success())
|
|
|
|
|
2019-06-20 01:11:03 +02:00
|
|
|
return nil
|
|
|
|
}
|