engine: resources: Simplify the Watch loop

I had some legacy unnecessary boolean for sending everywhere. Not sure
why I never re-read it, it's so easy to just copy and paste and carry
on.
This commit is contained in:
James Shubin
2025-05-25 02:12:14 -04:00
parent f73127ec23
commit b868a60f69
29 changed files with 48 additions and 244 deletions

View File

@@ -183,32 +183,27 @@ func (obj *NspawnRes) Watch(ctx context.Context) error {
obj.init.Running() // when started, notify engine that we're running
var send = false // send event?
for {
select {
case event := <-busChan:
// process org.freedesktop.machine1 events for this resource's name
if event.Body[0] == obj.Name() {
obj.init.Logf("Event received: %v", event.Name)
if event.Name == machineNew {
obj.init.Logf("Machine started")
} else if event.Name == machineRemoved {
obj.init.Logf("Machine stopped")
} else {
return fmt.Errorf("unknown event: %s", event.Name)
}
send = true
if event.Body[0] != obj.Name() {
continue
}
obj.init.Logf("Event received: %v", event.Name)
if event.Name == machineNew {
obj.init.Logf("Machine started")
} else if event.Name == machineRemoved {
obj.init.Logf("Machine stopped")
} else {
return fmt.Errorf("unknown event: %s", event.Name)
}
case <-ctx.Done(): // closed by the engine to signal shutdown
return nil
}
// do all our event sending all together to avoid duplicate msgs
if send {
send = false
obj.init.Event() // notify engine of an event (this can block)
}
obj.init.Event() // notify engine of an event (this can block)
}
}