@@ -5,6 +5,7 @@ import * as vscode from "vscode"
55import { WebSocket } from "ws"
66import { errToStr } from "./api-helper"
77import { type Storage } from "./storage"
8+ import { getMemoryLogger } from "./memoryLogger"
89
910// These are the template IDs of our notifications.
1011// Maybe in the future we should avoid hardcoding
@@ -16,9 +17,16 @@ export class Inbox implements vscode.Disposable {
1617readonly #storage:Storage
1718 #disposed= false
1819 #socket:WebSocket
20+ #messageCount= 0
21+ #workspaceId:string
1922
2023constructor ( workspace :Workspace , httpAgent :ProxyAgent , restClient :Api , storage :Storage ) {
24+ const logger = getMemoryLogger ( )
2125this . #storage= storage
26+ this . #workspaceId= workspace . id
27+
28+ logger . trackResourceCreated ( "InboxWebSocket" , workspace . id )
29+ logger . info ( `Creating inbox for workspace:${ workspace . owner_name } /${ workspace . name } (${ workspace . id } )` )
2230
2331const baseUrlRaw = restClient . getAxiosInstance ( ) . defaults . baseURL
2432if ( ! baseUrlRaw ) {
@@ -37,6 +45,8 @@ export class Inbox implements vscode.Disposable {
3745const socketProto = baseUrl . protocol === "https:" ?"wss:" :"ws:"
3846const socketUrl = `${ socketProto } //${ baseUrl . host } /api/v2/notifications/inbox/watch?format=plaintext&templates=${ watchTemplatesParam } &targets=${ watchTargetsParam } `
3947
48+ logger . debug ( `Connecting to inbox WebSocket at:${ socketUrl } ` )
49+
4050const coderSessionTokenHeader = "Coder-Session-Token"
4151this . #socket= new WebSocket ( new URL ( socketUrl ) , {
4252followRedirects :true ,
@@ -49,35 +59,72 @@ export class Inbox implements vscode.Disposable {
4959} )
5060
5161this . #socket. on ( "open" , ( ) => {
62+ logger . info ( `Inbox WebSocket connection opened for workspace:${ workspace . id } ` )
5263this . #storage. writeToCoderOutputChannel ( "Listening to Coder Inbox" )
5364} )
5465
5566this . #socket. on ( "error" , ( error ) => {
67+ logger . error ( `Inbox WebSocket error for workspace:${ workspace . id } ` , error )
5668this . notifyError ( error )
5769this . dispose ( )
5870} )
5971
72+ this . #socket. on ( "close" , ( code , reason ) => {
73+ logger . info ( `Inbox WebSocket closed for workspace:${ workspace . id } , code:${ code } , reason:${ reason || "none" } ` )
74+ if ( ! this . #disposed) {
75+ this . dispose ( )
76+ }
77+ } )
78+
6079this . #socket. on ( "message" , ( data ) => {
80+ this . #messageCount++
81+
82+ // Log periodic message stats
83+ if ( this . #messageCount% 10 === 0 ) {
84+ logger . info ( `Inbox received${ this . #messageCount} messages for workspace:${ workspace . id } ` )
85+ logger . logMemoryUsage ( "INBOX_WEBSOCKET" )
86+ }
87+
6188try {
6289const inboxMessage = JSON . parse ( data . toString ( ) ) as GetInboxNotificationResponse
63-
90+ logger . debug ( `Inbox notification received: ${ inboxMessage . notification . title } ` )
6491vscode . window . showInformationMessage ( inboxMessage . notification . title )
6592} catch ( error ) {
93+ logger . error ( `Error processing inbox message for workspace:${ workspace . id } ` , error )
6694this . notifyError ( error )
6795}
6896} )
97+
98+ // Log memory stats periodically
99+ const memoryInterval = setInterval (
100+ ( ) => {
101+ if ( ! this . #disposed) {
102+ logger . logMemoryUsage ( "INBOX_PERIODIC" )
103+ } else {
104+ clearInterval ( memoryInterval )
105+ }
106+ } ,
107+ 5 * 60 * 1000 ,
108+ ) // Every 5 minutes
69109}
70110
71111dispose ( ) {
112+ const logger = getMemoryLogger ( )
113+
72114if ( ! this . #disposed) {
115+ logger . info ( `Disposing inbox for workspace:${ this . #workspaceId} after${ this . #messageCount} messages` )
73116this . #storage. writeToCoderOutputChannel ( "No longer listening to Coder Inbox" )
74117this . #socket. close ( )
75118this . #disposed= true
119+ logger . trackResourceDisposed ( "InboxWebSocket" , this . #workspaceId)
76120}
77121}
78122
79123private notifyError ( error :unknown ) {
124+ const logger = getMemoryLogger ( )
80125const message = errToStr ( error , "Got empty error while monitoring Coder Inbox" )
126+
127+ logger . error ( `Inbox error for workspace:${ this . #workspaceId} ` , error )
81128this . #storage. writeToCoderOutputChannel ( message )
82129}
83130}