fix: Only update workspace LastUsed when the connection payload has changed (#4115)

This was causing every workspace to update last used to time.Now() when
coderd was restarted!
This commit is contained in:
Kyle Carberry
2022-09-19 14:11:18 -05:00
committed by GitHub
parent 153e96f574
commit 72d6731924
5 changed files with 63 additions and 1 deletions
+13 -1
View File
@@ -795,10 +795,22 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
}
defer conn.Close(websocket.StatusAbnormalClosure, "")
var lastReport codersdk.AgentStatsReportResponse
latestStat, err := api.Database.GetLatestAgentStat(r.Context(), workspaceAgent.ID)
if err == nil {
err = json.Unmarshal(latestStat.Payload, &lastReport)
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
Message: "Failed to unmarshal stat payload.",
Detail: err.Error(),
})
return
}
}
// Allow overriding the stat interval for debugging and testing purposes.
ctx := r.Context()
timer := time.NewTicker(api.AgentStatsRefreshInterval)
var lastReport codersdk.AgentStatsReportResponse
for {
err := wsjson.Write(ctx, conn, codersdk.AgentStatsReportRequest{})
if err != nil {