Bereinige Agent-Shutdown fuer bereits gestoppte Contexts
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
parent
9294f5e324
commit
fa57cb4e71
2 changed files with 40 additions and 1 deletions
|
|
@ -88,8 +88,16 @@ func (a *App) Run(ctx context.Context) error {
|
||||||
return fmt.Errorf("screen id is required")
|
return fmt.Errorf("screen id is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
a.mu.Lock()
|
||||||
|
a.status = StatusStopped
|
||||||
|
a.mu.Unlock()
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
a.mu.Lock()
|
a.mu.Lock()
|
||||||
a.status = StatusRunning
|
|
||||||
a.startedAt = a.now()
|
a.startedAt = a.now()
|
||||||
a.mu.Unlock()
|
a.mu.Unlock()
|
||||||
|
|
||||||
|
|
@ -101,6 +109,9 @@ func (a *App) Run(ctx context.Context) error {
|
||||||
a.Config.HeartbeatEvery,
|
a.Config.HeartbeatEvery,
|
||||||
)
|
)
|
||||||
a.emitHeartbeat()
|
a.emitHeartbeat()
|
||||||
|
a.mu.Lock()
|
||||||
|
a.status = StatusRunning
|
||||||
|
a.mu.Unlock()
|
||||||
|
|
||||||
ticker := time.NewTicker(time.Duration(a.Config.HeartbeatEvery) * time.Second)
|
ticker := time.NewTicker(time.Duration(a.Config.HeartbeatEvery) * time.Second)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
|
||||||
|
|
@ -107,3 +107,31 @@ func TestAppSnapshotIncludesConfiguredTargets(t *testing.T) {
|
||||||
t.Fatalf("HeartbeatEvery = %d, want %d", got, want)
|
t.Fatalf("HeartbeatEvery = %d, want %d", got, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAppRunWithCanceledContextDoesNotLogConfiguredOrHeartbeat(t *testing.T) {
|
||||||
|
var logBuffer bytes.Buffer
|
||||||
|
application := newApp(config.Config{
|
||||||
|
ScreenID: "screen-canceled",
|
||||||
|
ServerBaseURL: "http://127.0.0.1:8080",
|
||||||
|
MQTTBroker: "tcp://127.0.0.1:1883",
|
||||||
|
HeartbeatEvery: 5,
|
||||||
|
}, log.New(&logBuffer, "", 0), time.Now)
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
cancel()
|
||||||
|
|
||||||
|
if err := application.Run(ctx); err != nil {
|
||||||
|
t.Fatalf("Run() error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if got, want := application.Snapshot().Status, StatusStopped; got != want {
|
||||||
|
t.Fatalf("final status = %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
|
||||||
|
logs := logBuffer.String()
|
||||||
|
for _, needle := range []string{"event=agent_configured", "event=heartbeat_tick"} {
|
||||||
|
if strings.Contains(logs, needle) {
|
||||||
|
t.Fatalf("logs unexpectedly contain %q: %s", needle, logs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue