Skip to content

Commit

Permalink
Merge branch 'main' into BC-7171
Browse files Browse the repository at this point in the history
  • Loading branch information
bergatco authored Dec 17, 2024
2 parents b47ee89 + 75374c5 commit 30f769a
Show file tree
Hide file tree
Showing 56 changed files with 2,191 additions and 2,632 deletions.
3 changes: 2 additions & 1 deletion .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ S3_SECRET_KEY=miniouser
S3_SSL=false

FEATURE_TLDRAW_ENABLED=true
TLDRAW_WEBSOCKET_URL=ws://localhost:3345
TLDRAW_WEBSOCKET_URL=ws://localhost:3399
TLDRAW_WEBSOCKET_PORT=3399

X_API_ALLOWED_KEYS=randomString
16 changes: 8 additions & 8 deletions ansible/roles/tldraw-server/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
state: "{{ 'present' if WITH_BRANCH_MONGO_DB_MANAGEMENT is defined and WITH_BRANCH_MONGO_DB_MANAGEMENT|bool else 'absent'}}"
when:
- EXTERNAL_SECRETS_OPERATOR is defined and EXTERNAL_SECRETS_OPERATOR|bool
- WITH_TLDRAW2 is defined and WITH_TLDRAW2|bool
- WITH_TLDRAW is defined and WITH_TLDRAW|bool
tags:
- 1password

Expand All @@ -17,7 +17,7 @@
template: onepassword.yml.j2
when:
- ONEPASSWORD_OPERATOR is defined and ONEPASSWORD_OPERATOR|bool
- WITH_TLDRAW2 is defined and WITH_TLDRAW2|bool
- WITH_TLDRAW is defined and WITH_TLDRAW|bool
tags:
- 1password

Expand All @@ -28,7 +28,7 @@
template: configmap.yml.j2
when:
- ONEPASSWORD_OPERATOR is defined and ONEPASSWORD_OPERATOR|bool
- WITH_TLDRAW2 is defined and WITH_TLDRAW2|bool
- WITH_TLDRAW is defined and WITH_TLDRAW|bool
tags:
- configmap

Expand All @@ -37,7 +37,7 @@
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
template: deployment.yml.j2
state: "{{ 'present' if WITH_TLDRAW2 else 'absent'}}"
state: "{{ 'present' if WITH_TLDRAW else 'absent'}}"
tags:
- deployment

Expand All @@ -46,7 +46,7 @@
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
template: worker-deployment.yml.j2
state: "{{ 'present' if WITH_TLDRAW2 else 'absent'}}"
state: "{{ 'present' if WITH_TLDRAW else 'absent'}}"
tags:
- deployment

Expand All @@ -55,7 +55,7 @@
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
template: server-svc.yml.j2
when: WITH_TLDRAW2 is defined and WITH_TLDRAW2|bool
when: WITH_TLDRAW is defined and WITH_TLDRAW|bool
tags:
- service

Expand All @@ -64,7 +64,7 @@
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
template: pod-monitor.yml.j2
when: WITH_TLDRAW2 is defined and WITH_TLDRAW2|bool
when: WITH_TLDRAW is defined and WITH_TLDRAW|bool
tags:
- prometheus

Expand All @@ -74,6 +74,6 @@
namespace: "{{ NAMESPACE }}"
template: ingress.yml.j2
apply: yes
when: WITH_TLDRAW2 is defined and WITH_TLDRAW2|bool
when: WITH_TLDRAW is defined and WITH_TLDRAW|bool
tags:
- ingress
4 changes: 2 additions & 2 deletions src/apps/tldraw-server.app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ async function bootstrap(): Promise<void> {
await metricsApp.listen(metricsPort, async () => {
const logger = await metricsApp.resolve(Logger);
logger.setContext('METRICS');
logger.log(`Metrics server is running on port ${metricsPort}`);
logger.info(`Metrics server is running on port ${metricsPort}`);
});

await nestApp.listen(httpPort, async () => {
const logger = await nestApp.resolve(Logger);
logger.setContext('TLDRAW');
logger.log(`Server is running on port ${httpPort}`);
logger.info(`Server is running on port ${httpPort}`);
});
}
bootstrap();
10 changes: 10 additions & 0 deletions src/apps/tldraw-worker.app.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import { NestFactory } from '@nestjs/core';
import { WorkerModule } from '../modules/worker/worker.module.js';
import { WorkerService } from '../modules/worker/worker.service.js';

