|
| 1 | +import { Job } from 'bullmq'; |
| 2 | + |
| 3 | +import { Processor, WorkerHost } from '@nestjs/bullmq'; |
| 4 | +import { EventEmitter2 } from '@nestjs/event-emitter'; |
| 5 | +import { CommandBus, QueryBus } from '@nestjs/cqrs'; |
| 6 | +import { Logger } from '@nestjs/common'; |
| 7 | + |
| 8 | +import { GetSystemStatsCommand } from '@remnawave/node-contract'; |
| 9 | + |
| 10 | +import { ICommandResponse } from '@common/types/command-response.type'; |
| 11 | +import { AxiosService } from '@common/axios'; |
| 12 | +import { EVENTS } from '@libs/contracts/constants'; |
| 13 | + |
| 14 | +import { NodeEvent } from '@intergration-modules/telegram-bot/events/nodes/interfaces'; |
| 15 | + |
| 16 | +import { GetEnabledNodesQuery } from '@modules/nodes/queries/get-enabled-nodes'; |
| 17 | +import { UpdateNodeCommand } from '@modules/nodes/commands/update-node'; |
| 18 | +import { NodesEntity } from '@modules/nodes/entities/nodes.entity'; |
| 19 | + |
| 20 | +import { NodeHealthCheckJobNames } from './enums'; |
| 21 | +import { QueueNames } from '../queue.enum'; |
| 22 | + |
| 23 | +@Processor(QueueNames.nodeHealthCheck, { |
| 24 | + concurrency: 40, |
| 25 | +}) |
| 26 | +export class NodeHealthCheckQueueProcessor extends WorkerHost { |
| 27 | + private readonly logger = new Logger(NodeHealthCheckQueueProcessor.name); |
| 28 | + |
| 29 | + constructor( |
| 30 | + private readonly queryBus: QueryBus, |
| 31 | + private readonly commandBus: CommandBus, |
| 32 | + private readonly eventEmitter: EventEmitter2, |
| 33 | + private readonly axios: AxiosService, |
| 34 | + ) { |
| 35 | + super(); |
| 36 | + } |
| 37 | + async process(job: Job) { |
| 38 | + switch (job.name) { |
| 39 | + case NodeHealthCheckJobNames.checkNodeHealth: |
| 40 | + return this.handleCheckNodeHealthJob(job); |
| 41 | + default: |
| 42 | + this.logger.warn(`🚨 Job "${job.name}" is not handled.`); |
| 43 | + break; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + private async handleCheckNodeHealthJob(job: Job<NodesEntity>) { |
| 48 | + this.logger.debug( |
| 49 | + `✅ Handling "${NodeHealthCheckJobNames.checkNodeHealth}" job with ID: ${job?.id || ''}, data: ${JSON.stringify(job?.data || '')}`, |
| 50 | + ); |
| 51 | + |
| 52 | + try { |
| 53 | + const response = await this.axios.getSystemStats(job.data.address, job.data.port); |
| 54 | + switch (response.isOk) { |
| 55 | + case true: |
| 56 | + return this.handleConnectedNode(job.data, response.response!); |
| 57 | + case false: |
| 58 | + return this.handleDisconnectedNode(job.data, response.message); |
| 59 | + } |
| 60 | + } catch (error) { |
| 61 | + this.logger.error( |
| 62 | + `❌ Error handling "${NodeHealthCheckJobNames.checkNodeHealth}" job: ${error}`, |
| 63 | + ); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private async handleConnectedNode(node: NodesEntity, response: GetSystemStatsCommand.Response) { |
| 68 | + if (typeof response.response.uptime !== 'number') { |
| 69 | + this.logger.error(`Node ${node.uuid} uptime is not a number`); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + await this.updateNode({ |
| 74 | + node: { |
| 75 | + uuid: node.uuid, |
| 76 | + isConnected: true, |
| 77 | + isNodeOnline: true, |
| 78 | + isXrayRunning: true, |
| 79 | + lastStatusChange: new Date(), |
| 80 | + lastStatusMessage: '', |
| 81 | + }, |
| 82 | + }); |
| 83 | + |
| 84 | + if (!node.isConnected) { |
| 85 | + this.eventEmitter.emit( |
| 86 | + EVENTS.NODE.CONNECTION_RESTORED, |
| 87 | + new NodeEvent(node, EVENTS.NODE.CONNECTION_RESTORED), |
| 88 | + ); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private async handleDisconnectedNode(node: NodesEntity, message: string | undefined) { |
| 93 | + this.logger.debug(`Node ${node.uuid} is disconnected: ${message}`); |
| 94 | + |
| 95 | + const newNodeEntity = await this.updateNode({ |
| 96 | + node: { |
| 97 | + uuid: node.uuid, |
| 98 | + isConnected: false, |
| 99 | + isNodeOnline: false, |
| 100 | + isXrayRunning: false, |
| 101 | + lastStatusChange: new Date(), |
| 102 | + lastStatusMessage: message, |
| 103 | + usersOnline: 0, |
| 104 | + }, |
| 105 | + }); |
| 106 | + |
| 107 | + // this.eventBus.publish(new StartNodeEvent(newNodeEntity.response || node)); |
| 108 | + |
| 109 | + if (node.isConnected) { |
| 110 | + node.lastStatusMessage = message || null; |
| 111 | + this.eventEmitter.emit( |
| 112 | + EVENTS.NODE.CONNECTION_LOST, |
| 113 | + new NodeEvent(node, EVENTS.NODE.CONNECTION_LOST), |
| 114 | + ); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private async getEnabledNodes(): Promise<ICommandResponse<NodesEntity[]>> { |
| 119 | + return this.queryBus.execute<GetEnabledNodesQuery, ICommandResponse<NodesEntity[]>>( |
| 120 | + new GetEnabledNodesQuery(), |
| 121 | + ); |
| 122 | + } |
| 123 | + |
| 124 | + private async updateNode(dto: UpdateNodeCommand): Promise<ICommandResponse<NodesEntity>> { |
| 125 | + return this.commandBus.execute<UpdateNodeCommand, ICommandResponse<NodesEntity>>( |
| 126 | + new UpdateNodeCommand(dto.node), |
| 127 | + ); |
| 128 | + } |
| 129 | +} |
0 commit comments