fix: 修复重试次数设置无效的bug

pull/436/head
xiaojunnuo 2025-06-13 00:25:08 +08:00
parent c937583a50
commit e2099ac9ca
6 changed files with 17 additions and 17 deletions

View File

@ -130,7 +130,7 @@ export default function ({ crudExpose, context }: CreateCrudOptionsProps): Creat
await api.DoCheck(row.id); await api.DoCheck(row.id);
await crudExpose.doRefresh(); await crudExpose.doRefresh();
notification.success({ notification.success({
message: "检查完成", message: "检查任务已提交,请稍后刷新查看结果",
}); });
}, },
}, },

View File

@ -137,7 +137,7 @@ export default function ({ crudExpose, context }: CreateCrudOptionsProps): Creat
await api.DoCheck(row.id); await api.DoCheck(row.id);
await crudExpose.doRefresh(); await crudExpose.doRefresh();
notification.success({ notification.success({
message: "检查任务已提交", message: "检查任务已提交,请稍后刷新查看结果",
}); });
}, },
}, },

View File

@ -104,7 +104,7 @@ export class SiteInfoService extends BaseService<SiteInfoEntity> {
* @param notify * @param notify
* @param retryTimes * @param retryTimes
*/ */
async doCheck(site: SiteInfoEntity, notify = true, retryTimes = 3) { async doCheck(site: SiteInfoEntity, notify = true, retryTimes = null) {
if (!site?.domain) { if (!site?.domain) {
throw new Error("站点域名不能为空"); throw new Error("站点域名不能为空");
} }
@ -152,7 +152,7 @@ export class SiteInfoService extends BaseService<SiteInfoEntity> {
//检查ip //检查ip
await this.checkAllIp(site); await this.checkAllIp(site,retryTimes);
if (!notify) { if (!notify) {
return; return;
@ -181,7 +181,7 @@ export class SiteInfoService extends BaseService<SiteInfoEntity> {
} }
} }
async checkAllIp(site: SiteInfoEntity) { async checkAllIp(site: SiteInfoEntity,retryTimes = null) {
if (!site.ipCheck) { if (!site.ipCheck) {
return; return;
} }
@ -225,7 +225,7 @@ export class SiteInfoService extends BaseService<SiteInfoEntity> {
logger.error("send notify error", e); logger.error("send notify error", e);
} }
}; };
await this.siteIpService.checkAll(site, onFinished); await this.siteIpService.checkAll(site, retryTimes,onFinished);
} }
/** /**
@ -234,7 +234,7 @@ export class SiteInfoService extends BaseService<SiteInfoEntity> {
* @param notify * @param notify
* @param retryTimes * @param retryTimes
*/ */
async check(id: number, notify = false, retryTimes = 3) { async check(id: number, notify = false, retryTimes = null) {
const site = await this.info(id); const site = await this.info(id);
if (!site) { if (!site) {
throw new Error("站点不存在"); throw new Error("站点不存在");
@ -326,7 +326,6 @@ export class SiteInfoService extends BaseService<SiteInfoEntity> {
return setting; return setting;
} }
for (const site of sites) { for (const site of sites) {
let retryTimes = 3;
const setting = await getFromCache(site.userId) const setting = await getFromCache(site.userId)
if (isCommon) { if (isCommon) {
//公共的检查排除有设置cron的用户 //公共的检查排除有设置cron的用户
@ -334,8 +333,8 @@ export class SiteInfoService extends BaseService<SiteInfoEntity> {
//设置了cron跳过公共检查 //设置了cron跳过公共检查
continue; continue;
} }
retryTimes = setting.retryTimes??retryTimes
} }
let retryTimes = setting?.retryTimes
this.doCheck(site,true,retryTimes).catch(e => { this.doCheck(site,true,retryTimes).catch(e => {
logger.error(`检查站点证书失败,${site.domain}`, e.message); logger.error(`检查站点证书失败,${site.domain}`, e.message);
}); });

View File

@ -88,7 +88,7 @@ export class SiteIpService extends BaseService<SiteIpEntity> {
await this.updateIpCount(entity.id) await this.updateIpCount(entity.id)
} }
async check(ipId: number, domain: string, port: number) { async check(ipId: number, domain: string, port: number,retryTimes = null) {
if(!ipId){ if(!ipId){
return return
} }
@ -105,7 +105,7 @@ export class SiteIpService extends BaseService<SiteIpEntity> {
const res = await siteTester.test({ const res = await siteTester.test({
host: domain, host: domain,
port: port, port: port,
retryTimes: 3, retryTimes : retryTimes??3,
ipAddress: entity.ipAddress ipAddress: entity.ipAddress
}); });
@ -154,7 +154,7 @@ export class SiteIpService extends BaseService<SiteIpEntity> {
} }
} }
async checkAll(siteInfo: SiteInfoEntity,onFinish?: (e: any) => void) { async checkAll(siteInfo: SiteInfoEntity,retryTimes = null,onFinish?: (e: any) => void) {
const siteId = siteInfo.id; const siteId = siteInfo.id;
const ips = await this.repository.find({ const ips = await this.repository.find({
where: { where: {
@ -167,7 +167,7 @@ export class SiteIpService extends BaseService<SiteIpEntity> {
for (const item of ips) { for (const item of ips) {
const func = async () => { const func = async () => {
try { try {
return await this.check(item.id, domain, port); return await this.check(item.id, domain, port,retryTimes);
} catch (e) { } catch (e) {
logger.error("check site item error", e); logger.error("check site item error", e);
return { return {

View File

@ -18,7 +18,7 @@ export type SiteTestRes = {
export class SiteTester { export class SiteTester {
async test(req: SiteTestReq): Promise<SiteTestRes> { async test(req: SiteTestReq): Promise<SiteTestRes> {
logger.info("测试站点:", JSON.stringify(req)); logger.info("测试站点:", JSON.stringify(req));
const maxRetryTimes = req.retryTimes ?? 3; const maxRetryTimes = req.retryTimes==null ? 3 : req.retryTimes;
let tryCount = 0; let tryCount = 0;
let result: SiteTestRes = {}; let result: SiteTestRes = {};
while (true) { while (true) {
@ -28,12 +28,12 @@ export class SiteTester {
} catch (e) { } catch (e) {
tryCount++; tryCount++;
if (tryCount > maxRetryTimes) { if (tryCount > maxRetryTimes) {
logger.error(`测试站点出错,重试${maxRetryTimes}次。`, e.message); logger.error(`测试站点出错,已超过最大重试次数(${maxRetryTimes}`, e.message);
throw e; throw e;
} }
//指数退避 //指数退避
const time = 2 ** tryCount; const time = 2 ** tryCount;
logger.error(`测试站点出错,${time}s后重试`, e); logger.error(`测试站点出错,${time}s后重试(${tryCount}/${maxRetryTimes})`, e);
await utils.sleep(time * 1000); await utils.sleep(time * 1000);
} }
} }

View File

@ -59,7 +59,8 @@ export class TelegramNotification extends BaseNotification {
skipSslVerify: boolean; skipSslVerify: boolean;
replaceText(text: string) { replaceText(text: string) {
return text.replaceAll('.', '\\.').replaceAll('*', '\\*'); // .*()<> 等都需要用\\进行替换
return text.replace(/[\\.*()<>]/g, '\\$&');
} }
async send(body: NotificationBody) { async send(body: NotificationBody) {
if (!this.botToken || !this.chatId) { if (!this.botToken || !this.chatId) {