This commit is contained in:
Baobhan Sith
2025-04-24 14:17:20 +08:00
parent 7c46132d30
commit 1ae14ea80c
5 changed files with 104 additions and 28 deletions
@@ -218,6 +218,26 @@ export const deleteConnection = async (id: number): Promise<boolean> => {
}
};
/**
* 更新指定连接的 last_connected_at 时间戳
* @param id 连接 ID
* @param timestamp Unix 时间戳 (秒)
*/
export const updateLastConnected = async (id: number, timestamp: number): Promise<boolean> => {
const sql = `UPDATE connections SET last_connected_at = ? WHERE id = ?`;
try {
const db = await getDbInstance();
const result = await runDb(db, sql, [timestamp, id]);
if (result.changes === 0) {
console.warn(`[Repository] updateLastConnected: No connection found with ID ${id} to update.`);
}
return result.changes > 0;
} catch (err: any) {
console.error(`Repository: 更新连接 ${id} 的 last_connected_at 时出错:`, err.message);
throw new Error('更新上次连接时间失败');
}
};
/**
* 更新连接的标签关联 (使用事务)
* @param connectionId 连接 ID
+13 -1
View File
@@ -119,9 +119,21 @@ export const establishSshConnection = (
keepaliveCountMax: 10,
};
const readyHandler = () => {
const readyHandler = async () => { // 改为 async 函数
console.log(`SshService: SSH 连接到 ${connDetails.host}:${connDetails.port} (ID: ${connDetails.id}) 成功。`);
sshClient.removeListener('error', errorHandler); // 成功后移除错误监听器
// --- 新增:更新 last_connected_at ---
try {
const currentTimeSeconds = Math.floor(Date.now() / 1000);
await ConnectionRepository.updateLastConnected(connDetails.id, currentTimeSeconds);
console.log(`SshService: 已更新连接 ${connDetails.id} 的 last_connected_at 为 ${currentTimeSeconds}`);
} catch (updateError) {
// 更新失败不应阻止连接成功,但需要记录错误
console.error(`SshService: 更新连接 ${connDetails.id} 的 last_connected_at 失败:`, updateError);
}
// --- 结束新增 ---
resolve(sshClient); // 返回 Client 实例
};