|
@@ -11,7 +11,7 @@ class Scheduler {
|
|
|
this.notifier = new TelegramNotifier();
|
|
|
this.subscriptionManager = new SubscriptionManager();
|
|
|
this.isRunning = false;
|
|
|
- this.testInterval = parseInt(process.env.SPEED_TEST_INTERVAL) || 15; // 分钟
|
|
|
+ this.testInterval = parseInt(process.env.SPEED_TEST_INTERVAL) || 10; // 分钟
|
|
|
this.failureThreshold = parseInt(process.env.NOTIFICATION_FAILURE_THRESHOLD) || 3;
|
|
|
this.recoveryThreshold = parseInt(process.env.NOTIFICATION_RECOVERY_THRESHOLD) || 2;
|
|
|
}
|
|
@@ -20,16 +20,16 @@ class Scheduler {
|
|
|
* 启动调度器
|
|
|
*/
|
|
|
start() {
|
|
|
- logger.info('启动定时任务调度器 - 定时测速已关闭');
|
|
|
+ logger.info(`启动定时任务调度器 - 定时测速间隔: ${this.testInterval}分钟`);
|
|
|
|
|
|
- // 暂时关闭定时测速
|
|
|
- // const cronExpression = `*/${this.testInterval} * * * *`;
|
|
|
- // this.speedTestJob = cron.schedule(cronExpression, async () => {
|
|
|
- // await this.runSpeedTest();
|
|
|
- // }, {
|
|
|
- // scheduled: true,
|
|
|
- // timezone: 'Asia/Shanghai'
|
|
|
- // });
|
|
|
+ // 启用定时测速
|
|
|
+ const cronExpression = `*/${this.testInterval} * * * *`;
|
|
|
+ this.speedTestJob = cron.schedule(cronExpression, async () => {
|
|
|
+ await this.runSpeedTest();
|
|
|
+ }, {
|
|
|
+ scheduled: true,
|
|
|
+ timezone: 'Asia/Shanghai'
|
|
|
+ });
|
|
|
|
|
|
// 每小时重试失败的通知
|
|
|
this.notificationRetryJob = cron.schedule('0 * * * *', async () => {
|
|
@@ -99,6 +99,43 @@ class Scheduler {
|
|
|
// 批量测试节点
|
|
|
const testResults = await this.speedTester.testNodes(nodes);
|
|
|
|
|
|
+ // 检查是否有高延迟节点需要重测
|
|
|
+ const highLatencyNodes = [];
|
|
|
+ const highLatencyResults = [];
|
|
|
+
|
|
|
+ for (let i = 0; i < nodes.length; i++) {
|
|
|
+ const result = testResults[i];
|
|
|
+ if (result && result.isSuccess && result.latency && result.latency > 2000) {
|
|
|
+ highLatencyNodes.push(nodes[i]);
|
|
|
+ highLatencyResults.push(result);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果有高延迟节点,进行重测
|
|
|
+ if (highLatencyNodes.length > 0) {
|
|
|
+ logger.info(`发现 ${highLatencyNodes.length} 个高延迟节点,开始重测`);
|
|
|
+
|
|
|
+ // 重测高延迟节点
|
|
|
+ const retestResults = await this.speedTester.testNodes(highLatencyNodes);
|
|
|
+
|
|
|
+ // 更新测试结果,使用重测的结果
|
|
|
+ for (let i = 0; i < highLatencyNodes.length; i++) {
|
|
|
+ const node = highLatencyNodes[i];
|
|
|
+ const retestResult = retestResults[i];
|
|
|
+ const originalIndex = testResults.findIndex(r => r.nodeId === node.id);
|
|
|
+
|
|
|
+ if (originalIndex !== -1 && retestResult) {
|
|
|
+ // 如果重测结果更好,使用重测结果
|
|
|
+ if (retestResult.isSuccess && retestResult.latency && retestResult.latency <= 2000) {
|
|
|
+ testResults[originalIndex] = retestResult;
|
|
|
+ logger.info(`节点 ${node.name} 重测成功,延迟从 ${highLatencyResults[i].latency}ms 改善到 ${retestResult.latency}ms`);
|
|
|
+ } else {
|
|
|
+ logger.info(`节点 ${node.name} 重测后仍为高延迟: ${retestResult.latency || '超时'}ms`);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// 处理测试结果
|
|
|
await this.processTestResults(nodes, testResults);
|
|
|
|