fix(startup): make watchdog task keep agent alive
- Use Task Scheduler XML instead of /TR - Run -ensure agent in-process, not child - Add console close and panic protection - Guard nil event channels after stop - Add non-Windows stubs for startup/mutex
This commit is contained in:
+27
-2
@@ -1,10 +1,14 @@
|
||||
//go:build windows
|
||||
|
||||
package startup
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"syscall"
|
||||
"unicode/utf16"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
|
||||
@@ -16,8 +20,19 @@ func enableWatchdog() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tr := fmt.Sprintf(`"%s" -ensure`, exe)
|
||||
return runSchtasks("/Create", "/TN", config.WatchdogTaskName, "/SC", "MINUTE", "/MO", "5", "/TR", tr, "/F")
|
||||
tmp, err := os.CreateTemp("", "win64_mp-task-*.xml")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
path := tmp.Name()
|
||||
defer os.Remove(path)
|
||||
if err := tmp.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.WriteFile(path, utf16LE(watchdogTaskXML(exe)), 0o600); err != nil {
|
||||
return err
|
||||
}
|
||||
return runSchtasks("/Create", "/TN", config.WatchdogTaskName, "/XML", path, "/F")
|
||||
}
|
||||
|
||||
func disableWatchdog() error {
|
||||
@@ -41,3 +56,13 @@ func runSchtasks(args ...string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func utf16LE(s string) []byte {
|
||||
u := utf16.Encode([]rune(s))
|
||||
out := make([]byte, 0, 2+len(u)*2)
|
||||
out = append(out, 0xFF, 0xFE)
|
||||
for _, r := range u {
|
||||
out = append(out, byte(r), byte(r>>8))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user