2 Star 1 Fork 0

ly-code/supervisord

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
service.go 2.60 KB
一键复制 编辑 原始数据 按行查看 历史
package main
import (
"fmt"
"github.com/kardianos/service"
log "github.com/sirupsen/logrus"
)
// ServiceCommand install/uninstall/start/stop supervisord service
type ServiceCommand struct {
}
var serviceCommand ServiceCommand
type program struct{}
// Start supervised service
func (p *program) Start(s service.Service) error {
go p.run()
return nil
}
func (p *program) run() {}
// Stop supervised service
func (p *program) Stop(s service.Service) error {
// Stop should not block. Return with a few seconds.
return nil
}
// Execute implement Execute() method defined in flags.Commander interface, executes the given command
func (sc ServiceCommand) Execute(args []string) error {
if len(args) == 0 {
showUsage()
return nil
}
serviceArgs := make([]string, 0)
if options.Configuration != "" {
serviceArgs = append(serviceArgs, "--configuration="+options.Configuration)
}
if options.EnvFile != "" {
serviceArgs = append(serviceArgs, "--env-file="+options.EnvFile)
}
svcConfig := &service.Config{
Name: "go-supervisord",
DisplayName: "go-supervisord",
Description: "Supervisord service in golang",
Arguments: serviceArgs,
}
prg := &program{}
s, err := service.New(prg, svcConfig)
if err != nil {
log.Error("service init failed", err)
return err
}
action := args[0]
switch action {
case "install":
err := s.Install()
if err != nil {
log.Error("Failed to install service go-supervisord: ", err)
fmt.Println("Failed to install service go-supervisord: ", err)
return err
} else {
fmt.Println("Succeed to install service go-supervisord")
}
case "uninstall":
s.Stop()
err := s.Uninstall()
if err != nil {
log.Error("Failed to uninstall service go-supervisord: ", err)
fmt.Println("Failed to uninstall service go-supervisord: ", err)
return err
} else {
fmt.Println("Succeed to uninstall service go-supervisord")
}
case "start":
err := s.Start()
if err != nil {
log.Error("Failed to start service: ", err)
fmt.Println("Failed to start service: ", err)
return err
} else {
fmt.Println("Succeed to start service go-supervisord")
}
case "stop":
err := s.Stop()
if err != nil {
log.Error("Failed to stop service: ", err)
fmt.Println("Failed to stop service: ", err)
return err
} else {
fmt.Println("Succeed to stop service go-supervisord")
}
default:
showUsage()
}
return nil
}
func showUsage() {
fmt.Println("usage: supervisord service install/uninstall/start/stop")
}
func init() {
parser.AddCommand("service",
"install/uninstall/start/stop service",
"install/uninstall/start/stop service",
&serviceCommand)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/ly-code/supervisord.git
git@gitee.com:ly-code/supervisord.git
ly-code
supervisord
supervisord
master

搜索帮助