feat: 尝试改用文件监听实现daemon
This commit is contained in:
@@ -4,6 +4,8 @@ import (
|
||||
"fmt"
|
||||
"github.com/Mmx233/BitSrunLoginGo/global"
|
||||
"github.com/Mmx233/tool"
|
||||
"github.com/howeyc/fsnotify"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -29,8 +31,32 @@ func (a *daemon) MarkDaemon() error {
|
||||
|
||||
func (a *daemon) CheckDaemon() bool {
|
||||
if data, err := tool.File.Read(a.Path); err != nil {
|
||||
return true
|
||||
return false
|
||||
} else {
|
||||
return string(data) == a.Mark
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,15 +11,14 @@ import (
|
||||
func Guardian(output bool) {
|
||||
global.Status.Output = output
|
||||
|
||||
go Daemon.DaemonChan()
|
||||
|
||||
if e := Daemon.MarkDaemon(); e != nil {
|
||||
util.Log.Fatalln(e)
|
||||
}
|
||||
|
||||
var c = make(chan bool)
|
||||
for {
|
||||
if !Daemon.CheckDaemon() {
|
||||
os.Exit(1)
|
||||
}
|
||||
global.Status.Output = output
|
||||
go func() {
|
||||
defer func() {
|
||||
|
||||
Reference in New Issue
Block a user