2021-06-05 19:11:39 +08:00
|
|
|
import { RabbitSubscribe } from '@golevelup/nestjs-rabbitmq';
|
|
|
|
import { Injectable, OnModuleDestroy } from '@nestjs/common';
|
2021-06-05 21:34:19 +08:00
|
|
|
import { plainToClass } from 'class-transformer';
|
2021-06-05 19:11:39 +08:00
|
|
|
import { Observable, Subject } from 'rxjs';
|
|
|
|
import { filter, publish, tap } from 'rxjs/operators';
|
|
|
|
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();
|
|
|
|
@RabbitSubscribe({
|
|
|
|
exchange: EXCHANGE_PIPELINE_TASK_FANOUT,
|
|
|
|
routingKey: ROUTE_PIPELINE_TASK_LOG,
|
|
|
|
queue: QUEUE_HANDLE_PIPELINE_TASK_LOG_EVENT,
|
|
|
|
})
|
|
|
|
async handleEvent(message: PipelineTaskEvent) {
|
2021-06-05 21:34:19 +08:00
|
|
|
this.messageSubject.next(plainToClass(PipelineTaskEvent, message));
|
2021-06-05 19:11:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
getMessage$(taskId: string) {
|
|
|
|
return this.message$.pipe(
|
|
|
|
tap((val) => console.log(val)),
|
|
|
|
filter((event) => event.taskId === taskId),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
onModuleDestroy() {
|
|
|
|
this.messageSubject.complete();
|
|
|
|
}
|
|
|
|
}
|