This commit is contained in:
张成
2026-04-08 15:28:02 +08:00
parent bfd39eddcf
commit 048c40d802
5 changed files with 231 additions and 2 deletions

View File

@@ -421,6 +421,14 @@ class JobManager {
}
const list = Array.isArray(response.data) ? response.data : [];
console.log(`[工作管理] 获取 job_listings 成功,共 ${list.length} 个 tab`);
try {
const jobTypeAiSyncService = require('../../../services/job_type_ai_sync_service');
await jobTypeAiSyncService.maybeSyncAfterListings(sn_code, list, platform);
} catch (syncErr) {
console.warn('[工作管理] job_types AI 同步失败:', syncErr.message);
}
return list;
}

View File

@@ -26,7 +26,8 @@ class ScheduledJobs {
auto_search: false,
auto_deliver: false,
auto_chat: false,
auto_active: false
auto_active: false,
job_type_listings_ai: false
};
}
@@ -111,11 +112,23 @@ class ScheduledJobs {
this.jobs.push(autoActiveJob);
console.log('[定时任务] ✓ 已启动自动活跃任务 (每2小时)');
// 5. 每日拉取 get_job_listings 并用 AI 更新 job_typesdescription / excludeKeywords / commonSkills
const jobTypeListingsAiJob = node_schedule.scheduleJob(config.schedules.jobTypeListingsAi || '0 0 4 * * *', () => {
this.runDailyJobTypeListingsAiSync().catch((err) => {
console.error('[定时任务] 每日 job_types AI 同步失败:', err);
});
});
this.jobs.push(jobTypeListingsAiJob);
console.log('[定时任务] ✓ 已启动每日 job_types AI 同步 (每天 04:00)');
// 立即执行一次业务任务(可选)
setTimeout(() => {
console.log('[定时任务] 立即执行一次初始化任务...');
this.runAutoDeliverTask();
this.runAutoChatTask();
this.runDailyJobTypeListingsAiSync().catch((err) => {
console.error('[定时任务] 启动时 job_types AI 同步失败:', err);
});
}, 10000); // 延迟10秒,等待系统初始化完成和设备心跳
console.log('[定时任务] 所有定时任务启动完成!');
@@ -295,6 +308,80 @@ class ScheduledJobs {
}
}
/**
* 每日一次:对已绑定 job_type_id 且设备在线的账号下发 get_job_listings成功后在 jobManager 内触发 AI 更新 job_types
*/
async runDailyJobTypeListingsAiSync() {
const key = 'job_type_listings_ai';
if (this._runningFlags[key]) {
console.log('[job_type_listings_ai] 上一次执行尚未完成,本次跳过');
return;
}
this._runningFlags[key] = true;
try {
const Sequelize = require('sequelize');
const { Op } = Sequelize;
const scheduleManager = require('../index');
const jobApi = require('../../job/index');
const mqtt = scheduleManager.mqttClient;
if (!mqtt) {
console.warn('[job_type_listings_ai] MQTT 未初始化,跳过');
return;
}
const { pla_account } = db.models;
const accounts = await pla_account.findAll({
where: {
is_delete: 0,
is_enabled: 1,
job_type_id: { [Op.ne]: null }
},
attributes: ['id', 'sn_code', 'job_type_id', 'platform_type']
});
if (!accounts || accounts.length === 0) {
return;
}
const now = Date.now();
const offlineThreshold = 3 * 60 * 1000;
let ok = 0;
let skipped = 0;
for (const acc of accounts) {
const sn_code = acc.sn_code;
const device = deviceManager.devices.get(sn_code);
const lastHb = device && device.lastHeartbeat ? device.lastHeartbeat : 0;
const isOnline = device && device.isOnline && now - lastHb < offlineThreshold;
if (!isOnline) {
skipped++;
continue;
}
const platform =
acc.platform_type ||
(typeof acc.getDataValue === 'function' && acc.getDataValue('platform_type')) ||
'boss';
try {
await jobApi.get_job_listings(sn_code, mqtt, { platform });
ok++;
} catch (err) {
console.warn(`[job_type_listings_ai] 设备 ${sn_code} 失败:`, err.message);
skipped++;
}
}
if (ok > 0 || skipped > 0) {
console.log(`[job_type_listings_ai] 完成: 成功 ${ok},跳过/失败 ${skipped},共 ${accounts.length} 个账号`);
}
} catch (error) {
console.error('[job_type_listings_ai] 执行失败:', error);
} finally {
this._runningFlags[key] = false;
}
}
/**
* 获取启用指定功能的账号列表
* @param {string} featureType - 功能类型: auto_search, auto_deliver, auto_chat, auto_active

View File

@@ -51,7 +51,8 @@ class ScheduleConfig {
autoSearch: '0 0 */1 * * *', // 自动搜索任务每1小时执行一次
autoDeliver: '0 */2 * * * *', // 自动投递任务每2分钟执行一次
autoChat: '0 */1 * * * *', // 自动沟通任务每1分钟执行一次
autoActive: '0 0 */2 * * *' // 自动活跃任务每2小时执行一次
autoActive: '0 0 */2 * * *', // 自动活跃任务每2小时执行一次
jobTypeListingsAi: '0 0 4 * * *' // 每天 04:00 对有 job_type_id 的在线设备拉取 get_job_listings 并 AI 更新 job_types
};
}