Skip to content

Commit

Permalink
BC-7851 - move logic to y-redis
Browse files Browse the repository at this point in the history
  • Loading branch information
SevenWaysDP committed Sep 19, 2024
1 parent 62f6e85 commit 47e8f8c
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 56 deletions.
7 changes: 3 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@nestjs/core": "^10.4.1",
"@nestjs/passport": "^10.0.3",
"@nestjs/platform-express": "^10.4.1",
"@y/redis": "github:hpi-schul-cloud/y-redis#9896f03907f2a1c1dc26b785a2a10ea45ca493b1",
"@y/redis": "github:hpi-schul-cloud/y-redis#7d48e08d18ec78c9ab90063a7d867ec7f191319c",
"ioredis": "^5.4.1",
"passport": "^0.7.0",
"passport-headerapikey": "^1.2.2",
Expand Down
16 changes: 11 additions & 5 deletions src/infra/redis/redis.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ import { Logger } from '../logging/logger.js';
export class RedisService {
private sentinelServiceName: string;
private internalRedisInstance?: Redis;
private redisDeletionKey: string;
private redisDeletionActionKey: string;

public constructor(
private configService: ConfigService,
private logger: Logger,
) {
this.sentinelServiceName = this.configService.get<string>('REDIS_SENTINEL_SERVICE_NAME') ?? '';
const redisPrefix = this.configService.get<string>('REDIS_PREFIX') ?? 'y';

this.redisDeletionKey = `${redisPrefix}:delete`;
this.redisDeletionActionKey = `${redisPrefix}:delete:action`;

this.logger.setContext(RedisService.name);
}
Expand All @@ -33,14 +39,14 @@ export class RedisService {
public async addDeleteDocument(docName: string): Promise<void> {
const redisInstance = await this.getInternalRedisInstance();

await redisInstance.xadd('delete', '*', 'docName', docName);
await redisInstance.publish('delete', docName);
await redisInstance.xadd(this.redisDeletionKey, '*', 'docName', docName);
await redisInstance.publish(this.redisDeletionActionKey, docName);
}

public async subscribeToDeleteChannel(callback: (message: string) => void): Promise<void> {
const redisInstance = await this.getInternalRedisInstance();
redisInstance.subscribe('delete');
redisInstance.on('message', (chan, message) => {
const redisSubscriberInstance = await this.createRedisInstance();
redisSubscriberInstance.subscribe(this.redisDeletionActionKey);
redisSubscriberInstance.on('message', (chan, message) => {
callback(message);
});
}
Expand Down
23 changes: 0 additions & 23 deletions src/infra/storage/storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Logger } from '../logging/logger.js';

@Injectable()
export class StorageService {
private internalStorageInstance?: any;
private s3Endpoint: string;
private bucketName: string;

Expand Down Expand Up @@ -39,26 +38,4 @@ export class StorageService {

return store;
}

public async deleteDocument(parentId: string): Promise<void> {
const store = await this.getInternalStorageInstance();

const objectsList = [];
const stream = store.client.listObjectsV2(this.bucketName, parentId, true);

for await (const obj of stream) {
objectsList.push(obj.name);
}

await store.client.removeObjects(this.bucketName, objectsList);
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
private async getInternalStorageInstance(): Promise<any> {
if (!this.internalStorageInstance) {
this.internalStorageInstance = await this.get();
}

return this.internalStorageInstance;
}
}
16 changes: 3 additions & 13 deletions src/modules/server/service/tldraw-document.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { TldrawDocumentService } from './tldraw-document.service.js';
import { TemplatedApp } from 'uws';
import { RedisService } from '../../../infra/redis/index.js';
import { StorageService } from '../../../infra/storage/index.js';
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { TemplatedApp } from 'uws';
import { TldrawDocumentService } from './tldraw-document.service.js';

describe('Tldraw-Document Service', () => {
let app: INestApplication;
Expand Down Expand Up @@ -51,23 +51,13 @@ describe('Tldraw-Document Service', () => {

expect(webSocketServer.publish).toHaveBeenCalledWith(docName, expectedMessage);
});

it('should call storageService deleteDocument', async () => {
const { parentId, docName, expectedMessage } = setup();

await service.deleteByDocName(parentId);

expect(storageService.deleteDocument).toHaveBeenCalledWith(parentId);
});
});

describe('when storage service throws error', () => {
const setup = () => {
const error = new Error('error');
const parentId = '123';

storageService.deleteDocument.mockRejectedValueOnce(error);

return { error, parentId };
};

Expand Down
14 changes: 4 additions & 10 deletions src/modules/server/service/tldraw-document.service.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
import { Injectable, Inject } from '@nestjs/common';
import { Inject, Injectable } from '@nestjs/common';
import { TemplatedApp } from 'uws';
import { RedisService } from '../../../infra/redis/index.js';
import { StorageService } from '../../../infra/storage/index.js';
import { TemplatedApp, WebSocketBehavior } from 'uws';
// @ts-expect-error - @y/redis is only having jsdoc types
import { Api } from '@y/redis';
const UWS = 'UWS';

@Injectable()
export class TldrawDocumentService {
constructor(
private readonly storageService: StorageService,
public constructor(
@Inject(UWS) private webSocketServer: TemplatedApp,
private readonly redisService: RedisService,
) {}

async deleteByDocName(parentId: string) {
public async deleteByDocName(parentId: string): Promise<void> {
const docName = `y:room:${parentId}:index`;

this.webSocketServer.publish(docName, 'action:delete');

await this.redisService.addDeleteDocument(docName);

await this.storageService.deleteDocument(parentId);
}
}

0 comments on commit 47e8f8c

Please sign in to comment.