Skip to content

Commit

Permalink
[ws-man-bridge] Publish ws-instance-update - WEB-592 (#18162)
Browse files Browse the repository at this point in the history
* [ws-man-bridge] Publish ws-instance-update

* tests

* fix

* fix
  • Loading branch information
easyCZ authored Jul 4, 2023
1 parent f25767b commit 56d95d7
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 7 deletions.
1 change: 1 addition & 0 deletions components/gitpod-protocol/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ export * from "./context-url";
export * from "./teams-projects-protocol";
export * from "./snapshot-url";
export * from "./webhook-event";
export * from "./redis";
12 changes: 12 additions & 0 deletions components/gitpod-protocol/src/redis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Copyright (c) 2023 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License.AGPL.txt in the project root for license information.
*/

export const WorkspaceInstanceUpdatesChannel = "chan:workspace-instances";

export type RedisWorkspaceInstanceUpdate = {
instanceID: string;
workspaceID: string;
};
3 changes: 2 additions & 1 deletion components/ws-manager-bridge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"rimraf": "^3.0.2",
"ts-node": "^10.4.0",
"typescript": "~4.4.2",
"watch": "^1.0.2"
"watch": "^1.0.2",
"ioredis-mock": "^8.7.0"
}
}
5 changes: 4 additions & 1 deletion components/ws-manager-bridge/src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,10 @@ export class WorkspaceManagerBridge implements Disposable {
await lifecycleHandler();
}
await this.messagebus.notifyOnInstanceUpdate(ctx, userId, instance);
await this.publisher.publishInstanceUpdate();
await this.publisher.publishInstanceUpdate({
instanceID: instance.id,
workspaceID: instance.workspaceId,
});
} catch (e) {
TraceContext.setError({ span }, e);
throw e;
Expand Down
46 changes: 46 additions & 0 deletions components/ws-manager-bridge/src/redis/publisher.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License.AGPL.txt in the project root for license information.
*/

import { suite, test } from "@testdeck/mocha";
import * as chai from "chai";
import { RedisPublisher } from "./publisher";
import { Metrics } from "../metrics";
import { RedisClient } from "./client";
import { Container, ContainerModule } from "inversify";

const expect = chai.expect;
const Redis = require("ioredis-mock");

@suite
class TestRedisPublisher {
protected container: Container;

public before() {
const client = {
get: () => new Redis(),
} as RedisClient;

this.container = new Container();
this.container.load(
new ContainerModule((bind) => {
bind(Metrics).toSelf().inSingletonScope();
bind(RedisClient).toConstantValue(client);
bind(RedisPublisher).toSelf().inSingletonScope();
}),
);
}

@test public publishInstanceUpdate() {
const publisher = this.container.get(RedisPublisher);
expect(() => {
publisher.publishInstanceUpdate({
instanceID: "123",
workspaceID: "foo-bar-123",
});
}).not.to.throw;
}
}
module.exports = new TestRedisPublisher();
22 changes: 18 additions & 4 deletions components/ws-manager-bridge/src/redis/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,33 @@
import { inject, injectable } from "inversify";
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
import { Metrics } from "../metrics";
import { RedisClient } from "./client";
import { RedisWorkspaceInstanceUpdate, WorkspaceInstanceUpdatesChannel } from "@gitpod/gitpod-protocol";

@injectable()
export class RedisPublisher {
constructor(@inject(Metrics) private readonly metrics: Metrics) {}
constructor(
@inject(RedisClient) private readonly client: RedisClient,
@inject(Metrics) private readonly metrics: Metrics,
) {}

async publishPrebuildUpdate(): Promise<void> {
log.debug("[redis] Publish prebuild udpate invoked.");
this.metrics.reportUpdatePublished("prebuild");
}

async publishInstanceUpdate(): Promise<void> {
log.debug("[redis] Publish instance udpate invoked.");
this.metrics.reportUpdatePublished("workspace-instance");
async publishInstanceUpdate(update: RedisWorkspaceInstanceUpdate): Promise<void> {
let err: Error | undefined;
try {
const serialized = JSON.stringify(update);
await this.client.get().publish(WorkspaceInstanceUpdatesChannel, serialized);
log.debug("[redis] Succesfully published instance update.", update);
} catch (e) {
err = e;
log.error("[redis] Failed to publish instance update.", e, update);
} finally {
this.metrics.reportUpdatePublished("workspace-instance", err);
}
}

async publishHeadlessUpdate(): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,10 @@ export class WorkspaceInstanceControllerImpl implements WorkspaceInstanceControl
await this.onStopped(ctx, info.workspace.ownerId, info.latestInstance);

await this.messagebus.notifyOnInstanceUpdate(ctx, info.workspace.ownerId, info.latestInstance);
await this.publisher.publishInstanceUpdate();
await this.publisher.publishInstanceUpdate({
instanceID: info.latestInstance.id,
workspaceID: info.workspace.id,
});

await this.prebuildUpdater.stopPrebuildInstance(ctx, info.latestInstance);
}
Expand Down

0 comments on commit 56d95d7

Please sign in to comment.