async function bootstrap(): Promise<void> {
const nestApp = await NestFactory.createApplicationContext(WorkerModule);

await nestApp.init();
const workerService = await nestApp.resolve(WorkerService);

try {
workerService.start();
} catch (error) {
console.error(error);
workerService.stop();
process.exit(1);
}
}
bootstrap();
1 change: 0 additions & 1 deletion src/infra/authorization/authorization.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ export class AuthorizationService {

private createErrorResponsePayload(code: number, reason: string): ResponsePayload {
const response = ResponsePayloadBuilder.buildWithError(code, reason);
this.logger.log(`Error: ${code} - ${reason}`);

return response;
}
Expand Down
14 changes: 7 additions & 7 deletions src/infra/logger/logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { RequestLoggingBody } from './interfaces/logger.interface.js';
import { Logger } from './logger.js';

describe('Logger', () => {
let service: Logger;
let logger: Logger;
let processStdoutWriteSpy: jest.SpyInstance<
boolean,
[str: string | Uint8Array, encoding?: BufferEncoding | undefined, cb?: ((err?: Error) => void) | undefined],
Expand All @@ -30,7 +30,7 @@ describe('Logger', () => {
],
}).compile();

service = await module.resolve(Logger);
logger = await module.resolve(Logger);
winstonLogger = module.get(WINSTON_MODULE_PROVIDER);
});

Expand All @@ -44,26 +44,26 @@ describe('Logger', () => {
processStderrWriteSpy.mockRestore();
});

