wip: emit machine updates on heartbeat for real-time status

- Machine-alive handler now properly emits update events to all user connections
- Ensures mobile clients receive real-time machine status updates
- Fixed null handling when machine doesn't exist in database

Note: Last test was not able to spawn new session - webhook callback timing issue

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Kirill Dubovitskiy 2025-08-16 07:12:11 -07:00
parent 3a804f24c8
commit 6b1a3c3e82

View File

@ -1339,7 +1339,7 @@ export async function startApi(): Promise<{ app: FastifyInstance; io: Server }>
const machineId = data.machineId; const machineId = data.machineId;
// Update machine lastActiveAt in database // Update machine lastActiveAt in database
await db.machine.update({ const machine = await db.machine.update({
where: { where: {
accountId_id: { accountId_id: {
accountId: userId, accountId: userId,
@ -1352,7 +1352,33 @@ export async function startApi(): Promise<{ app: FastifyInstance; io: Server }>
} }
}).catch(() => { }).catch(() => {
// Machine might not exist yet, that's ok // Machine might not exist yet, that's ok
return null;
}); });
// If machine was updated, emit update to all user connections
if (machine) {
const updSeq = await allocateUserSeq(userId);
emitUpdateToInterestedClients({
event: 'update',
userId,
payload: {
id: randomKeyNaked(12),
seq: updSeq,
body: {
t: 'update-machine',
id: machine.id,
metadata: machine.metadata ? {
version: machine.metadataVersion,
value: machine.metadata
} : undefined,
active: true,
lastActiveAt: t
},
createdAt: Date.now()
},
recipientFilter: { type: 'all-user-authenticated-connections' }
});
}
} catch (error) { } catch (error) {
log({ module: 'websocket', level: 'error' }, `Error in machine-alive: ${error}`); log({ module: 'websocket', level: 'error' }, `Error in machine-alive: ${error}`);
} }