add daemon

This commit is contained in:
kingecg 2023-12-10 19:26:09 +08:00
parent 49ccc86721
commit 703a7e12ee
11 changed files with 263 additions and 15 deletions

View File

@ -2,7 +2,10 @@
"logging" :{
"appenders": {
"out" :{
"type": "console"
"type": "file",
"options":{
"file": "gohttpd.log"
}
}
},
"categories": {

1
go.mod
View File

@ -3,6 +3,7 @@ module git.pyer.club/kingecg/gohttpd
go 1.19
require (
git.pyer.club/kingecg/godaemon v0.0.0-20231210104221-3a72649c6511 // indirect
git.pyer.club/kingecg/gologger v1.0.1 // indirect
github.com/samber/lo v1.39.0 // indirect
github.com/soheilhy/cmux v0.1.5 // indirect

2
go.sum
View File

@ -1,3 +1,5 @@
git.pyer.club/kingecg/godaemon v0.0.0-20231210104221-3a72649c6511 h1:qgNgm8ewkZdK7S3gaMagPKi90wgHpuGjpQdBVz21tjE=
git.pyer.club/kingecg/godaemon v0.0.0-20231210104221-3a72649c6511/go.mod h1:6cL7tPbT1m6EaTvrbDg/YhoZG0Mms8WBD10yRGhng4o=
git.pyer.club/kingecg/gologger v1.0.0 h1:H3oFIJ1p7mlAgJgJ/wiM+hxn34x7IxY4YiafY8iMfAk=
git.pyer.club/kingecg/gologger v1.0.0/go.mod h1:SNSl2jRHPzIpHSzdKOoVG798rtYMjPDPFyxUrEgivkY=
git.pyer.club/kingecg/gologger v1.0.1 h1:snCb0ePlfDUglX+CHwNzq5MRK5uNTnPUks1Dnapl/p8=

View File

@ -12,22 +12,24 @@ import (
"git.pyer.club/kingecg/gologger"
)
var conf model.GoHttpdConfig
type GoHttp struct {
conf model.GoHttpdConfig
logger gologger.Logger
}
func (g *GoHttp) Start() {
g.logger = gologger.GetLogger("Server")
g.logger.Info("start gohttpd")
// if g.conf != nil {
port := model.DefaultAdminConfig.Port
if g.conf.Admin != nil {
port = g.conf.Admin.Port
if conf.Admin != nil {
port = conf.Admin.Port
}
g.makeServer("admin", port, admin.AdminServerMux)
for _, server := range g.conf.Servers {
for _, server := range conf.Servers {
sHandler := g.assembleServerMux(server.Paths)
g.makeServer(server.ServerName, server.Port, sHandler)
}
@ -68,7 +70,7 @@ func (g *GoHttp) assembleServerMux(p []model.HttpPath) http.Handler {
func (g *GoHttp) Stop() {}
func (g *GoHttp) LoadConfig(configPath string) {
func LoadConfig(configPath string) {
cpath := configPath
if cpath == "" {
cpath = GetExecDir() + "/config.json"
@ -76,8 +78,8 @@ func (g *GoHttp) LoadConfig(configPath string) {
// read content from cpath
content, _ := os.ReadFile(cpath)
json.Unmarshal(content, &g.conf)
gologger.Configure(g.conf.Logging)
g.logger = gologger.GetLogger("Server")
g.logger.Info("Load config success")
json.Unmarshal(content, &conf)
gologger.Configure(conf.Logging)
logger := gologger.GetLogger("Server")
logger.Info("Load config success")
}

20
main.go
View File

@ -4,16 +4,28 @@ import (
"os"
"os/signal"
"syscall"
"git.pyer.club/kingecg/godaemon"
)
func main() {
httpd := &GoHttp{}
httpd.LoadConfig("")
httpd.Start()
LoadConfig("")
daemon := godaemon.NewGoDaemon(start, stop)
daemon.Start()
}
func start(g *godaemon.GoDaemon) {
var waiter = make(chan os.Signal, 1) // buffered channel
signal.Notify(waiter, syscall.SIGTERM, syscall.SIGINT)
httpd := &GoHttp{}
httpd.Start()
// blocks here until there's a signal
<-waiter
}
func stop(g *godaemon.GoDaemon) {
g.Running.Process.Signal(syscall.SIGTERM)
}

View File

@ -10,6 +10,7 @@ import (
"bufio"
"io"
"git.pyer.club/kingecg/gologger"
logger "git.pyer.club/kingecg/gologger"
"github.com/soheilhy/cmux"
)
@ -30,6 +31,7 @@ func makeMatcher(name string, s *ServerListener) cmux.Matcher {
}
type ServerListener struct {
port int
listener cmux.CMux
servers map[string]*http.Server
}
@ -73,6 +75,8 @@ func (s *ServerListener) ShutDown() {
}
func (s *ServerListener) Serve() {
l := gologger.GetLogger("Listener")
l.Debug("listen on :", s.port)
go s.listener.Serve()
}
@ -85,7 +89,7 @@ func NewServerListener(port int) *ServerListener {
l.Error("Listen error:", err)
}
muxer := cmux.New(l)
s := &ServerListener{listener: muxer, servers: make(map[string]*http.Server)}
s := &ServerListener{port: port, listener: muxer, servers: make(map[string]*http.Server)}
return s
}

View File

@ -0,0 +1,23 @@
# ---> Go
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Dependency directories (remove the comment below to include it)
# vendor/
# Go workspace file
go.work

View File

@ -0,0 +1,9 @@
MIT License
Copyright (c) 2023 kingecg
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -0,0 +1,2 @@
# godaemon

View File

@ -0,0 +1,187 @@
package godaemon
import (
"flag"
"fmt"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strconv"
"strings"
"syscall"
"git.pyer.club/kingecg/gologger"
)
const (
daemon_env_key = "_go_daemon"
daemon_process = "g_daemon"
daemon_task = "g_dtask"
daemon_taskargs = "g_args"
)
type GoDaemon struct {
pidFile string
flag *string
sigChan chan os.Signal
state string
gologger.Logger
Running *exec.Cmd
StartFn func(*GoDaemon)
StopFn func(*GoDaemon)
}
func (g *GoDaemon) GetPid() int {
pids, ferr := os.ReadFile(g.pidFile)
pid, err := strconv.Atoi(string(pids))
if err != nil || ferr != nil {
return 0
}
return pid
}
func (g *GoDaemon) Start() {
if g.flag == nil {
g.flag = flag.String("s", "", "send signal to daemon. support: reload and quit")
}
if IsMaster() {
flag.Parse()
if *g.flag == "" {
g.startDaemon()
return
}
var sig syscall.Signal
if *g.flag == "reload" {
sig = syscall.SIGHUP
} else if *g.flag == "quit" {
sig = syscall.SIGTERM
} else {
fmt.Println("Not supported signal")
return
}
p := g.getDaemonProcess()
if p == nil {
fmt.Println("Daemon process not found")
return
}
g.Debug("Send signal:", p.Pid, sig)
p.Signal(sig)
} else if IsDaemon() {
pid := os.Getpid()
os.WriteFile(g.pidFile, []byte(strconv.Itoa(pid)), 0644)
g.sigChan = make(chan os.Signal)
signal.Notify(g.sigChan, syscall.SIGTERM, syscall.SIGHUP)
go g.serveSignal()
for {
g.Debug("Starting new task")
g.Running = g.startTask()
g.state = "running"
g.Running.Process.Wait()
if g.state == "stopped" {
g.Debug("daemon is stopped, exit now")
break
}
}
} else {
g.StartFn(g)
}
}
func (g *GoDaemon) serveSignal() {
sig := <-g.sigChan
if sig == syscall.SIGTERM {
g.state = "stopped"
} else {
g.state = "restart"
}
g.Debug("Stop it")
g.StopFn(g)
}
func (g *GoDaemon) getDaemonProcess() *os.Process {
pid := g.GetPid()
if pid == 0 {
return nil
}
p, err := os.FindProcess(pid)
if err != nil {
g.Debug(err)
}
serr := p.Signal(syscall.Signal(0))
if serr != nil {
return nil
}
return p
}
func (g *GoDaemon) startDaemon() {
dp := g.getDaemonProcess()
if dp != nil {
fmt.Println("daemon is running with pid:", dp.Pid)
return
}
execName, _ := os.Executable()
cmd := exec.Command(execName)
cmd.Env = append(cmd.Env, daemon_env_key+"="+daemon_process)
pargs := os.Args[1:]
cmd.Env = append(cmd.Env, daemon_taskargs+"="+strings.Join(pargs, ";"))
cmd.Start()
}
func (g *GoDaemon) startTask() *exec.Cmd {
extraArgs, _ := os.LookupEnv(daemon_taskargs)
var cmd *exec.Cmd
execName, _ := os.Executable()
if extraArgs != "" {
eargs := strings.Split(extraArgs, ";")
cmd = exec.Command(execName, eargs...)
} else {
cmd = exec.Command(execName)
}
cmd.Env = append(cmd.Env, daemon_env_key+"="+daemon_task)
cmd.Start()
return cmd
}
func IsMaster() bool {
goDaemonEnv, _ := os.LookupEnv(daemon_env_key)
return goDaemonEnv == ""
}
func IsDaemon() bool {
goDaemonEnv, _ := os.LookupEnv(daemon_env_key)
return goDaemonEnv == daemon_process
}
func IsDaemonTask() bool {
goDaemonEnv, _ := os.LookupEnv(daemon_env_key)
return goDaemonEnv == daemon_task
}
func NewGoDaemon(start, stop func(*GoDaemon)) *GoDaemon {
godaemon := &GoDaemon{
Logger: gologger.GetLogger("daemon"),
}
execName, _ := os.Executable()
if filepath.Ext(execName) != "" {
execName = strings.TrimSuffix(execName, filepath.Ext(execName))
}
godaemon.pidFile = execName + ".pid"
godaemon.StartFn = start
godaemon.StopFn = stop
return godaemon
}

3
vendor/modules.txt vendored
View File

@ -1,3 +1,6 @@
# git.pyer.club/kingecg/godaemon v0.0.0-20231210104221-3a72649c6511
## explicit; go 1.19
git.pyer.club/kingecg/godaemon
# git.pyer.club/kingecg/gologger v1.0.1
## explicit; go 1.19
git.pyer.club/kingecg/gologger