Files
autoAiWorkSys/api/middleware/schedule/config.js
张成 5d7444cd65 1
2025-11-24 13:23:42 +08:00

150 lines
4.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const dayjs = require('dayjs');
const config = require('../../../config/config');
/**
* 调度系统配置中心
* 统一管理所有配置参数
*/
class ScheduleConfig {
constructor() {
// 工作时间配置
this.workHours = {
start: 9,
end: 18
};
// 频率限制配置(毫秒)
this.rateLimits = {
search: 30 * 60 * 1000, // 搜索间隔30分钟
apply: 5 * 60 * 1000, // 投递间隔5分钟
chat: 1 * 60 * 1000, // 聊天间隔1分钟
};
// 单日操作限制
this.dailyLimits = {
maxSearch: 20, // 每天最多搜索20次
maxApply: 50, // 每天最多投递50份简历
maxChat: 100, // 每天最多发送100条聊天
};
// 任务超时配置(毫秒)
this.taskTimeouts = {
get_login_qr_code: 30 * 1000, // 登录检查30秒
get_resume: 60 * 1000, // 获取简历1分钟
search_jobs: 5 * 60 * 1000, // 搜索岗位5分钟
chat: 30 * 1000, // 聊天30秒
apply: 30 * 1000 // 投递30秒
};
// 任务优先级配置
this.taskPriorities = {
get_login_qr_code: 10, // 最高优先级
get_resume: 9,
apply: 8,
auto_deliver: 7, // 自动投递任务
search_jobs: 6,
chat: 5,
cleanup: 1
};
// 监控配置
this.monitoring = {
heartbeatTimeout: 3 * 60 * 1000, // 心跳超时5分钟
taskFailureRate: 0.5, // 任务失败率50%
consecutiveFailures: 3, // 连续失败次数3次
alertCooldown: 5 * 60 * 1000, // 告警冷却5分钟
offlineThreshold: 24 * 60 * 60 * 1000 // 离线设备清理24小时
};
// 定时任务配置
this.schedules = {
dailyReset: '0 0 * * *', // 每天凌晨重置统计
jobFlowInterval: '0 */5 * * * *', // 每10秒执行一次找工作流程
monitoringInterval: '*/1 * * * *', // 监控检查间隔1分钟
autoDeliver: '0 */5 * * * *', // 自动投递任务每5分钟执行一次
// 监控检查间隔1分钟
};
// 测试配置覆盖
if (config.test) {
this.applyTestConfig(config.test);
}
}
/**
* 应用测试配置
* @param {object} testConfig - 测试配置
*/
applyTestConfig(testConfig) {
if (testConfig.skipWorkStartHour) {
this.workHours.start = 0;
this.workHours.end = 24;
}
// 测试模式下缩短所有间隔时间
if (testConfig.fastMode) {
this.rateLimits.search = 10 * 1000; // 10秒
this.rateLimits.apply = 5 * 1000; // 5秒
this.rateLimits.chat = 2 * 1000; // 2秒
}
}
/**
* 检查是否在工作时间
* @returns {boolean}
*/
isWorkingHours() {
const now = dayjs();
const hour = now.hour();
return hour >= this.workHours.start && hour < this.workHours.end;
}
/**
* 获取任务超时时间
* @param {string} taskType - 任务类型
* @returns {number} 超时时间(毫秒)
*/
getTaskTimeout(taskType) {
return this.taskTimeouts[taskType] || 30000; // 默认30秒
}
/**
* 获取任务优先级
* @param {string} taskType - 任务类型
* @param {object} options - 选项
* @returns {number} 优先级1-10
*/
getTaskPriority(taskType, options = {}) {
let priority = this.taskPriorities[taskType] || 5;
if (options.urgent) {
priority = Math.min(10, priority + 2);
}
return priority;
}
/**
* 获取操作频率限制
* @param {string} operation - 操作类型
* @returns {number} 间隔时间(毫秒)
*/
getRateLimit(operation) {
return this.rateLimits[operation] || 0;
}
/**
* 获取日限制
* @param {string} operation - 操作类型
* @returns {number} 日限制次数
*/
getDailyLimit(operation) {
return this.dailyLimits[`max${operation.charAt(0).toUpperCase() + operation.slice(1)}`] || Infinity;
}
}
// 导出单例
const scheduleConfig = new ScheduleConfig();
module.exports = scheduleConfig;