1
This commit is contained in:
184
api/middleware/schedule/tasks/autoActiveTask.js
Normal file
184
api/middleware/schedule/tasks/autoActiveTask.js
Normal file
@@ -0,0 +1,184 @@
|
||||
const BaseTask = require('./baseTask');
|
||||
const db = require('../../dbProxy');
|
||||
const config = require('../config');
|
||||
|
||||
/**
|
||||
* 自动活跃账号任务
|
||||
* 定期浏览职位、刷新简历、查看通知等,保持账号活跃度
|
||||
*/
|
||||
class AutoActiveTask extends BaseTask {
|
||||
constructor() {
|
||||
super('auto_active_account', {
|
||||
defaultInterval: 120, // 默认2小时
|
||||
defaultPriority: 5, // 较低优先级
|
||||
requiresLogin: true, // 需要登录
|
||||
conflictsWith: [ // 与这些任务冲突
|
||||
'auto_deliver', // 投递任务
|
||||
'auto_search' // 搜索任务
|
||||
]
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证任务参数
|
||||
*/
|
||||
validateParams(params) {
|
||||
if (!params.platform) {
|
||||
return {
|
||||
valid: false,
|
||||
reason: '缺少必要参数: platform'
|
||||
};
|
||||
}
|
||||
return { valid: true };
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取任务名称
|
||||
*/
|
||||
getTaskName(params) {
|
||||
return `自动活跃账号 - ${params.platform || 'boss'}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行自动活跃任务
|
||||
*/
|
||||
async execute(sn_code, params) {
|
||||
console.log(`[自动活跃] 设备 ${sn_code} 开始执行活跃任务`);
|
||||
|
||||
const actions = [];
|
||||
|
||||
// 1. 浏览推荐职位
|
||||
actions.push({
|
||||
action: 'browse_jobs',
|
||||
count: Math.floor(Math.random() * 5) + 3 // 3-7个职位
|
||||
});
|
||||
|
||||
// 2. 刷新简历
|
||||
actions.push({
|
||||
action: 'refresh_resume',
|
||||
success: true
|
||||
});
|
||||
|
||||
// 3. 查看通知
|
||||
actions.push({
|
||||
action: 'check_notifications',
|
||||
count: Math.floor(Math.random() * 3)
|
||||
});
|
||||
|
||||
// 4. 浏览公司主页
|
||||
actions.push({
|
||||
action: 'browse_companies',
|
||||
count: Math.floor(Math.random() * 3) + 1
|
||||
});
|
||||
|
||||
console.log(`[自动活跃] 设备 ${sn_code} 完成 ${actions.length} 个活跃操作`);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
actions: actions,
|
||||
message: `完成 ${actions.length} 个活跃操作`
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加活跃任务到队列
|
||||
*/
|
||||
async addToQueue(sn_code, taskQueue, customParams = {}) {
|
||||
const now = new Date();
|
||||
console.log(`[自动活跃] ${now.toLocaleString()} 尝试为设备 ${sn_code} 添加任务`);
|
||||
|
||||
try {
|
||||
// 1. 获取账号信息
|
||||
const { pla_account } = db.models;
|
||||
const account = await pla_account.findOne({
|
||||
where: {
|
||||
sn_code: sn_code,
|
||||
is_delete: 0,
|
||||
is_enabled: 1
|
||||
}
|
||||
});
|
||||
|
||||
if (!account) {
|
||||
console.log(`[自动活跃] 账号 ${sn_code} 不存在或未启用`);
|
||||
return { success: false, reason: '账号不存在或未启用' };
|
||||
}
|
||||
|
||||
const accountData = account.toJSON();
|
||||
|
||||
// 2. 检查是否开启了自动活跃
|
||||
if (!accountData.auto_active) {
|
||||
console.log(`[自动活跃] 设备 ${sn_code} 未开启自动活跃`);
|
||||
return { success: false, reason: '未开启自动活跃' };
|
||||
}
|
||||
|
||||
// 3. 获取活跃策略配置
|
||||
let activeStrategy = {};
|
||||
if (accountData.active_strategy) {
|
||||
activeStrategy = typeof accountData.active_strategy === 'string'
|
||||
? JSON.parse(accountData.active_strategy)
|
||||
: accountData.active_strategy;
|
||||
}
|
||||
|
||||
// 4. 检查时间范围
|
||||
if (activeStrategy.time_range) {
|
||||
const timeCheck = this.checkTimeRange(activeStrategy.time_range);
|
||||
if (!timeCheck.allowed) {
|
||||
console.log(`[自动活跃] 设备 ${sn_code} ${timeCheck.reason}`);
|
||||
return { success: false, reason: timeCheck.reason };
|
||||
}
|
||||
}
|
||||
|
||||
// 5. 执行所有层级的冲突检查
|
||||
const conflictCheck = await this.canExecuteTask(sn_code, taskQueue);
|
||||
if (!conflictCheck.allowed) {
|
||||
console.log(`[自动活跃] 设备 ${sn_code} 冲突检查未通过: ${conflictCheck.reason}`);
|
||||
return { success: false, reason: conflictCheck.reason };
|
||||
}
|
||||
|
||||
// 6. 检查活跃间隔
|
||||
const active_interval = activeStrategy.active_interval || this.config.defaultInterval;
|
||||
const intervalCheck = await this.checkExecutionInterval(sn_code, active_interval);
|
||||
|
||||
if (!intervalCheck.allowed) {
|
||||
console.log(`[自动活跃] 设备 ${sn_code} ${intervalCheck.reason}`);
|
||||
return { success: false, reason: intervalCheck.reason };
|
||||
}
|
||||
|
||||
// 7. 构建任务参数
|
||||
const taskParams = {
|
||||
platform: accountData.platform_type || 'boss',
|
||||
actions: activeStrategy.actions || ['browse_jobs', 'refresh_resume', 'check_notifications'],
|
||||
...customParams
|
||||
};
|
||||
|
||||
// 8. 验证参数
|
||||
const validation = this.validateParams(taskParams);
|
||||
if (!validation.valid) {
|
||||
return { success: false, reason: validation.reason };
|
||||
}
|
||||
|
||||
// 9. 添加任务到队列
|
||||
await taskQueue.addTask(sn_code, {
|
||||
taskType: this.taskType,
|
||||
taskName: this.getTaskName(taskParams),
|
||||
taskParams: taskParams,
|
||||
priority: this.config.defaultPriority
|
||||
});
|
||||
|
||||
console.log(`[自动活跃] 已为设备 ${sn_code} 添加活跃任务,间隔: ${active_interval} 分钟`);
|
||||
|
||||
// 10. 释放任务锁
|
||||
this.releaseTaskLock(sn_code);
|
||||
|
||||
return { success: true };
|
||||
|
||||
} catch (error) {
|
||||
console.error(`[自动活跃] 添加任务失败:`, error);
|
||||
this.releaseTaskLock(sn_code);
|
||||
return { success: false, reason: error.message };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 导出单例
|
||||
module.exports = new AutoActiveTask();
|
||||
Reference in New Issue
Block a user