()
| 168 | } |
| 169 | |
| 170 | func (u *updater) run() { |
| 171 | u.enc = wsjson.NewEncoder[workspacesdk.ConnectionWatchEvent](u.conn, websocket.MessageText) |
| 172 | defer func() { |
| 173 | // this is a no-op if we have already closed for some other reason. |
| 174 | _ = u.enc.Close(websocket.StatusNormalClosure) |
| 175 | }() |
| 176 | |
| 177 | for { |
| 178 | select { |
| 179 | case <-u.watcherCtx.Done(): |
| 180 | u.errorThenClose(workspacesdk.WatchError{ |
| 181 | Code: workspacesdk.WatchErrorServerShutdown, |
| 182 | Retryable: true, |
| 183 | Message: "server is shutting down", |
| 184 | }) |
| 185 | return |
| 186 | case <-u.connCtx.Done(): |
| 187 | return |
| 188 | case e := <-u.events: |
| 189 | if e.sync { |
| 190 | // zero this out so we'll send a full update |
| 191 | u.lastBuild = database.GetLatestWorkspaceBuildWithStatusByWorkspaceIDRow{} |
| 192 | if !u.buildUpdate() { |
| 193 | return |
| 194 | } |
| 195 | } |
| 196 | if e.wsEvent != nil { |
| 197 | switch e.wsEvent.Kind { |
| 198 | case wspubsub.WorkspaceEventKindStateChange: |
| 199 | if !u.buildUpdate() { |
| 200 | return |
| 201 | } |
| 202 | case wspubsub.WorkspaceEventKindAgentLifecycleUpdate: |
| 203 | if !u.maybeSendAgentUpdate() { |
| 204 | return |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | func (u *updater) buildUpdate() bool { |
| 213 | build, err := u.db.GetLatestWorkspaceBuildWithStatusByWorkspaceID(u.connCtx, u.workspaceID) |
no test coverage detected