2017-06-02 16:08:14 +08:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/astaxie/beego"
|
|
|
|
"github.com/kardianos/service"
|
2017-06-14 09:23:29 +08:00
|
|
|
"github.com/lifei6671/mindoc/commands"
|
|
|
|
"github.com/lifei6671/mindoc/conf"
|
|
|
|
"github.com/lifei6671/mindoc/controllers"
|
2017-06-02 16:08:14 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Daemon struct {
|
|
|
|
config *service.Config
|
|
|
|
errs chan error
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDaemon() *Daemon {
|
|
|
|
|
|
|
|
config := &service.Config{
|
2018-01-26 17:17:38 +08:00
|
|
|
Name: "mindocd", //服务显示名称
|
2017-06-05 11:37:22 +08:00
|
|
|
DisplayName: "MinDoc service", //服务名称
|
2017-06-02 16:08:14 +08:00
|
|
|
Description: "A document online management program.", //服务描述
|
2018-01-26 17:17:38 +08:00
|
|
|
WorkingDirectory: conf.WorkingDirectory,
|
2017-06-02 16:08:14 +08:00
|
|
|
Arguments: os.Args[1:],
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Daemon{
|
|
|
|
config: config,
|
|
|
|
errs: make(chan error, 100),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Daemon) Config() *service.Config {
|
|
|
|
return d.config
|
|
|
|
}
|
|
|
|
func (d *Daemon) Start(s service.Service) error {
|
|
|
|
|
|
|
|
go d.Run()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Daemon) Run() {
|
|
|
|
|
|
|
|
commands.ResolveCommand(d.config.Arguments)
|
|
|
|
|
|
|
|
commands.RegisterFunction()
|
|
|
|
|
2018-08-07 17:19:56 +08:00
|
|
|
commands.RegisterAutoLoadConfig()
|
|
|
|
|
2017-06-02 16:08:14 +08:00
|
|
|
beego.ErrorController(&controllers.ErrorController{})
|
|
|
|
|
|
|
|
fmt.Printf("MinDoc version => %s\nbuild time => %s\nstart directory => %s\n%s\n", conf.VERSION, conf.BUILD_TIME, os.Args[0], conf.GO_VERSION)
|
|
|
|
|
|
|
|
beego.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Daemon) Stop(s service.Service) error {
|
|
|
|
if service.Interactive() {
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Install() {
|
|
|
|
d := NewDaemon()
|
|
|
|
d.config.Arguments = os.Args[3:]
|
|
|
|
|
|
|
|
s, err := service.New(d, d.config)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
beego.Error("Create service error => ", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
err = s.Install()
|
|
|
|
if err != nil {
|
|
|
|
beego.Error("Install service error:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
} else {
|
|
|
|
beego.Info("Service installed!")
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Uninstall() {
|
|
|
|
d := NewDaemon()
|
|
|
|
s, err := service.New(d, d.config)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
beego.Error("Create service error => ", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
err = s.Uninstall()
|
|
|
|
if err != nil {
|
|
|
|
beego.Error("Install service error:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
} else {
|
|
|
|
beego.Info("Service uninstalled!")
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Restart() {
|
|
|
|
d := NewDaemon()
|
|
|
|
s, err := service.New(d, d.config)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
beego.Error("Create service error => ", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
err = s.Restart()
|
|
|
|
if err != nil {
|
|
|
|
beego.Error("Install service error:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
} else {
|
|
|
|
beego.Info("Service Restart!")
|
|
|
|
}
|
2017-06-05 11:37:22 +08:00
|
|
|
os.Exit(0)
|
2017-06-02 16:08:14 +08:00
|
|
|
}
|