refactor(hub): decode each agent response into a new struct

The WebSocket path reused sys.data, and CBOR leaves omitted fields
untouched, so stale values carried over (e.g. Details re-saved every
poll, SystemdServicesUpdated stuck true, old map entries kept).

sys.data is now set only by the regular update for alerts. Removes the
Wi-Fi-only workaround in UnmarshalResponse.
This commit is contained in:
henrygd
2026-09-26 09:43:33 -04:00
parent c7f2177b08
commit 24ec18a937
4 changed files with 105 additions and 65 deletions

View File

@@ -153,6 +153,7 @@ func (sys *System) update() error {
// ensure deprecated fields from older agents are migrated to current fields
migrateDeprecatedFields(data, !sys.detailsFetched.Load())
sys.data = data
// create system records
_, err = sys.createRecords(data)
@@ -704,11 +705,9 @@ func (sys *System) ensureSSHTransport() error {
}
// fetchDataFromAgent attempts to fetch data from the agent, prioritizing WebSocket if available.
// Each fetch decodes into a new struct: CBOR leaves fields the agent omits
// untouched, and real-time and regular updates may fetch concurrently.
func (sys *System) fetchDataFromAgent(options common.DataRequestOptions) (*system.CombinedData, error) {
if sys.data == nil {
sys.data = &system.CombinedData{}
}
if sys.WsConn != nil && sys.WsConn.IsConnected() {
wsData, err := sys.fetchDataViaWebSocket(options)
if err == nil {
@@ -744,11 +743,11 @@ func (sys *System) fetchDataViaWebSocket(options common.DataRequestOptions) (*sy
ctx, cancel := context.WithTimeout(context.Background(), wsDataRequestTimeout)
defer cancel()
wsTransport := transport.NewWebSocketTransport(sys.WsConn)
err := wsTransport.Request(ctx, common.GetData, options, sys.data)
if err != nil {
data := &system.CombinedData{}
if err := wsTransport.Request(ctx, common.GetData, options, data); err != nil {
return nil, err
}
return sys.data, nil
return data, nil
}
// FetchContainerInfoFromAgent fetches container info from the agent
@@ -810,9 +809,8 @@ func MakeStableHashId(strings ...string) string {
}
// fetchDataViaSSH handles fetching data using SSH.
// This function encapsulates the original SSH logic.
// It updates sys.data directly upon successful fetch.
func (sys *System) fetchDataViaSSH(options common.DataRequestOptions) (*system.CombinedData, error) {
data := &system.CombinedData{}
err := sys.runSSHOperation(4*time.Second, 1, func(session *ssh.Session) (bool, error) {
stdout, err := session.StdoutPipe()
if err != nil {
@@ -823,7 +821,8 @@ func (sys *System) fetchDataViaSSH(options common.DataRequestOptions) (*system.C
return false, err
}
*sys.data = system.CombinedData{}
// reset in case of retry after a partial decode
*data = system.CombinedData{}
if sys.agentVersion.GTE(beszel.MinVersionAgentResponse) && stdinErr == nil {
req := common.HubRequest[any]{Action: common.GetData, Data: options}
@@ -832,7 +831,7 @@ func (sys *System) fetchDataViaSSH(options common.DataRequestOptions) (*system.C
var resp common.AgentResponse
if decErr := cbor.NewDecoder(stdout).Decode(&resp); decErr == nil && resp.SystemData != nil {
*sys.data = *resp.SystemData
*data = *resp.SystemData
if err := session.Wait(); err != nil {
return false, err
}
@@ -842,9 +841,9 @@ func (sys *System) fetchDataViaSSH(options common.DataRequestOptions) (*system.C
var decodeErr error
if sys.agentVersion.GTE(beszel.MinVersionCbor) {
decodeErr = cbor.NewDecoder(stdout).Decode(sys.data)
decodeErr = cbor.NewDecoder(stdout).Decode(data)
} else {
decodeErr = json.NewDecoder(stdout).Decode(sys.data)
decodeErr = json.NewDecoder(stdout).Decode(data)
}
if decodeErr != nil {
@@ -861,7 +860,7 @@ func (sys *System) fetchDataViaSSH(options common.DataRequestOptions) (*system.C
return nil, err
}
return sys.data, nil
return data, nil
}
// runSSHOperation establishes an SSH session and executes the provided operation.