Skip to content

[server] Broadcast workspace instance update to listeners WEB-597 #18212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion components/server/src/messaging/redis-subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ import {
updateSubscribersRegistered,
} from "../prometheus-metrics";
import { Redis } from "ioredis";
import { WorkspaceDB } from "@gitpod/gitpod-db/lib";

@injectable()
export class RedisSubscriber implements LocalMessageBroker {
constructor(@inject(Redis) private readonly redis: Redis) {}
constructor(
@inject(Redis) private readonly redis: Redis,
@inject(WorkspaceDB) private readonly workspaceDB: WorkspaceDB,
) {}

protected workspaceInstanceUpdateListeners: Map<string, WorkspaceInstanceUpdateListener[]> = new Map();

Expand Down Expand Up @@ -79,6 +83,33 @@ export class RedisSubscriber implements LocalMessageBroker {

private async onInstanceUpdate(update: RedisWorkspaceInstanceUpdate): Promise<void> {
log.debug("[redis] Received instance update", { update });

if (!update.ownerID || !update.instanceID) {
return;
}

const listeners = this.workspaceInstanceUpdateListeners.get(update.ownerID) || [];
if (listeners.length === 0) {
return;
}

const ctx = {};
const instance = await this.workspaceDB.findInstanceById(update.instanceID);
if (!instance) {
return;
}

for (const l of listeners) {
try {
l(ctx, instance);
} catch (err) {
log.error(
{ userId: update.ownerID, instanceId: instance.id, workspaceId: update.workspaceID },
"Failed to broadcast workspace instance update.",
err,
);
}
}
}

async stop() {
Expand Down