1
This commit is contained in:
@@ -161,41 +161,63 @@ class ScheduledJobs {
|
|||||||
*/
|
*/
|
||||||
async cleanupOfflineDeviceTasks() {
|
async cleanupOfflineDeviceTasks() {
|
||||||
try {
|
try {
|
||||||
// 移除 device_status 依赖,离线任务清理功能暂时禁用
|
|
||||||
// 如果需要在 pla_account 表中添加在线状态字段,可以重新实现此功能
|
|
||||||
console.log('[清理离线任务] device_status 已移除,功能暂时禁用');
|
|
||||||
return;
|
|
||||||
|
|
||||||
/* 原有代码已注释
|
|
||||||
const Sequelize = require('sequelize');
|
|
||||||
const { device_status, task_status, op } = db.models;
|
|
||||||
|
|
||||||
// 离线阈值:10分钟
|
// 离线阈值:10分钟
|
||||||
const offlineThreshold = 10 * 60 * 1000; // 10分钟
|
const offlineThreshold = 10 * 60 * 1000; // 10分钟
|
||||||
const now = new Date();
|
const now = Date.now();
|
||||||
const thresholdTime = new Date(now.getTime() - offlineThreshold);
|
const thresholdTime = now - offlineThreshold;
|
||||||
|
|
||||||
// 查询离线超过10分钟的设备
|
// 获取所有启用的账号
|
||||||
const offlineDevices = await device_status.findAll({
|
const pla_account = db.getModel('pla_account');
|
||||||
|
const accounts = await pla_account.findAll({
|
||||||
where: {
|
where: {
|
||||||
isOnline: false,
|
is_delete: 0,
|
||||||
lastHeartbeatTime: {
|
is_enabled: 1
|
||||||
[op.lt]: thresholdTime
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
attributes: ['sn_code', 'lastHeartbeatTime']
|
attributes: ['sn_code']
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!offlineDevices || offlineDevices.length === 0) {
|
if (!accounts || accounts.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 通过 deviceManager 检查哪些设备离线超过10分钟
|
||||||
|
const offlineSnCodes = [];
|
||||||
|
const offlineDevicesInfo = [];
|
||||||
|
|
||||||
|
for (const account of accounts) {
|
||||||
|
const sn_code = account.sn_code;
|
||||||
|
const device = deviceManager.devices.get(sn_code);
|
||||||
|
|
||||||
|
if (!device) {
|
||||||
|
// 设备从未发送过心跳,视为离线
|
||||||
|
offlineSnCodes.push(sn_code);
|
||||||
|
offlineDevicesInfo.push({
|
||||||
|
sn_code: sn_code,
|
||||||
|
lastHeartbeatTime: null
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// 检查最后心跳时间
|
||||||
|
const lastHeartbeat = device.lastHeartbeat || 0;
|
||||||
|
if (lastHeartbeat < thresholdTime || !device.isOnline) {
|
||||||
|
offlineSnCodes.push(sn_code);
|
||||||
|
offlineDevicesInfo.push({
|
||||||
|
sn_code: sn_code,
|
||||||
|
lastHeartbeatTime: lastHeartbeat ? new Date(lastHeartbeat) : null
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (offlineSnCodes.length === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const offlineSnCodes = offlineDevices.map(dev => dev.sn_code);
|
|
||||||
console.log(`[清理离线任务] 发现 ${offlineSnCodes.length} 个离线超过10分钟的设备: ${offlineSnCodes.join(', ')}`);
|
console.log(`[清理离线任务] 发现 ${offlineSnCodes.length} 个离线超过10分钟的设备: ${offlineSnCodes.join(', ')}`);
|
||||||
|
|
||||||
let totalCancelled = 0;
|
let totalCancelled = 0;
|
||||||
|
|
||||||
// 为每个离线设备取消任务
|
// 为每个离线设备取消任务
|
||||||
|
const task_status = db.getModel('task_status');
|
||||||
for (const sn_code of offlineSnCodes) {
|
for (const sn_code of offlineSnCodes) {
|
||||||
try {
|
try {
|
||||||
// 查询该设备的所有pending/running任务
|
// 查询该设备的所有pending/running任务
|
||||||
@@ -211,6 +233,8 @@ class ScheduledJobs {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const deviceInfo = offlineDevicesInfo.find(d => d.sn_code === sn_code);
|
||||||
|
|
||||||
// 更新任务状态为cancelled
|
// 更新任务状态为cancelled
|
||||||
const updateResult = await task_status.update(
|
const updateResult = await task_status.update(
|
||||||
{
|
{
|
||||||
@@ -218,7 +242,7 @@ class ScheduledJobs {
|
|||||||
endTime: new Date(),
|
endTime: new Date(),
|
||||||
result: JSON.stringify({
|
result: JSON.stringify({
|
||||||
reason: '设备离线超过10分钟,任务已自动取消',
|
reason: '设备离线超过10分钟,任务已自动取消',
|
||||||
offlineTime: offlineDevices.find(d => d.sn_code === sn_code)?.lastHeartbeatTime
|
offlineTime: deviceInfo?.lastHeartbeatTime
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -246,7 +270,6 @@ class ScheduledJobs {
|
|||||||
if (totalCancelled > 0) {
|
if (totalCancelled > 0) {
|
||||||
console.log(`[清理离线任务] 共取消 ${totalCancelled} 个离线设备的任务`);
|
console.log(`[清理离线任务] 共取消 ${totalCancelled} 个离线设备的任务`);
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('[清理离线任务] 执行失败:', error);
|
console.error('[清理离线任务] 执行失败:', error);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user