1
This commit is contained in:
150
api/middleware/schedule/config.js
Normal file
150
api/middleware/schedule/config.js
Normal file
@@ -0,0 +1,150 @@
|
||||
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;
|
||||
Reference in New Issue
Block a user