feat(center-nodes): add node heartbeat endpoint
This commit is contained in:
@@ -1,4 +1,10 @@
|
||||
import { Column, CreateDateColumn, Entity, PrimaryGeneratedColumn } from 'typeorm';
|
||||
import {
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
Entity,
|
||||
PrimaryGeneratedColumn,
|
||||
UpdateDateColumn,
|
||||
} from 'typeorm';
|
||||
|
||||
@Entity('guild_nodes')
|
||||
export class GuildNode {
|
||||
@@ -21,6 +27,12 @@ export class GuildNode {
|
||||
})
|
||||
status!: 'active' | 'offline' | 'revoked';
|
||||
|
||||
@Column({ type: 'datetime', nullable: true })
|
||||
lastHeartbeatAt!: Date | null;
|
||||
|
||||
@CreateDateColumn()
|
||||
createdAt!: Date;
|
||||
|
||||
@UpdateDateColumn()
|
||||
updatedAt!: Date;
|
||||
}
|
||||
|
||||
@@ -65,6 +65,27 @@ export class NodesController {
|
||||
};
|
||||
}
|
||||
|
||||
@Post(':nodeId/heartbeat')
|
||||
async heartbeat(@Param('nodeId') nodeId: string) {
|
||||
const node = await this.nodeRepo.findOne({ where: { nodeId } });
|
||||
if (!node) {
|
||||
throw new NotFoundException('node not found');
|
||||
}
|
||||
|
||||
node.lastHeartbeatAt = new Date();
|
||||
if (node.status !== 'revoked') {
|
||||
node.status = 'active';
|
||||
}
|
||||
|
||||
const saved = await this.nodeRepo.save(node);
|
||||
return {
|
||||
status: 'ok',
|
||||
nodeId: saved.nodeId,
|
||||
nodeStatus: saved.status,
|
||||
lastHeartbeatAt: saved.lastHeartbeatAt,
|
||||
};
|
||||
}
|
||||
|
||||
@Patch(':nodeId/status')
|
||||
async updateStatus(
|
||||
@Param('nodeId') nodeId: string,
|
||||
|
||||
Reference in New Issue
Block a user