describe('WHEN log logging', () => {
describe('WHEN info logging', () => {
it('should call winstonLogger.info', () => {
const error = new Error('custom error');
service.log(error.message, error.stack);
logger.info(error.message, error.stack);
expect(winstonLogger.info).toHaveBeenCalled();
});
});

describe('WHEN warn logging', () => {
it('should call winstonLogger.warning', () => {
const error = new Error('custom error');
service.warn(error.message, error.stack);
logger.warning(error.message, error.stack);
expect(winstonLogger.warning).toHaveBeenCalled();
});
});

describe('WHEN debug logging', () => {
it('should call winstonLogger.debug', () => {
const error = new Error('custom error');
service.debug(error.message, error.stack);
logger.debug(error.message, error.stack);
expect(winstonLogger.debug).toHaveBeenCalled();
});
});
Expand All @@ -81,7 +81,7 @@ describe('Logger', () => {
},
error,
};
service.http(message, error.stack);
logger.http(message, error.stack);
expect(winstonLogger.notice).toHaveBeenCalled();
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/infra/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ export class Logger {

public constructor(@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: winston.Logger) {}

public log(message: unknown, context?: string): void {
public info(message: unknown, context?: string): void {
this.logger.info(this.createMessage(message, context));
}

public warn(message: unknown, context?: string): void {
public warning(message: unknown, context?: string): void {
this.logger.warning(this.createMessage(message, context));
}

Expand Down
4 changes: 2 additions & 2 deletions src/infra/metrics/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './metrics.module.js';
export * from './metrics.service.js';
export { MetricsModule } from './metrics.module.js';
export { MetricsService } from './metrics.service.js';
2 changes: 1 addition & 1 deletion src/infra/redis/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './redis.factory.js';
export * from './redis.module.js';
export * from './redis.service.js';
6 changes: 3 additions & 3 deletions src/infra/redis/interfaces/redis-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ export interface RedisAdapter {
exists(stream: string): Promise<number>;
createGroup(): Promise<void>;
quit(): Promise<void>;
readStreams(streams: StreamNameClockPair[]): Promise<StreamMessagesReply>;
readMessagesFromStream(streamName: string): Promise<StreamMessagesReply>;
readStreams(streams: StreamNameClockPair[]): Promise<StreamMessagesReply[]>;
readMessagesFromStream(streamName: string): Promise<StreamMessagesReply[]>;
reclaimTasks(consumerName: string, redisTaskDebounce: number, tryClaimCount: number): Promise<XAutoClaimResponse>;
getDeletedDocEntries(): Promise<StreamMessageReply[]>;
deleteDeleteDocEntry(id: string): Promise<number>;
deleteDeletedDocEntry(id: string): Promise<number>;
tryClearTask(task: Task): Promise<number>;
tryDeduplicateTask(task: Task, lastId: number, redisMinMessageLifetime: number): Promise<void>;
}
14 changes: 4 additions & 10 deletions src/infra/redis/interfaces/stream-message-reply.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
import { RedisKey } from 'ioredis';

interface Message {
key: RedisKey;
m?: RedisKey;
m?: Buffer;
docName?: string;
compact?: string;
compact?: Buffer;
}

export interface StreamMessageReply {
id: RedisKey;
message: Record<keyof Message, RedisKey>;
message: Message;
}

export interface StreamMessagesSingleReply {
export interface StreamMessagesReply {
name: string;
messages: StreamMessageReply[] | null;
}

export type StreamMessagesReply = {
name: string;
messages: StreamMessageReply[] | null;
}[];
6 changes: 3 additions & 3 deletions src/infra/redis/ioredis.adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ describe(IoRedisAdapter.name, () => {

await redisAdapter.createGroup();

expect(logger.log).toHaveBeenCalledWith(error);
expect(logger.info).toHaveBeenCalledWith(error);
});
});

Expand Down Expand Up @@ -412,15 +412,15 @@ describe(IoRedisAdapter.name, () => {
it('should call redis xdel with correct values', async () => {
const { id, xdelSpy, expectedProps, redisAdapter } = await setup();

await redisAdapter.deleteDeleteDocEntry(id);
await redisAdapter.deleteDeletedDocEntry(id);

expect(xdelSpy).toHaveBeenCalledWith(...expectedProps);
});

it('should return correct value', async () => {
const { id, redisAdapter } = await setup();

const result = await redisAdapter.deleteDeleteDocEntry(id);
const result = await redisAdapter.deleteDeletedDocEntry(id);

expect(result).toBe(1);
});
Expand Down
8 changes: 4 additions & 4 deletions src/infra/redis/ioredis.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class IoRedisAdapter implements RedisAdapter {
try {
await this.redis.xgroup('CREATE', this.redisWorkerStreamName, this.redisWorkerGroupName, '0', 'MKSTREAM');
} catch (e) {
this.logger.log(e);
this.logger.info(e);
// It is okay when the group already exists, so we can ignore this error.
if (e.message !== 'BUSYGROUP Consumer Group name already exists') {
throw e;
Expand All @@ -88,7 +88,7 @@ export class IoRedisAdapter implements RedisAdapter {
await this.redis.quit();
}

public async readStreams(streams: StreamNameClockPair[]): Promise<StreamMessagesReply> {
public async readStreams(streams: StreamNameClockPair[]): Promise<StreamMessagesReply[]> {
const reads = await this.redis.xreadBuffer(
'COUNT',
1000,
Expand All @@ -104,7 +104,7 @@ export class IoRedisAdapter implements RedisAdapter {
return streamReplyRes;
}

public async readMessagesFromStream(streamName: string): Promise<StreamMessagesReply> {
public async readMessagesFromStream(streamName: string): Promise<StreamMessagesReply[]> {
const reads = await this.redis.xreadBuffer('STREAMS', streamName, '0');

const streamReplyRes = mapToStreamMessagesReply(reads);
Expand Down Expand Up @@ -140,7 +140,7 @@ export class IoRedisAdapter implements RedisAdapter {
return transformedDeletedTasks;
}

public deleteDeleteDocEntry(id: string): Promise<number> {
public deleteDeletedDocEntry(id: string): Promise<number> {
const result = this.redis.xdel(this.redisDeleteStreamName, id);

return result;
Expand Down
2 changes: 1 addition & 1 deletion src/infra/redis/mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function mapToStreamMessagesReplies(messages: XItems | unknown): StreamMe
return result;
}

export function mapToStreamMessagesReply(streamReply: XReadBufferReply | unknown): StreamMessagesReply {
export function mapToStreamMessagesReply(streamReply: XReadBufferReply | unknown): StreamMessagesReply[] {
if (streamReply === null) {
return [];
}
Expand Down
Loading

0 comments on commit 30f769a

Please sign in to comment.