perf: 站点IP监控前先同步一下IP

This commit is contained in:
xiaojunnuo
2025-07-01 22:33:27 +08:00
parent 7c0f43c8a3
commit a080b606ab
4 changed files with 51 additions and 21 deletions
@@ -225,7 +225,7 @@ export class SiteInfoService extends BaseService<SiteInfoEntity> {
logger.error("send notify error", e);
}
};
await this.siteIpService.checkAll(site, retryTimes,onFinished);
await this.siteIpService.syncAndCheck(site, retryTimes,onFinished);
}
/**
@@ -59,33 +59,58 @@ export class SiteIpService extends BaseService<SiteIpEntity> {
async sync(entity: SiteInfoEntity) {
async sync(entity: SiteInfoEntity,check:boolean = true) {
const domain = entity.domain;
//从域名解析中获取所有ip
const ips = await this.getAllIpsFromDomain(domain);
if (ips.length === 0 ) {
throw new Error(`没有发现${domain}的IP`)
logger.warn(`没有发现${domain}的IP`)
return
}
//删除所有的ip
await this.repository.delete({
siteId: entity.id,
from: "sync"
});
//添加新的ip
for (const ip of ips) {
await this.repository.save({
ipAddress: ip,
userId: entity.userId,
const oldIps = await this.repository.find({
where:{
siteId: entity.id,
from:"sync"
}
})
let hasChanged = true
if (oldIps.length === ips.length ){
//检查是否有变化
const oldIpList = oldIps.map(ip=>ip.ipAddress).sort().join(",")
const newIpList = ips.filter(ip=>!oldIpList.includes(ip)).sort().join(",")
if(oldIpList === newIpList){
//无变化
hasChanged = false
}
}
if(hasChanged){
logger.info(`发现${domain}的IP变化,需要更新,旧IP:${oldIps.map(ip=>ip.ipAddress).join(",")},新IP:${ips.join(",")}`)
//有变化需要更新
//删除所有的ip
await this.repository.delete({
siteId: entity.id,
from: "sync",
disabled:false,
from: "sync"
});
}
await this.checkAll(entity);
await this.updateIpCount(entity.id)
//添加新的ip
for (const ip of ips) {
await this.repository.save({
ipAddress: ip,
userId: entity.userId,
siteId: entity.id,
from: "sync",
disabled:false,
});
await this.updateIpCount(entity.id)
}
}
if (check){
await this.checkAll(entity);
}
}
async check(ipId: number, domain: string, port: number,retryTimes = null) {
@@ -278,4 +303,9 @@ export class SiteIpService extends BaseService<SiteIpEntity> {
};
await batchAdd(list);
}
async syncAndCheck(siteEntity:SiteInfoEntity,retryTimes = null,onFinish?: (e: any) => void){
await this.sync(siteEntity,false);
await this.checkAll(siteEntity,retryTimes,onFinish);
}
}