@@ -59,22 +59,32 @@ export class WorkspaceManagerBridge implements Disposable {
59
59
protected cluster : WorkspaceClusterInfo ;
60
60
61
61
public start ( cluster : WorkspaceClusterInfo , clientProvider : ClientProvider ) {
62
- const logPayload = { name : cluster . name , url : cluster . url } ;
62
+ const logPayload = { name : cluster . name , url : cluster . url , govern : cluster . govern } ;
63
63
log . info ( `starting bridge to cluster...` , logPayload ) ;
64
64
this . cluster = cluster ;
65
65
66
- if ( cluster . govern ) {
67
- log . debug ( `starting DB updater : ${ cluster . name } ` , logPayload ) ;
68
- /* no await */ this . startDatabaseUpdater ( clientProvider , logPayload )
66
+ const startStatusUpdateHandler = ( writeToDB : boolean ) => {
67
+ log . debug ( `starting status update handler : ${ cluster . name } ` , logPayload ) ;
68
+ /* no await */ this . startStatusUpdateHandler ( clientProvider , writeToDB , logPayload )
69
69
// this is a mere safe-guard: we do not expect the code inside to fail
70
- . catch ( err => log . error ( "cannot start database updater" , err ) ) ;
70
+ . catch ( err => log . error ( "cannot start status update handler" , err ) ) ;
71
+ } ;
71
72
73
+ if ( cluster . govern ) {
74
+ // notify servers and _update the DB_
75
+ startStatusUpdateHandler ( true ) ;
76
+
77
+ // the actual "governing" part
72
78
const controllerInterval = this . config . controllerIntervalSeconds ;
73
79
if ( controllerInterval <= 0 ) {
74
80
throw new Error ( "controllerInterval <= 0!" ) ;
75
81
}
76
82
log . debug ( `starting controller: ${ cluster . name } ` , logPayload ) ;
77
83
this . startController ( clientProvider , controllerInterval , this . config . controllerMaxDisconnectSeconds ) ;
84
+ } else {
85
+ // _DO NOT_ update the DB (another bridge is responsible for that)
86
+ // Still, listen to all updates, generate/derive new state and distribute it locally!
87
+ startStatusUpdateHandler ( false ) ;
78
88
}
79
89
log . info ( `started bridge to cluster.` , logPayload ) ;
80
90
}
@@ -83,15 +93,15 @@ export class WorkspaceManagerBridge implements Disposable {
83
93
this . dispose ( ) ;
84
94
}
85
95
86
- protected async startDatabaseUpdater ( clientProvider : ClientProvider , logPayload : { } ) : Promise < void > {
96
+ protected async startStatusUpdateHandler ( clientProvider : ClientProvider , writeToDB : boolean , logPayload : { } ) : Promise < void > {
87
97
const subscriber = new WsmanSubscriber ( clientProvider ) ;
88
98
this . disposables . push ( subscriber ) ;
89
99
90
100
const onReconnect = ( ctx : TraceContext , s : WorkspaceStatus [ ] ) => {
91
- s . forEach ( sx => this . serializeMessagesByInstanceId < WorkspaceStatus > ( ctx , sx , m => m . getId ( ) , ( ctx , msg ) => this . handleStatusUpdate ( ctx , msg ) ) )
101
+ s . forEach ( sx => this . serializeMessagesByInstanceId < WorkspaceStatus > ( ctx , sx , m => m . getId ( ) , ( ctx , msg ) => this . handleStatusUpdate ( ctx , msg , writeToDB ) ) )
92
102
} ;
93
103
const onStatusUpdate = ( ctx : TraceContext , s : WorkspaceStatus ) => {
94
- this . serializeMessagesByInstanceId < WorkspaceStatus > ( ctx , s , msg => msg . getId ( ) , ( ctx , s ) => this . handleStatusUpdate ( ctx , s ) )
104
+ this . serializeMessagesByInstanceId < WorkspaceStatus > ( ctx , s , msg => msg . getId ( ) , ( ctx , s ) => this . handleStatusUpdate ( ctx , s , writeToDB ) )
95
105
} ;
96
106
await subscriber . subscribe ( { onReconnect, onStatusUpdate } , logPayload ) ;
97
107
}
@@ -110,7 +120,7 @@ export class WorkspaceManagerBridge implements Disposable {
110
120
this . queues . set ( instanceId , q ) ;
111
121
}
112
122
113
- protected async handleStatusUpdate ( ctx : TraceContext , rawStatus : WorkspaceStatus ) {
123
+ protected async handleStatusUpdate ( ctx : TraceContext , rawStatus : WorkspaceStatus , writeToDB : boolean ) {
114
124
const status = rawStatus . toObject ( ) ;
115
125
if ( ! status . spec || ! status . metadata || ! status . conditions ) {
116
126
log . warn ( "Received invalid status update" , status ) ;
@@ -123,6 +133,7 @@ export class WorkspaceManagerBridge implements Disposable {
123
133
124
134
const span = TraceContext . startSpan ( "handleStatusUpdate" , ctx ) ;
125
135
span . setTag ( "status" , JSON . stringify ( filterStatus ( status ) ) ) ;
136
+ span . setTag ( "writeToDB" , writeToDB ) ;
126
137
try {
127
138
// Beware of the ID mapping here: What's a workspace to the ws-manager is a workspace instance to the rest of the system.
128
139
// The workspace ID of ws-manager is the workspace instance ID in the database.
@@ -246,20 +257,26 @@ export class WorkspaceManagerBridge implements Disposable {
246
257
break ;
247
258
}
248
259
249
- await this . updatePrebuiltWorkspace ( { span} , status ) ;
250
-
251
260
span . setTag ( "after" , JSON . stringify ( instance ) ) ;
252
- await this . workspaceDB . trace ( { span} ) . storeInstance ( instance ) ;
253
- await this . messagebus . notifyOnInstanceUpdate ( { span} , userId , instance ) ;
254
261
255
- // important: call this after the DB update
256
- await this . cleanupProbeWorkspace ( { span} , status ) ;
262
+ // now notify all prebuild listeners about updates - and update DB if needed
263
+ await this . updatePrebuiltWorkspace ( { span} , userId , status , writeToDB ) ;
257
264
258
- if ( ! ! lifecycleHandler ) {
259
- await lifecycleHandler ( ) ;
265
+ if ( writeToDB ) {
266
+ await this . workspaceDB . trace ( ctx ) . storeInstance ( instance ) ;
267
+
268
+ // cleanup
269
+ // important: call this after the DB update
270
+ await this . cleanupProbeWorkspace ( ctx , status ) ;
271
+
272
+ if ( ! ! lifecycleHandler ) {
273
+ await lifecycleHandler ( ) ;
274
+ }
260
275
}
276
+ await this . messagebus . notifyOnInstanceUpdate ( ctx , userId , instance ) ;
277
+
261
278
} catch ( e ) {
262
- TraceContext . setError ( { span } , e ) ;
279
+ TraceContext . setError ( { span} , e ) ;
263
280
throw e ;
264
281
} finally {
265
282
span . finish ( ) ;
@@ -321,7 +338,7 @@ export class WorkspaceManagerBridge implements Disposable {
321
338
// probes are an EE feature - we just need the hook here
322
339
}
323
340
324
- protected async updatePrebuiltWorkspace ( ctx : TraceContext , status : WorkspaceStatus . AsObject ) {
341
+ protected async updatePrebuiltWorkspace ( ctx : TraceContext , userId : string , status : WorkspaceStatus . AsObject , writeToDB : boolean ) {
325
342
// prebuilds are an EE feature - we just need the hook here
326
343
}
327
344
0 commit comments