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:
+22
-17
@@ -21,17 +21,19 @@ import (
|
||||
)
|
||||
|
||||
type Agent struct {
|
||||
addr string
|
||||
root string
|
||||
guard *instance.Guard
|
||||
server *http.Server
|
||||
startedAt time.Time
|
||||
addr string
|
||||
root string
|
||||
guard *instance.Guard
|
||||
server *http.Server
|
||||
startedAt time.Time
|
||||
stopOnInterrupt bool
|
||||
}
|
||||
|
||||
// Config controls agent startup. Zero values use environment defaults.
|
||||
type Config struct {
|
||||
ListenAddr string
|
||||
Parallel bool
|
||||
ListenAddr string
|
||||
Parallel bool
|
||||
StopOnInterrupt bool
|
||||
}
|
||||
|
||||
func New(cfg Config) (*Agent, error) {
|
||||
@@ -40,8 +42,9 @@ func New(cfg Config) (*Agent, error) {
|
||||
addr = config.EnvOr("AGENT_ADDR", config.DefaultAddr)
|
||||
}
|
||||
a := &Agent{
|
||||
addr: addr,
|
||||
startedAt: time.Now(),
|
||||
addr: addr,
|
||||
startedAt: time.Now(),
|
||||
stopOnInterrupt: cfg.StopOnInterrupt,
|
||||
}
|
||||
if root := strings.TrimSpace(os.Getenv("AGENT_FILE_ROOT")); root != "" {
|
||||
resolved, err := files.CanonicalExistingPath(root)
|
||||
@@ -116,14 +119,16 @@ func (a *Agent) Serve() error {
|
||||
|
||||
helpers.Log.Printf("win64_mp %s listening on http://%s", config.Version, a.addr)
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
|
||||
defer stop()
|
||||
helpers.Go("shutdown", func() {
|
||||
<-ctx.Done()
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
_ = a.server.Shutdown(shutdownCtx)
|
||||
})
|
||||
if a.stopOnInterrupt {
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
|
||||
defer stop()
|
||||
helpers.Go("shutdown", func() {
|
||||
<-ctx.Done()
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
_ = a.server.Shutdown(shutdownCtx)
|
||||
})
|
||||
}
|
||||
|
||||
if err := a.server.Serve(listener); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
return fmt.Errorf("serve: %w", err)
|
||||
|
||||
Reference in New Issue
Block a user