fennec-be/src/pipeline-tasks/pipeline-task.logger.ts

38 lines
1.2 KiB
TypeScript
Raw Normal View History

2021-06-05 19:11:39 +08:00
import { RabbitSubscribe } from '@golevelup/nestjs-rabbitmq';
import { Injectable, OnModuleDestroy } from '@nestjs/common';
import { plainToClass } from 'class-transformer';
2021-06-05 19:11:39 +08:00
import { Observable, Subject } from 'rxjs';
2021-06-06 22:11:30 +08:00
import { filter } from 'rxjs/operators';
2021-06-05 19:11:39 +08:00
import { PipelineTaskEvent } from './models/pipeline-task-event';
import {
EXCHANGE_PIPELINE_TASK_FANOUT,
QUEUE_HANDLE_PIPELINE_TASK_LOG_EVENT,
ROUTE_PIPELINE_TASK_LOG,
} from './pipeline-tasks.constants';
@Injectable()
export class PipelineTaskLogger implements OnModuleDestroy {
private readonly messageSubject = new Subject<PipelineTaskEvent>();
private readonly message$: Observable<PipelineTaskEvent> = this.messageSubject.pipe();
2021-06-06 22:11:30 +08:00
2021-06-05 19:11:39 +08:00
@RabbitSubscribe({
exchange: EXCHANGE_PIPELINE_TASK_FANOUT,
routingKey: ROUTE_PIPELINE_TASK_LOG,
queue: QUEUE_HANDLE_PIPELINE_TASK_LOG_EVENT,
2021-06-06 22:11:30 +08:00
queueOptions: {
autoDelete: true,
},
2021-06-05 19:11:39 +08:00
})
async handleEvent(message: PipelineTaskEvent) {
this.messageSubject.next(plainToClass(PipelineTaskEvent, message));
2021-06-05 19:11:39 +08:00
}
getMessage$(taskId: string) {
2021-06-06 22:11:30 +08:00
return this.message$.pipe(filter((event) => event.taskId === taskId));
2021-06-05 19:11:39 +08:00
}
onModuleDestroy() {
this.messageSubject.complete();
}
}