Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import {
Injectable,
Logger,
OnModuleDestroy,
OnModuleInit,
} from "@nestjs/common";
import { AgentService } from "../asker/agent.service";
import { IConfAgent } from "../agent.config";
import { ConsumerService } from "@ocm-engine/nats";
import { ConfigService } from "@nestjs/config";
@Injectable()
export class AgentConsumerService implements OnModuleInit, OnModuleDestroy {
private readonly logger = new Logger(AgentConsumerService.name);
constructor(
private readonly agentService: AgentService,
private readonly consumerService: ConsumerService,
private readonly configService: ConfigService,
) {}
async onModuleInit(): Promise<void> {
const config = this.configService.get<IConfAgent>("agent");
if (config?.agentIsRest) {
this.logger.log(
"Agent is configured as rest, there is no need for consumer!",
);
return;
}
await this.consumerService.subscribe(async (event) => {
this.logger.debug(JSON.stringify(event, null, 2));
//TODO: transform the event dto to event<DTO> type
//call the service methods
//call the gateway webhook
return;
});
}
async onModuleDestroy(): Promise<void> {
this.logger.log("disconnecting from broker");
await this.consumerService.disconnect();
}
}