fix: 移除 beginTransaction 依赖,修复事务 FOR UPDATE 挂起导致任务无法拉取
This commit is contained in:
parent
6b050a6865
commit
9626ba78e0
@ -68,7 +68,9 @@ class HikvisionService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 组装录像任务落参,并在事务内创建录像占位记录(防止并发重复)
|
* 组装录像任务落参,并用原子 INSERT 创建录像占位记录(防止并发重复)
|
||||||
|
* 注:不使用显式事务(egg-mysql 5 + mysql2 3.23 的事务 conn.query 存在挂起 bug),
|
||||||
|
* 改用 INSERT ... SELECT ... WHERE NOT EXISTS 原子防重。
|
||||||
* @param {Object} task 任务行(wms_pack_task 或 wms_return_unpack)
|
* @param {Object} task 任务行(wms_pack_task 或 wms_return_unpack)
|
||||||
* @param {Object} cfg 任务类型配置
|
* @param {Object} cfg 任务类型配置
|
||||||
* - videoTable: 录像表名
|
* - videoTable: 录像表名
|
||||||
@ -93,72 +95,57 @@ class HikvisionService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const items = [];
|
const items = [];
|
||||||
// 事务:原子化检查+插入,防止并发重复
|
for (const cameraId of cameraIds) {
|
||||||
const conn = await app.mysql.get('wms').beginTransaction();
|
// 按设备ID匹配摄像头
|
||||||
try {
|
let matchedCamera = cfg.cameras.find(c => c.id === cameraId);
|
||||||
for (const cameraId of cameraIds) {
|
if (!matchedCamera && cfg.workbenchMatch && task.workbench_id) {
|
||||||
// 按设备ID匹配摄像头
|
matchedCamera = cfg.cameras.find(
|
||||||
let matchedCamera = cfg.cameras.find(c => c.id === cameraId);
|
c => c.custom_name && c.custom_name.includes(`${task.workbench_id}号`)
|
||||||
if (!matchedCamera && cfg.workbenchMatch && task.workbench_id) {
|
|
||||||
matchedCamera = cfg.cameras.find(
|
|
||||||
c => c.custom_name && c.custom_name.includes(`${task.workbench_id}号`)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (!matchedCamera) matchedCamera = cfg.cameras[0];
|
|
||||||
if (!matchedCamera) continue;
|
|
||||||
|
|
||||||
const matchedServer = cfg.servers.find(s => s.id === matchedCamera.server_id) || cfg.servers[0];
|
|
||||||
if (!matchedServer) continue;
|
|
||||||
|
|
||||||
// 事务内 SELECT ... FOR UPDATE 锁定检查,防止并发重复
|
|
||||||
const [existing] = await conn.query(
|
|
||||||
`SELECT id FROM ${cfg.videoTable}
|
|
||||||
WHERE ${cfg.fkColumn} = ? AND camera_id = ? AND deleted_at IS NULL
|
|
||||||
FOR UPDATE`,
|
|
||||||
[task.id, matchedCamera.id]
|
|
||||||
);
|
);
|
||||||
if (!existing || existing.length === 0) {
|
|
||||||
await conn.insert(cfg.videoTable, {
|
|
||||||
[cfg.fkColumn]: task.id,
|
|
||||||
camera_id: matchedCamera.id,
|
|
||||||
video_url: '',
|
|
||||||
status: 'created',
|
|
||||||
created_at: app.mysql.literals.now,
|
|
||||||
updated_at: app.mysql.literals.now,
|
|
||||||
});
|
|
||||||
console.log(`[Hikvision] 创建录像记录: ${cfg.videoTable}=${task.id}, camera=${matchedCamera.id}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
items.push({
|
|
||||||
...cfg.buildFields(task, cameraId),
|
|
||||||
uniqueCode: `${cfg.codePrefix}${task.id}_${cameraId}`,
|
|
||||||
task_status: task.task_status,
|
|
||||||
start_time: this._toISO(task.start_time, -5000),
|
|
||||||
end_time: this._toISO(task.end_time),
|
|
||||||
operator: task.operator_text,
|
|
||||||
camera: {
|
|
||||||
id: matchedCamera.id,
|
|
||||||
channel_id: matchedCamera.channel_id,
|
|
||||||
channel_name: matchedCamera.channel_name,
|
|
||||||
ip: matchedCamera.ip,
|
|
||||||
port: matchedCamera.port,
|
|
||||||
model: matchedCamera.model,
|
|
||||||
custom_name: matchedCamera.custom_name,
|
|
||||||
},
|
|
||||||
server: {
|
|
||||||
id: matchedServer.id,
|
|
||||||
brand: matchedServer.brand,
|
|
||||||
internal_ip: matchedServer.internal_ip,
|
|
||||||
internal_port: matchedServer.internal_port || 80,
|
|
||||||
username: matchedServer.username,
|
|
||||||
password: matchedServer.password,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
await conn.commit();
|
if (!matchedCamera) matchedCamera = cfg.cameras[0];
|
||||||
} catch (txError) {
|
if (!matchedCamera) continue;
|
||||||
await conn.rollback();
|
|
||||||
throw txError;
|
const matchedServer = cfg.servers.find(s => s.id === matchedCamera.server_id) || cfg.servers[0];
|
||||||
|
if (!matchedServer) continue;
|
||||||
|
|
||||||
|
// 原子插入防重:仅当该任务+摄像头没有未删除录像行时插入
|
||||||
|
await app.mysql.get('wms').query(
|
||||||
|
`INSERT INTO ${cfg.videoTable} (${cfg.fkColumn}, camera_id, video_url, status, created_at, updated_at)
|
||||||
|
SELECT ?, ?, '', 'created', NOW(), NOW()
|
||||||
|
FROM DUAL
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1 FROM ${cfg.videoTable}
|
||||||
|
WHERE ${cfg.fkColumn} = ? AND camera_id = ? AND deleted_at IS NULL
|
||||||
|
)`,
|
||||||
|
[task.id, matchedCamera.id, task.id, matchedCamera.id]
|
||||||
|
);
|
||||||
|
|
||||||
|
items.push({
|
||||||
|
...cfg.buildFields(task, cameraId),
|
||||||
|
uniqueCode: `${cfg.codePrefix}${task.id}_${cameraId}`,
|
||||||
|
task_status: task.task_status,
|
||||||
|
start_time: this._toISO(task.start_time, -5000),
|
||||||
|
end_time: this._toISO(task.end_time),
|
||||||
|
operator: task.operator_text,
|
||||||
|
camera: {
|
||||||
|
id: matchedCamera.id,
|
||||||
|
channel_id: matchedCamera.channel_id,
|
||||||
|
channel_name: matchedCamera.channel_name,
|
||||||
|
ip: matchedCamera.ip,
|
||||||
|
port: matchedCamera.port,
|
||||||
|
model: matchedCamera.model,
|
||||||
|
custom_name: matchedCamera.custom_name,
|
||||||
|
},
|
||||||
|
server: {
|
||||||
|
id: matchedServer.id,
|
||||||
|
brand: matchedServer.brand,
|
||||||
|
internal_ip: matchedServer.internal_ip,
|
||||||
|
internal_port: matchedServer.internal_port || 80,
|
||||||
|
username: matchedServer.username,
|
||||||
|
password: matchedServer.password,
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
return items;
|
return items;
|
||||||
}
|
}
|
||||||
@ -469,78 +456,75 @@ class HikvisionService extends BaseService {
|
|||||||
return { status: 'fail', message: '该仓库无录制电脑在线' };
|
return { status: 'fail', message: '该仓库无录制电脑在线' };
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. 事务:先删除全部旧行,再逐摄像头插入并推送
|
// 4. 重置录像行(无显式事务):先删旧行,再逐摄像头插入 created 行
|
||||||
const cameraIds = typeof task[cfg.cameraIdsField] === 'string'
|
const cameraIds = typeof task[cfg.cameraIdsField] === 'string'
|
||||||
? JSON.parse(task[cfg.cameraIdsField]) : (task[cfg.cameraIdsField] || []);
|
? JSON.parse(task[cfg.cameraIdsField]) : (task[cfg.cameraIdsField] || []);
|
||||||
|
|
||||||
const conn = await app.mysql.get('wms').beginTransaction();
|
await app.mysql.get('wms').delete(cfg.videoTable, { [cfg.fkColumn]: task.id });
|
||||||
try {
|
|
||||||
await conn.delete(cfg.videoTable, { [cfg.fkColumn]: task.id });
|
|
||||||
|
|
||||||
for (const cameraId of cameraIds) {
|
const pushedItems = [];
|
||||||
let matchedCamera = cameras.find(c => c.id === cameraId);
|
for (const cameraId of cameraIds) {
|
||||||
if (!matchedCamera && cfg.workbenchMatch && task.workbench_id) {
|
let matchedCamera = cameras.find(c => c.id === cameraId);
|
||||||
matchedCamera = cameras.find(
|
if (!matchedCamera && cfg.workbenchMatch && task.workbench_id) {
|
||||||
c => c.custom_name && c.custom_name.includes(`${task.workbench_id}号`)
|
matchedCamera = cameras.find(
|
||||||
);
|
c => c.custom_name && c.custom_name.includes(`${task.workbench_id}号`)
|
||||||
}
|
);
|
||||||
if (!matchedCamera) matchedCamera = cameras[0];
|
}
|
||||||
if (!matchedCamera) continue;
|
if (!matchedCamera) matchedCamera = cameras[0];
|
||||||
const matchedServer = servers.find(s => s.id === matchedCamera.server_id) || servers[0];
|
if (!matchedCamera) continue;
|
||||||
if (!matchedServer) continue;
|
const matchedServer = servers.find(s => s.id === matchedCamera.server_id) || servers[0];
|
||||||
|
if (!matchedServer) continue;
|
||||||
|
|
||||||
await conn.insert(cfg.videoTable, {
|
await app.mysql.get('wms').insert(cfg.videoTable, {
|
||||||
[cfg.fkColumn]: task.id,
|
[cfg.fkColumn]: task.id,
|
||||||
camera_id: matchedCamera.id,
|
camera_id: matchedCamera.id,
|
||||||
video_url: '',
|
video_url: '',
|
||||||
status: 'created',
|
status: 'created',
|
||||||
created_at: app.mysql.literals.now,
|
created_at: app.mysql.literals.now,
|
||||||
updated_at: app.mysql.literals.now,
|
updated_at: app.mysql.literals.now,
|
||||||
});
|
});
|
||||||
|
pushedItems.push({ cameraId, matchedCamera, matchedServer });
|
||||||
|
}
|
||||||
|
console.log(`[Hikvision] 强制录制-重置录像行: ${cfg.fkColumn}=${task.id}`);
|
||||||
|
|
||||||
// 5. 推送录制任务
|
// 5. 推送录制任务(推送失败则删除刚插入的行,恢复原状)
|
||||||
const payload = {
|
for (const { cameraId, matchedCamera, matchedServer } of pushedItems) {
|
||||||
...cfg.buildFields(task, cameraId),
|
const payload = {
|
||||||
uniqueCode: `${cfg.codePrefix}${task.id}_${cameraId}`,
|
...cfg.buildFields(task, cameraId),
|
||||||
start_time: this._toISO(task.start_time, -5000),
|
uniqueCode: `${cfg.codePrefix}${task.id}_${cameraId}`,
|
||||||
end_time: this._toISO(task.end_time),
|
start_time: this._toISO(task.start_time, -5000),
|
||||||
operator: task.operator_text,
|
end_time: this._toISO(task.end_time),
|
||||||
camera: {
|
operator: task.operator_text,
|
||||||
id: matchedCamera.id,
|
camera: {
|
||||||
channel_id: matchedCamera.channel_id,
|
id: matchedCamera.id,
|
||||||
channel_name: matchedCamera.channel_name,
|
channel_id: matchedCamera.channel_id,
|
||||||
ip: matchedCamera.ip,
|
channel_name: matchedCamera.channel_name,
|
||||||
port: matchedCamera.port,
|
ip: matchedCamera.ip,
|
||||||
model: matchedCamera.model,
|
port: matchedCamera.port,
|
||||||
},
|
model: matchedCamera.model,
|
||||||
server: {
|
},
|
||||||
id: matchedServer.id,
|
server: {
|
||||||
brand: matchedServer.brand,
|
id: matchedServer.id,
|
||||||
internal_ip: matchedServer.internal_ip,
|
brand: matchedServer.brand,
|
||||||
internal_port: matchedServer.internal_port || 80,
|
internal_ip: matchedServer.internal_ip,
|
||||||
username: matchedServer.username,
|
internal_port: matchedServer.internal_port || 80,
|
||||||
password: matchedServer.password,
|
username: matchedServer.username,
|
||||||
},
|
password: matchedServer.password,
|
||||||
};
|
},
|
||||||
|
};
|
||||||
|
|
||||||
const sendRes = await app.ws.sendTo(recordingUniqueId, JSON.stringify({
|
const sendRes = await app.ws.sendTo(recordingUniqueId, JSON.stringify({
|
||||||
method: 'hikvision.receiveTask',
|
method: 'hikvision.receiveTask',
|
||||||
data: payload,
|
data: payload,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
if (sendRes !== 1) {
|
if (sendRes !== 1) {
|
||||||
await conn.rollback();
|
// 推送失败:删除刚插入的行,避免任务卡在 created 状态
|
||||||
return { status: 'fail', message: '推送失败,录制电脑可能已断开' };
|
await app.mysql.get('wms').delete(cfg.videoTable, { [cfg.fkColumn]: task.id });
|
||||||
}
|
return { status: 'fail', message: '推送失败,录制电脑可能已断开' };
|
||||||
|
|
||||||
console.log(`[Hikvision] 强制录制-已推送: ${cfg.fkColumn}=${task.id}, camera=${cameraId}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await conn.commit();
|
console.log(`[Hikvision] 强制录制-已推送: ${cfg.fkColumn}=${task.id}, camera=${cameraId}`);
|
||||||
console.log(`[Hikvision] 强制录制-重置录像行: ${cfg.fkColumn}=${task.id}`);
|
|
||||||
} catch (txError) {
|
|
||||||
await conn.rollback();
|
|
||||||
throw txError;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return { status: 'success', message: '强制录制任务已推送', [cfg.fkColumn]: task.id };
|
return { status: 'success', message: '强制录制任务已推送', [cfg.fkColumn]: task.id };
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user