Path: blob/main/components/gitpod-db/src/redis/publisher.ts
2500 views
/**1* Copyright (c) 2023 Gitpod GmbH. All rights reserved.2* Licensed under the GNU Affero General Public License (AGPL).3* See License.AGPL.txt in the project root for license information.4*/5import "reflect-metadata";67import { inject, injectable } from "inversify";8import { log } from "@gitpod/gitpod-protocol/lib/util/logging";9import {10HeadlessUpdatesChannel,11PrebuildUpdatesChannel,12RedisHeadlessUpdate,13RedisPrebuildUpdate,14RedisWorkspaceInstanceUpdate,15WorkspaceInstanceUpdatesChannel,16} from "@gitpod/gitpod-protocol";17import { Redis } from "ioredis";18import { reportUpdatePublished } from "./metrics";1920@injectable()21export class RedisPublisher {22constructor(@inject(Redis) private readonly redis: Redis) {}2324async publishPrebuildUpdate(update: RedisPrebuildUpdate): Promise<void> {25log.debug("[redis] Publish prebuild update invoked.");2627let err: Error | undefined;28try {29const serialized = JSON.stringify(update);30await this.redis.publish(PrebuildUpdatesChannel, serialized);31log.debug("[redis] Successfully published prebuild update.", update);32} catch (e) {33err = e;34log.error("[redis] Failed to publish prebuild update.", e, update);35} finally {36reportUpdatePublished("prebuild", err);37}38}3940async publishInstanceUpdate(update: RedisWorkspaceInstanceUpdate): Promise<void> {41let err: Error | undefined;42try {43const serialized = JSON.stringify(update);44await this.redis.publish(WorkspaceInstanceUpdatesChannel, serialized);45log.debug("[redis] Successfully published instance update.", update);46} catch (e) {47err = e;48log.error("[redis] Failed to publish instance update.", e, update);49} finally {50reportUpdatePublished("workspace-instance", err);51}52}5354async publishHeadlessUpdate(update: RedisHeadlessUpdate): Promise<void> {55log.debug("[redis] Publish headless update invoked.");5657let err: Error | undefined;58try {59const serialized = JSON.stringify(update);60await this.redis.publish(HeadlessUpdatesChannel, serialized);61log.debug("[redis] Successfully published headless update.", update);62} catch (e) {63err = e;64log.error("[redis] Failed to publish headless update.", e, update);65} finally {66reportUpdatePublished("headless", err);67}68}69}707172