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} cfg 任务类型配置
|
||||
* - videoTable: 录像表名
|
||||
@ -93,72 +95,57 @@ class HikvisionService extends BaseService {
|
||||
}
|
||||
|
||||
const items = [];
|
||||
// 事务:原子化检查+插入,防止并发重复
|
||||
const conn = await app.mysql.get('wms').beginTransaction();
|
||||
try {
|
||||
for (const cameraId of cameraIds) {
|
||||
// 按设备ID匹配摄像头
|
||||
let matchedCamera = cfg.cameras.find(c => c.id === cameraId);
|
||||
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]
|
||||
for (const cameraId of cameraIds) {
|
||||
// 按设备ID匹配摄像头
|
||||
let matchedCamera = cfg.cameras.find(c => c.id === cameraId);
|
||||
if (!matchedCamera && cfg.workbenchMatch && task.workbench_id) {
|
||||
matchedCamera = cfg.cameras.find(
|
||||
c => c.custom_name && c.custom_name.includes(`${task.workbench_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();
|
||||
} catch (txError) {
|
||||
await conn.rollback();
|
||||
throw txError;
|
||||
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;
|
||||
|
||||
// 原子插入防重:仅当该任务+摄像头没有未删除录像行时插入
|
||||
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;
|
||||
}
|
||||
@ -469,78 +456,75 @@ class HikvisionService extends BaseService {
|
||||
return { status: 'fail', message: '该仓库无录制电脑在线' };
|
||||
}
|
||||
|
||||
// 4. 事务:先删除全部旧行,再逐摄像头插入并推送
|
||||
// 4. 重置录像行(无显式事务):先删旧行,再逐摄像头插入 created 行
|
||||
const cameraIds = typeof task[cfg.cameraIdsField] === 'string'
|
||||
? JSON.parse(task[cfg.cameraIdsField]) : (task[cfg.cameraIdsField] || []);
|
||||
|
||||
const conn = await app.mysql.get('wms').beginTransaction();
|
||||
try {
|
||||
await conn.delete(cfg.videoTable, { [cfg.fkColumn]: task.id });
|
||||
await app.mysql.get('wms').delete(cfg.videoTable, { [cfg.fkColumn]: task.id });
|
||||
|
||||
for (const cameraId of cameraIds) {
|
||||
let matchedCamera = cameras.find(c => c.id === cameraId);
|
||||
if (!matchedCamera && cfg.workbenchMatch && 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;
|
||||
const matchedServer = servers.find(s => s.id === matchedCamera.server_id) || servers[0];
|
||||
if (!matchedServer) continue;
|
||||
const pushedItems = [];
|
||||
for (const cameraId of cameraIds) {
|
||||
let matchedCamera = cameras.find(c => c.id === cameraId);
|
||||
if (!matchedCamera && cfg.workbenchMatch && 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;
|
||||
const matchedServer = servers.find(s => s.id === matchedCamera.server_id) || servers[0];
|
||||
if (!matchedServer) continue;
|
||||
|
||||
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,
|
||||
});
|
||||
await app.mysql.get('wms').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,
|
||||
});
|
||||
pushedItems.push({ cameraId, matchedCamera, matchedServer });
|
||||
}
|
||||
console.log(`[Hikvision] 强制录制-重置录像行: ${cfg.fkColumn}=${task.id}`);
|
||||
|
||||
// 5. 推送录制任务
|
||||
const payload = {
|
||||
...cfg.buildFields(task, cameraId),
|
||||
uniqueCode: `${cfg.codePrefix}${task.id}_${cameraId}`,
|
||||
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,
|
||||
},
|
||||
server: {
|
||||
id: matchedServer.id,
|
||||
brand: matchedServer.brand,
|
||||
internal_ip: matchedServer.internal_ip,
|
||||
internal_port: matchedServer.internal_port || 80,
|
||||
username: matchedServer.username,
|
||||
password: matchedServer.password,
|
||||
},
|
||||
};
|
||||
// 5. 推送录制任务(推送失败则删除刚插入的行,恢复原状)
|
||||
for (const { cameraId, matchedCamera, matchedServer } of pushedItems) {
|
||||
const payload = {
|
||||
...cfg.buildFields(task, cameraId),
|
||||
uniqueCode: `${cfg.codePrefix}${task.id}_${cameraId}`,
|
||||
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,
|
||||
},
|
||||
server: {
|
||||
id: matchedServer.id,
|
||||
brand: matchedServer.brand,
|
||||
internal_ip: matchedServer.internal_ip,
|
||||
internal_port: matchedServer.internal_port || 80,
|
||||
username: matchedServer.username,
|
||||
password: matchedServer.password,
|
||||
},
|
||||
};
|
||||
|
||||
const sendRes = await app.ws.sendTo(recordingUniqueId, JSON.stringify({
|
||||
method: 'hikvision.receiveTask',
|
||||
data: payload,
|
||||
}));
|
||||
const sendRes = await app.ws.sendTo(recordingUniqueId, JSON.stringify({
|
||||
method: 'hikvision.receiveTask',
|
||||
data: payload,
|
||||
}));
|
||||
|
||||
if (sendRes !== 1) {
|
||||
await conn.rollback();
|
||||
return { status: 'fail', message: '推送失败,录制电脑可能已断开' };
|
||||
}
|
||||
|
||||
console.log(`[Hikvision] 强制录制-已推送: ${cfg.fkColumn}=${task.id}, camera=${cameraId}`);
|
||||
if (sendRes !== 1) {
|
||||
// 推送失败:删除刚插入的行,避免任务卡在 created 状态
|
||||
await app.mysql.get('wms').delete(cfg.videoTable, { [cfg.fkColumn]: task.id });
|
||||
return { status: 'fail', message: '推送失败,录制电脑可能已断开' };
|
||||
}
|
||||
|
||||
await conn.commit();
|
||||
console.log(`[Hikvision] 强制录制-重置录像行: ${cfg.fkColumn}=${task.id}`);
|
||||
} catch (txError) {
|
||||
await conn.rollback();
|
||||
throw txError;
|
||||
console.log(`[Hikvision] 强制录制-已推送: ${cfg.fkColumn}=${task.id}, camera=${cameraId}`);
|
||||
}
|
||||
|
||||
return { status: 'success', message: '强制录制任务已推送', [cfg.fkColumn]: task.id };
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user