@@ -11,6 +11,8 @@ import { getDefaultWorkerHeaders } from "./util.js";
11
11
import { HeartbeatService } from "../../utils/heartbeat.js" ;
12
12
13
13
type SupervisorSessionOptions = SupervisorClientCommonOptions & {
14
+ queueConsumerEnabled ?: boolean ;
15
+ runNotificationsEnabled ?: boolean ;
14
16
heartbeatIntervalSeconds ?: number ;
15
17
dequeueIntervalMs ?: number ;
16
18
preDequeue ?: PreDequeueFn ;
@@ -20,15 +22,21 @@ type SupervisorSessionOptions = SupervisorClientCommonOptions & {
20
22
export class SupervisorSession extends EventEmitter < WorkerEvents > {
21
23
public readonly httpClient : SupervisorHttpClient ;
22
24
23
- private socket ?: Socket < WorkerServerToClientEvents , WorkerClientToServerEvents > ;
25
+ private readonly runNotificationsEnabled : boolean ;
26
+ private runNotificationsSocket ?: Socket < WorkerServerToClientEvents , WorkerClientToServerEvents > ;
24
27
28
+ private readonly queueConsumerEnabled : boolean ;
25
29
private readonly queueConsumer : RunQueueConsumer ;
30
+
26
31
private readonly heartbeatService : HeartbeatService ;
27
32
private readonly heartbeatIntervalSeconds : number ;
28
33
29
34
constructor ( private opts : SupervisorSessionOptions ) {
30
35
super ( ) ;
31
36
37
+ this . runNotificationsEnabled = opts . runNotificationsEnabled ?? true ;
38
+ this . queueConsumerEnabled = opts . queueConsumerEnabled ?? true ;
39
+
32
40
this . httpClient = new SupervisorHttpClient ( opts ) ;
33
41
this . queueConsumer = new RunQueueConsumer ( {
34
42
client : this . httpClient ,
@@ -76,12 +84,12 @@ export class SupervisorSession extends EventEmitter<WorkerEvents> {
76
84
subscribeToRunNotifications ( runFriendlyIds : string [ ] ) {
77
85
console . log ( "[SupervisorSession] Subscribing to run notifications" , { runFriendlyIds } ) ;
78
86
79
- if ( ! this . socket ) {
87
+ if ( ! this . runNotificationsSocket ) {
80
88
console . error ( "[SupervisorSession] Socket not connected" ) ;
81
89
return ;
82
90
}
83
91
84
- this . socket . emit ( "run:subscribe" , { version : "1" , runFriendlyIds } ) ;
92
+ this . runNotificationsSocket . emit ( "run:subscribe" , { version : "1" , runFriendlyIds } ) ;
85
93
86
94
Promise . allSettled (
87
95
runFriendlyIds . map ( ( runFriendlyId ) =>
@@ -96,12 +104,12 @@ export class SupervisorSession extends EventEmitter<WorkerEvents> {
96
104
unsubscribeFromRunNotifications ( runFriendlyIds : string [ ] ) {
97
105
console . log ( "[SupervisorSession] Unsubscribing from run notifications" , { runFriendlyIds } ) ;
98
106
99
- if ( ! this . socket ) {
107
+ if ( ! this . runNotificationsSocket ) {
100
108
console . error ( "[SupervisorSession] Socket not connected" ) ;
101
109
return ;
102
110
}
103
111
104
- this . socket . emit ( "run:unsubscribe" , { version : "1" , runFriendlyIds } ) ;
112
+ this . runNotificationsSocket . emit ( "run:unsubscribe" , { version : "1" , runFriendlyIds } ) ;
105
113
106
114
Promise . allSettled (
107
115
runFriendlyIds . map ( ( runFriendlyId ) =>
@@ -116,15 +124,15 @@ export class SupervisorSession extends EventEmitter<WorkerEvents> {
116
124
) ;
117
125
}
118
126
119
- private createSocket ( ) {
127
+ private createRunNotificationsSocket ( ) {
120
128
const wsUrl = new URL ( this . opts . apiUrl ) ;
121
129
wsUrl . pathname = "/worker" ;
122
130
123
- this . socket = io ( wsUrl . href , {
131
+ const socket = io ( wsUrl . href , {
124
132
transports : [ "websocket" ] ,
125
133
extraHeaders : getDefaultWorkerHeaders ( this . opts ) ,
126
134
} ) ;
127
- this . socket . on ( "run:notify" , ( { version, run } ) => {
135
+ socket . on ( "run:notify" , ( { version, run } ) => {
128
136
console . log ( "[SupervisorSession][WS] Received run notification" , { version, run } ) ;
129
137
this . emit ( "runNotification" , { time : new Date ( ) , run } ) ;
130
138
@@ -137,15 +145,17 @@ export class SupervisorSession extends EventEmitter<WorkerEvents> {
137
145
console . error ( "[SupervisorSession] Failed to send debug log" , { error } ) ;
138
146
} ) ;
139
147
} ) ;
140
- this . socket . on ( "connect" , ( ) => {
148
+ socket . on ( "connect" , ( ) => {
141
149
console . log ( "[SupervisorSession][WS] Connected to platform" ) ;
142
150
} ) ;
143
- this . socket . on ( "connect_error" , ( error ) => {
151
+ socket . on ( "connect_error" , ( error ) => {
144
152
console . error ( "[SupervisorSession][WS] Connection error" , { error } ) ;
145
153
} ) ;
146
- this . socket . on ( "disconnect" , ( reason , description ) => {
154
+ socket . on ( "disconnect" , ( reason , description ) => {
147
155
console . log ( "[SupervisorSession][WS] Disconnected from platform" , { reason, description } ) ;
148
156
} ) ;
157
+
158
+ return socket ;
149
159
}
150
160
151
161
async start ( ) {
@@ -167,14 +177,25 @@ export class SupervisorSession extends EventEmitter<WorkerEvents> {
167
177
name : workerGroup . name ,
168
178
} ) ;
169
179
170
- this . queueConsumer . start ( ) ;
171
- this . heartbeatService . start ( ) ;
172
- this . createSocket ( ) ;
180
+ if ( this . queueConsumerEnabled ) {
181
+ console . log ( "[SupervisorSession] Queue consumer enabled" ) ;
182
+ this . queueConsumer . start ( ) ;
183
+ this . heartbeatService . start ( ) ;
184
+ } else {
185
+ console . warn ( "[SupervisorSession] Queue consumer disabled" ) ;
186
+ }
187
+
188
+ if ( this . runNotificationsEnabled ) {
189
+ console . log ( "[SupervisorSession] Run notifications enabled" ) ;
190
+ this . runNotificationsSocket = this . createRunNotificationsSocket ( ) ;
191
+ } else {
192
+ console . warn ( "[SupervisorSession] Run notifications disabled" ) ;
193
+ }
173
194
}
174
195
175
196
async stop ( ) {
176
197
this . heartbeatService . stop ( ) ;
177
- this . socket ?. disconnect ( ) ;
198
+ this . runNotificationsSocket ?. disconnect ( ) ;
178
199
}
179
200
180
201
private getHeartbeatBody ( ) : WorkerApiHeartbeatRequestBody {
0 commit comments