chore: 移除 daemon 模式

This commit is contained in:
Mmx233
2023-06-01 18:26:09 +08:00
parent 874eeddf96
commit 6c60a52d4e
10 changed files with 7 additions and 133 deletions

View File

@@ -1,59 +0,0 @@
package controllers
import (
"fmt"
"github.com/Mmx233/BitSrunLoginGo/internal/global"
"github.com/howeyc/fsnotify"
"os"
"time"
)
type daemon struct {
Mark string
Path string
}
// Daemon 后台模式控制包
var Daemon = daemon{
Mark: fmt.Sprint(time.Now().UnixNano()),
Path: global.Config.Settings.Daemon.Path,
}
// MarkDaemon 写入后台标记文件
func (a *daemon) MarkDaemon() error {
return os.WriteFile(a.Path, []byte(a.Mark), 0600)
}
// CheckDaemon 检查后台标记文件
func (a *daemon) CheckDaemon() bool {
if data, err := os.ReadFile(a.Path); err != nil {
return false
} else {
return string(data) == a.Mark
}
}
// DaemonChan 后台标记文件监听
func (a *daemon) DaemonChan() bool {
f, err := fsnotify.NewWatcher()
if err != nil {
panic(err)
}
err = f.Watch(Daemon.Path)
if err != nil {
panic(err)
}
for {
select {
case event := <-f.Event:
if event.IsModify() && a.CheckDaemon() {
continue
}
os.Exit(0)
case e := <-f.Error:
panic(e)
}
}
}

View File

@@ -2,8 +2,6 @@ package controllers
import (
"github.com/Mmx233/BitSrunLoginGo/internal/global"
"os"
"os/exec"
"time"
"github.com/Mmx233/BitSrunLoginGo/tools"
@@ -12,16 +10,10 @@ import (
// Guardian 守护模式逻辑
func Guardian() {
log.Infoln("[以守护模式启动]")
GuardianDuration := time.Duration(global.Config.Settings.Guardian.Duration) * time.Second
if global.Config.Settings.Daemon.Enable {
go Daemon.DaemonChan()
if e := Daemon.MarkDaemon(); e != nil {
log.Warnln("写入daemon标记文件失败: ", e)
}
}
var c = make(chan bool)
for {
go func() {
@@ -52,16 +44,3 @@ func Guardian() {
time.Sleep(GuardianDuration)
}
}
// EnterGuardian 守护模式入口控制是否进入daemon
func EnterGuardian() {
log.Infoln("[以守护模式启动]")
if global.Config.Settings.Daemon.Enable || global.Flags.Daemon {
if err := exec.Command(os.Args[0], append(os.Args[1:], "--running-daemon")...).Start(); err != nil {
log.Fatalln("启动守护失败: ", err)
}
log.Infoln("[进入后台进程模式]")
return
}
Guardian()
}

View File

@@ -30,9 +30,6 @@ func readConfig() {
Basic: models.Basic{
Timeout: 5,
},
Daemon: models.Daemon{
Path: ".BitSrun",
},
Guardian: models.Guardian{
Duration: 300,
},

View File

@@ -7,10 +7,6 @@ import (
var Flags struct {
//配置文件路径
Path string
//daemon模式内置标记
RunningDaemon bool
//强制daemon
Daemon bool
Interface string
}
@@ -18,7 +14,5 @@ var Flags struct {
func initFlags() {
flag.StringVar(&Flags.Path, "config", "Config.yaml", "config path")
flag.StringVar(&Flags.Interface, "interface", "", "specify the eth name")
flag.BoolVar(&Flags.RunningDaemon, "running-daemon", false, "")
flag.BoolVar(&Flags.Daemon, "daemon", false, "")
flag.Parse()
}

View File

@@ -4,11 +4,6 @@ import (
"github.com/Mmx233/BitSrunLoginGo/pkg/srun"
)
type Daemon struct {
Enable bool `json:"enable" yaml:"enable" mapstructure:"enable"`
Path string `json:"path" yaml:"path" mapstructure:"path"`
}
type Guardian struct {
Enable bool `json:"enable" yaml:"enable" mapstructure:"enable"`
Duration uint `json:"duration" yaml:"duration" mapstructure:"duration"`
@@ -39,7 +34,6 @@ type DDNS struct {
type Settings struct {
Basic Basic `json:"basic" yaml:"basic" mapstructure:"basic"`
Guardian Guardian `json:"guardian" yaml:"guardian" mapstructure:"guardian"`
Daemon Daemon `json:"daemon" yaml:"daemon" mapstructure:"daemon"`
Log Log `json:"log" yaml:"log" mapstructure:"log"`
DDNS DDNS `json:"ddns" yaml:"ddns" mapstructure:"ddns"`
}