Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BC-6338 - When rabbitmq restarts the preview service gets stuck #4729

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ metadata:
app: preview-generator
data:
NEST_LOG_LEVEL: "info"
EXIT_ON_ERROR: "true"
2 changes: 1 addition & 1 deletion apps/server/src/apps/files-storage-consumer.app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { install as sourceMapInstall } from 'source-map-support';
async function bootstrap() {
sourceMapInstall();

const nestApp = await NestFactory.createMicroservice(FilesStorageAMQPModule);
const nestApp = await NestFactory.create(FilesStorageAMQPModule);
await nestApp.init();

console.log('#########################################');
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/apps/preview-generator-consumer.app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { install as sourceMapInstall } from 'source-map-support';
async function bootstrap() {
sourceMapInstall();

const nestApp = await NestFactory.createMicroservice(PreviewGeneratorAMQPModule);
const nestApp = await NestFactory.create(PreviewGeneratorAMQPModule);
await nestApp.init();

console.log('#############################################');
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/core/logger/interfaces/logger-config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export interface LoggerConfig {
NEST_LOG_LEVEL: string;
EXIT_ON_ERROR?: boolean;
}
2 changes: 1 addition & 1 deletion apps/server/src/core/logger/logger.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { Logger } from './logger';
return {
levels: winston.config.syslog.levels,
level: configService.get<string>('NEST_LOG_LEVEL'),
exitOnError: false,
exitOnError: configService.get<boolean>('EXIT_ON_ERROR') ?? false,
transports: [
new winston.transports.Console({
handleExceptions: true,
Expand Down
16 changes: 16 additions & 0 deletions apps/server/src/infra/healthcheck/healthcheck.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { DynamicModule, Module, Type } from '@nestjs/common';
import { TerminusModule } from '@nestjs/terminus';
import { AmqpConsumerHealthIndicator } from './indicators/amqp-consumer.indictor';

@Module({})
export class HealthCheckModule {
static register(controllers?: Type<any>[]): DynamicModule {
return {
module: HealthCheckModule,
imports: [TerminusModule],
providers: [AmqpConsumerHealthIndicator],
exports: [AmqpConsumerHealthIndicator],
controllers,
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { AmqpConnection } from '@golevelup/nestjs-rabbitmq';
import { HealthCheckError, HealthIndicator } from '@nestjs/terminus';

export class AmqpConsumerHealthIndicator extends HealthIndicator {
private amqpConnection!: AmqpConnection;

setAmqpConnection(connection: AmqpConnection) {
this.amqpConnection = connection;
}

public isHealthy(key: string) {
const { consumerTags, channel } = this.amqpConnection;

console.log('CHANNEL', channel);
console.log('CONSUMER_TAGS', consumerTags);

const isHealthy = consumerTags.length > 0;

const result = this.getStatus(key, isHealthy);

if (isHealthy) {
return result;
}
throw new HealthCheckError('Consumer check failed', result);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { DynamicModule, Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { HealthCheckModule } from '@infra/healthcheck/healthcheck.module';
import { RabbitMQWrapperModule } from '@infra/rabbitmq';
import { S3ClientAdapter, S3ClientModule } from '@infra/s3-client';
import { DynamicModule, Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { ScheduleModule } from '@nestjs/schedule';
import { createConfigModuleOptions } from '@src/config';
import { Logger, LoggerModule } from '@src/core/logger';
import { PreviewConfig } from './interface/preview-consumer-config';
Expand All @@ -25,6 +27,8 @@ export class PreviewGeneratorConsumerModule {
return {
module: PreviewGeneratorConsumerModule,
imports: [
HealthCheckModule.register(),
ScheduleModule.forRoot(),
LoggerModule,
S3ClientModule.register([storageConfig]),
RabbitMQWrapperModule,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
import { RabbitPayload, RabbitRPC } from '@golevelup/nestjs-rabbitmq';
import { AmqpConnection, RabbitPayload, RabbitRPC } from '@golevelup/nestjs-rabbitmq';
import { AmqpConsumerHealthIndicator } from '@infra/healthcheck/indicators/amqp-consumer.indictor';
import { FilesPreviewEvents, FilesPreviewExchange } from '@infra/rabbitmq';
import { Injectable } from '@nestjs/common';
import { Cron } from '@nestjs/schedule';
import { Logger } from '@src/core/logger';
import { PreviewFileOptions } from './interface';
import { PreviewActionsLoggable } from './loggable/preview-actions.loggable';
import { PreviewGeneratorService } from './preview-generator.service';

@Injectable()
export class PreviewGeneratorConsumer {
constructor(private readonly previewGeneratorService: PreviewGeneratorService, private logger: Logger) {
constructor(
private readonly previewGeneratorService: PreviewGeneratorService,
private logger: Logger,
private amqpConnection: AmqpConnection,
private health: AmqpConsumerHealthIndicator
) {
this.logger.setContext(PreviewGeneratorConsumer.name);
health.setAmqpConnection(amqpConnection);
}

@Cron('5 * * * * *')
healty() {
console.log(this.health.isHealthy('preview-generator-consumer'));
}

@RabbitRPC({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GetFile, S3ClientAdapter } from '@infra/s3-client';
import { Injectable, UnprocessableEntityException } from '@nestjs/common';
import { Injectable, OnModuleInit, UnprocessableEntityException } from '@nestjs/common';
import { Logger } from '@src/core/logger';
import m, { subClass } from 'gm';
import { PassThrough } from 'stream';
Expand All @@ -9,7 +9,11 @@ import { PreviewNotPossibleException } from './loggable/preview-exception';
import { PreviewGeneratorBuilder } from './preview-generator.builder';

@Injectable()
export class PreviewGeneratorService {
export class PreviewGeneratorService implements OnModuleInit {
onModuleInit() {
console.log('onModuleInit', PreviewGeneratorService.name);
}

private imageMagick = subClass({ imageMagick: '7+' });

constructor(private readonly storageClient: S3ClientAdapter, private logger: Logger) {
Expand Down
18 changes: 16 additions & 2 deletions config/default.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@
"default": "",
"description": "Add custom domain to the list of blocked domains (comma separated list)."
},
"BLOCKLIST_OF_EMAIL_DOMAINS":{
"BLOCKLIST_OF_EMAIL_DOMAINS": {
"type": "string",
"default": "",
"description": "Add custom domain to the list of blocked domains (comma separated list)."
Expand Down Expand Up @@ -1034,6 +1034,11 @@
"description": "Nest Log level for api. The http flag is for request logging. The http flag do only work by api methods with added 'request logging interceptor'.",
"enum": ["emerg", "alert", "crit", "error", "warning", "notice", "info", "debug"]
},
"EXIT_ON_ERROR": {
"type": "boolean",
"default": false,
"description": "By default, the application is not terminated in our project after an uncaughtException has been logged. If this is not the desired behavior, set exitOnError to true."
},
"SYSTEM_LOG_LEVEL": {
"type": "string",
"default": "requestError",
Expand Down Expand Up @@ -1452,7 +1457,16 @@
"TLDRAW": {
"type": "object",
"description": "Configuration of tldraw related settings",
"required": ["PING_TIMEOUT", "SOCKET_PORT","GC_ENABLED", "DB_FLUSH_SIZE", "MAX_DOCUMENT_SIZE", "ASSETS_ENABLED", "ASSETS_MAX_SIZE", "ASSETS_ALLOWED_EXTENSIONS_LIST"],
"required": [
"PING_TIMEOUT",
"SOCKET_PORT",
"GC_ENABLED",
"DB_FLUSH_SIZE",
"MAX_DOCUMENT_SIZE",
"ASSETS_ENABLED",
"ASSETS_MAX_SIZE",
"ASSETS_ALLOWED_EXTENSIONS_LIST"
],
"properties": {
"SOCKET_PORT": {
"type": "number",
Expand Down
Loading
Loading