feat(agent): add crash logs and watchdog startup

- Recover panics to disk for diagnostics
- Add 5-minute watchdog task with startup
- Add -ensure flag to start detached agent
- Add script to pull remote folder via API
This commit is contained in:
2026-09-01 20:04:20 +03:00
parent 2ce438852a
commit 36cb847f9e
11 changed files with 458 additions and 8 deletions
+58
View File
@@ -0,0 +1,58 @@
package crashlog
import (
"fmt"
"os"
"path/filepath"
"runtime/debug"
"sync"
"time"
"tea.chunkbyte.com/kato/go-worm/lib/config"
)
var mu sync.Mutex
// Recover logs a panic and re-raises after writing the crash file.
func Recover() {
if r := recover(); r != nil {
_ = write("panic", fmt.Sprint(r), debug.Stack())
panic(r)
}
}
// RecordPanic writes a recovered panic without exiting (e.g. HTTP handlers).
func RecordPanic(recovered any) {
_ = write("panic", fmt.Sprint(recovered), debug.Stack())
}
// RecordError writes a fatal error before exit.
func RecordError(err error) {
if err == nil {
return
}
_ = write("fatal", err.Error(), debug.Stack())
}
func write(kind, detail string, stack []byte) error {
dir, err := config.LogsDir()
if err != nil {
return err
}
mu.Lock()
defer mu.Unlock()
if err := os.MkdirAll(dir, 0o700); err != nil {
return err
}
name := fmt.Sprintf("crash-%s.log", time.Now().Format("2006-01-02-150405"))
path := filepath.Join(dir, name)
body := fmt.Sprintf(
"time: %s\nversion: %s\nkind: %s\ndetail: %s\n\nstack:\n%s",
time.Now().Format(time.RFC3339Nano),
config.Version,
kind,
detail,
stack,
)
return os.WriteFile(path, []byte(body), 0o600)
}
+38
View File
@@ -0,0 +1,38 @@
package crashlog
import (
"os"
"path/filepath"
"strings"
"testing"
"tea.chunkbyte.com/kato/go-worm/lib/config"
)
func TestWrite(t *testing.T) {
base := t.TempDir()
t.Setenv("XDG_CONFIG_HOME", base)
t.Setenv("APPDATA", base)
if err := write("test", "boom", []byte("trace")); err != nil {
t.Fatal(err)
}
dir := filepath.Join(base, config.AppDataDir, config.LogsSubdir)
entries, err := os.ReadDir(dir)
if err != nil {
t.Fatal(err)
}
if len(entries) != 1 {
t.Fatalf("entries = %d, want 1", len(entries))
}
data, err := os.ReadFile(filepath.Join(dir, entries[0].Name()))
if err != nil {
t.Fatal(err)
}
text := string(data)
for _, part := range []string{"kind: test", "detail: boom", "trace", config.Version} {
if !strings.Contains(text, part) {
t.Fatalf("log missing %q: %s", part, text)
}
}
}