Skip to content

Commit b439372

Browse files
committed
[server] notify only browser clients on instance/prebuild updates
1 parent 72d59cb commit b439372

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

components/server/src/workspace/gitpod-server-impl.ts

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,8 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
285285

286286
private readonly disposables = new DisposableCollection();
287287

288+
private shouldNotifyClients = false;
289+
288290
dispose(): void {
289291
this.disposables.dispose();
290292
}
@@ -309,12 +311,20 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
309311
log.debug({ userId: this.userID }, `clientRegion: ${clientHeaderFields.clientRegion}`);
310312
log.debug({ userId: this.userID }, "initializeClient");
311313

312-
this.listenForWorkspaceInstanceUpdates();
314+
if (this.client && clientMetadata.type === "browser") {
315+
this.shouldNotifyClients = true;
316+
}
313317

314-
this.listenForPrebuildUpdates().catch((err) => log.error("error registering for prebuild updates", err));
318+
if (this.shouldNotifyClients) {
319+
this.listenForWorkspaceInstanceUpdates();
320+
this.listenForPrebuildUpdates().catch((err) => log.error("error registering for prebuild updates", err));
321+
}
315322
}
316323

317324
private async listenForPrebuildUpdates() {
325+
if (!this.shouldNotifyClients) {
326+
return;
327+
}
318328
// 'registering for prebuild updates for all projects this user has access to
319329
const projects = await this.getAccessibleProjects();
320330

@@ -331,7 +341,7 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
331341
);
332342

333343
for (const projectId of projects) {
334-
this.disposables.pushAll([this.subscriber.listenForPrebuildUpdates(projectId, handler)]);
344+
this.disposables.push(this.subscriber.listenForPrebuildUpdates(projectId, handler));
335345
}
336346

337347
// TODO(at) we need to keep the list of accessible project up to date
@@ -453,7 +463,8 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
453463
// Once we have those, we should remove this.
454464
//
455465
const ws = await this.workspaceDb.trace(ctx).findById(workspaceID);
456-
if (!!ws && !!wsi && ws.ownerId !== this.userID) {
466+
const relatedPrebuildFound = !!ws && !!wsi && ws.ownerId !== this.userID;
467+
if (this.client && relatedPrebuildFound) {
457468
const resetListenerFromRedis = this.subscriber.listenForWorkspaceInstanceUpdates(
458469
ws.ownerId,
459470
(ctx, instance) => {
@@ -529,7 +540,7 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
529540
}
530541

531542
private listenForWorkspaceInstanceUpdates(): void {
532-
if (!this.userID || !this.client) {
543+
if (!this.shouldNotifyClients || !this.userID) {
533544
return;
534545
}
535546

@@ -3046,19 +3057,23 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
30463057
}
30473058

30483059
const project = await this.projectsService.createProject(params, user);
3060+
30493061
// update client registration for the logged in user
3050-
const prebuildUpdateHandler = (ctx: TraceContext, update: PrebuildWithStatus) =>
3051-
TraceContext.withSpan(
3052-
"forwardPrebuildUpdateToClient",
3053-
(ctx) => {
3054-
traceClientMetadata(ctx, this.clientMetadata);
3055-
TraceContext.setJsonRPCMetadata(ctx, "onPrebuildUpdate");
3062+
if (this.shouldNotifyClients) {
3063+
const prebuildUpdateHandler = (ctx: TraceContext, update: PrebuildWithStatus) =>
3064+
TraceContext.withSpan(
3065+
"forwardPrebuildUpdateToClient",
3066+
(ctx) => {
3067+
traceClientMetadata(ctx, this.clientMetadata);
3068+
TraceContext.setJsonRPCMetadata(ctx, "onPrebuildUpdate");
3069+
3070+
this.client?.onPrebuildUpdate(update);
3071+
},
3072+
ctx,
3073+
);
30563074

3057-
this.client?.onPrebuildUpdate(update);
3058-
},
3059-
ctx,
3060-
);
3061-
this.disposables.pushAll([this.subscriber.listenForPrebuildUpdates(project.id, prebuildUpdateHandler)]);
3075+
this.disposables.pushAll([this.subscriber.listenForPrebuildUpdates(project.id, prebuildUpdateHandler)]);
3076+
}
30623077

30633078
return project;
30643079
}

0 commit comments

Comments
 (0)