happy-server/sources/app/timeout.ts
2025-07-26 02:00:25 -07:00

37 lines
1.2 KiB
TypeScript

import { pubsub } from "@/services/pubsub";
import { db } from "@/storage/db";
import { delay } from "@/utils/delay";
import { forever } from "@/utils/forever";
import { shutdownSignal } from "@/utils/shutdown";
export function startTimeout() {
forever('session-timeout', async () => {
while (true) {
// Find timed out sessions
const sessions = await db.session.findMany({
where: {
active: true,
lastActiveAt: {
lte: new Date(Date.now() - 1000 * 60 * 10) // 10 minutes
}
}
});
for (const session of sessions) {
await db.session.update({
where: { id: session.id },
data: { active: false }
});
pubsub.emit('update-ephemeral', session.accountId, {
type: 'activity',
id: session.id,
active: false,
activeAt: session.lastActiveAt.getTime(),
thinking: false
});
}
// Wait for 1 minute
await delay(1000 * 60, shutdownSignal);
}
});
}