Skip to content

Commit

Permalink
[gitpod-db] Include redis client to share WEB-621 (gitpod-io#18264)
Browse files Browse the repository at this point in the history
* [gitpod-db] Include redis client to share

* fix

* fix

* fix
  • Loading branch information
easyCZ authored Jul 25, 2023
1 parent 6d703bd commit bd9c0de
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 55 deletions.
3 changes: 2 additions & 1 deletion components/gitpod-db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"reflect-metadata": "^0.1.13",
"slugify": "^1.6.5",
"the-big-username-blacklist": "^1.5.2",
"typeorm": "0.2.38"
"typeorm": "0.2.38",
"ioredis": "^5.3.2"
},
"devDependencies": {
"@testdeck/mocha": "^0.3.3",
Expand Down
1 change: 1 addition & 0 deletions components/gitpod-db/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ export * from "./typeorm/metrics";
export * from "./personal-access-token-db";
export * from "./typeorm/entity/db-personal-access-token";
export * from "./linked-in-profile-db";
export * from "./redis/client";
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

import { Redis } from "ioredis";

export function newRedisClient(host: string, port: number): Redis {
export function newRedisClient(opts: { host: string; port: number; connectionName: string }): Redis {
return new Redis({
port: Number(port),
host,
port: opts.port,
host: opts.host,
enableReadyCheck: true,
keepAlive: 10 * 1000,
connectionName: "server",
connectionName: opts.connectionName,
});
}
4 changes: 2 additions & 2 deletions components/server/src/container-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ import { PrebuildManager } from "./prebuilds/prebuild-manager";
import { PrebuildStatusMaintainer } from "./prebuilds/prebuilt-status-maintainer";
import { StartPrebuildContextParser } from "./prebuilds/start-prebuild-context-parser";
import { ProjectsService } from "./projects/projects-service";
import { newRedisClient } from "./redis/client";
import { RedisMutex } from "./redis/mutex";
import { Server } from "./server";
import { SessionHandler } from "./session-handler";
Expand Down Expand Up @@ -128,6 +127,7 @@ import { OrganizationService } from "./orgs/organization-service";
import { RedisSubscriber } from "./messaging/redis-subscriber";
import { Redis } from "ioredis";
import { RedisPublisher } from "./redis/publisher";
import { newRedisClient } from "@gitpod/gitpod-db/lib";

export const productionContainerModule = new ContainerModule(
(bind, unbind, isBound, rebind, unbindAsync, onActivation, onDeactivation) => {
Expand Down Expand Up @@ -351,7 +351,7 @@ export const productionContainerModule = new ContainerModule(
bind(Redis).toDynamicValue((ctx) => {
const config = ctx.container.get<Config>(Config);
const [host, port] = config.redis.address.split(":");
return newRedisClient(host, Number(port));
return newRedisClient({ host, port: Number(port), connectionName: "server" });
});

bind(RedisMutex).toSelf().inSingletonScope();
Expand Down
9 changes: 7 additions & 2 deletions components/ws-manager-bridge/src/container-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ import { getExperimentsClientForBackend } from "@gitpod/gitpod-protocol/lib/expe
import { WorkspaceInstanceController, WorkspaceInstanceControllerImpl } from "./workspace-instance-controller";
import { AppClusterWorkspaceInstancesController } from "./app-cluster-instance-controller";
import { PrebuildUpdater } from "./prebuild-updater";
import { RedisClient } from "./redis/client";
import { RedisPublisher } from "./redis/publisher";
import { Redis } from "ioredis";
import { newRedisClient } from "@gitpod/gitpod-db/lib";

export const containerModule = new ContainerModule((bind) => {
bind(BridgeController).toSelf().inSingletonScope();
Expand Down Expand Up @@ -85,6 +86,10 @@ export const containerModule = new ContainerModule((bind) => {

bind(AppClusterWorkspaceInstancesController).toSelf().inSingletonScope();

bind(RedisClient).toSelf().inSingletonScope();
bind(Redis).toDynamicValue((ctx) => {
const config = ctx.container.get<Configuration>(Configuration);
const [host, port] = config.redis.address.split(":");
return newRedisClient({ host, port: Number(port), connectionName: "server" });
});
bind(RedisPublisher).toSelf().inSingletonScope();
});
32 changes: 0 additions & 32 deletions components/ws-manager-bridge/src/redis/client.ts

This file was deleted.

10 changes: 4 additions & 6 deletions components/ws-manager-bridge/src/redis/publisher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,24 @@ 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";
import { Redis } from "ioredis";

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

@suite
class TestRedisPublisher {
protected container: Container;

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

this.container = new Container();
this.container.load(
new ContainerModule((bind) => {
bind(Metrics).toSelf().inSingletonScope();
bind(RedisClient).toConstantValue(client);
bind(Redis).toConstantValue(client);
bind(RedisPublisher).toSelf().inSingletonScope();
}),
);
Expand Down
13 changes: 5 additions & 8 deletions components/ws-manager-bridge/src/redis/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import { inject, injectable } from "inversify";
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
import { Metrics } from "../metrics";
import { RedisClient } from "./client";
import {
HeadlessUpdatesChannel,
PrebuildUpdatesChannel,
Expand All @@ -16,21 +15,19 @@ import {
RedisWorkspaceInstanceUpdate,
WorkspaceInstanceUpdatesChannel,
} from "@gitpod/gitpod-protocol";
import { Redis } from "ioredis";

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

async publishPrebuildUpdate(update: RedisPrebuildUpdate): Promise<void> {
log.debug("[redis] Publish prebuild udpate invoked.");

let err: Error | undefined;
try {
const serialized = JSON.stringify(update);
await this.client.get().publish(PrebuildUpdatesChannel, serialized);
await this.redis.publish(PrebuildUpdatesChannel, serialized);
log.debug("[redis] Succesfully published prebuild update.", update);
} catch (e) {
err = e;
Expand All @@ -44,7 +41,7 @@ export class RedisPublisher {
let err: Error | undefined;
try {
const serialized = JSON.stringify(update);
await this.client.get().publish(WorkspaceInstanceUpdatesChannel, serialized);
await this.redis.publish(WorkspaceInstanceUpdatesChannel, serialized);
log.debug("[redis] Succesfully published instance update.", update);
} catch (e) {
err = e;
Expand All @@ -60,7 +57,7 @@ export class RedisPublisher {
let err: Error | undefined;
try {
const serialized = JSON.stringify(update);
await this.client.get().publish(HeadlessUpdatesChannel, serialized);
await this.redis.publish(HeadlessUpdatesChannel, serialized);
log.debug("[redis] Succesfully published headless update.", update);
} catch (e) {
err = e;
Expand Down

0 comments on commit bd9c0de

Please sign in to comment.