@@ -5,6 +5,7 @@ import * as vscode from "vscode"
5
5
import { WebSocket } from "ws"
6
6
import { errToStr } from "./api-helper"
7
7
import { type Storage } from "./storage"
8
+ import { getMemoryLogger } from "./memoryLogger"
8
9
9
10
// These are the template IDs of our notifications.
10
11
// Maybe in the future we should avoid hardcoding
@@ -16,9 +17,16 @@ export class Inbox implements vscode.Disposable {
16
17
readonly #storage: Storage
17
18
#disposed = false
18
19
#socket: WebSocket
20
+ #messageCount = 0
21
+ #workspaceId: string
19
22
20
23
constructor ( workspace : Workspace , httpAgent : ProxyAgent , restClient : Api , storage : Storage ) {
24
+ const logger = getMemoryLogger ( )
21
25
this . #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 } )` )
22
30
23
31
const baseUrlRaw = restClient . getAxiosInstance ( ) . defaults . baseURL
24
32
if ( ! baseUrlRaw ) {
@@ -37,6 +45,8 @@ export class Inbox implements vscode.Disposable {
37
45
const socketProto = baseUrl . protocol === "https:" ? "wss:" : "ws:"
38
46
const socketUrl = `${ socketProto } //${ baseUrl . host } /api/v2/notifications/inbox/watch?format=plaintext&templates=${ watchTemplatesParam } &targets=${ watchTargetsParam } `
39
47
48
+ logger . debug ( `Connecting to inbox WebSocket at: ${ socketUrl } ` )
49
+
40
50
const coderSessionTokenHeader = "Coder-Session-Token"
41
51
this . #socket = new WebSocket ( new URL ( socketUrl ) , {
42
52
followRedirects : true ,
@@ -49,35 +59,72 @@ export class Inbox implements vscode.Disposable {
49
59
} )
50
60
51
61
this . #socket. on ( "open" , ( ) => {
62
+ logger . info ( `Inbox WebSocket connection opened for workspace: ${ workspace . id } ` )
52
63
this . #storage. writeToCoderOutputChannel ( "Listening to Coder Inbox" )
53
64
} )
54
65
55
66
this . #socket. on ( "error" , ( error ) => {
67
+ logger . error ( `Inbox WebSocket error for workspace: ${ workspace . id } ` , error )
56
68
this . notifyError ( error )
57
69
this . dispose ( )
58
70
} )
59
71
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
+
60
79
this . #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
+
61
88
try {
62
89
const inboxMessage = JSON . parse ( data . toString ( ) ) as GetInboxNotificationResponse
63
-
90
+ logger . debug ( `Inbox notification received: ${ inboxMessage . notification . title } ` )
64
91
vscode . window . showInformationMessage ( inboxMessage . notification . title )
65
92
} catch ( error ) {
93
+ logger . error ( `Error processing inbox message for workspace: ${ workspace . id } ` , error )
66
94
this . notifyError ( error )
67
95
}
68
96
} )
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
69
109
}
70
110
71
111
dispose ( ) {
112
+ const logger = getMemoryLogger ( )
113
+
72
114
if ( ! this . #disposed) {
115
+ logger . info ( `Disposing inbox for workspace: ${ this . #workspaceId} after ${ this . #messageCount} messages` )
73
116
this . #storage. writeToCoderOutputChannel ( "No longer listening to Coder Inbox" )
74
117
this . #socket. close ( )
75
118
this . #disposed = true
119
+ logger . trackResourceDisposed ( "InboxWebSocket" , this . #workspaceId)
76
120
}
77
121
}
78
122
79
123
private notifyError ( error : unknown ) {
124
+ const logger = getMemoryLogger ( )
80
125
const message = errToStr ( error , "Got empty error while monitoring Coder Inbox" )
126
+
127
+ logger . error ( `Inbox error for workspace: ${ this . #workspaceId} ` , error )
81
128
this . #storage. writeToCoderOutputChannel ( message )
82
129
}
83
130
}
0 commit comments