mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-06 15:52:22 +01:00
Merge gateway cleanup fix into sleep-12-decision-target-invalidation
Co-Authored-By: Paperclip <noreply@paperclip.ing> * codex/sleep-11-decision-effect-recovery: fix(server): drain gateway expiry during one-shot cleanup
This commit is contained in:
commit
eb8ecb49ef
4 files changed
+67
-19
No files matched your search
@@ -12,7 +12,7 @@ export function registerGatewayCredentialExpiryWork(
|
||||
retryMs: 30_000,
|
||||
notificationCoalesceMs: 30_000,
|
||||
canRun,
|
||||
run: () => gateway.cleanupExpiredSessions(),
|
||||
run: () => gateway.cleanupExpiredSessions({ singleBatch: true }),
|
||||
next: () => gateway.nextCredentialExpiryAt(),
|
||||
});
|
||||
}
|
||||
@@ -11479,28 +11479,36 @@ export function createToolGatewayService(
|
||||
return row?.at == null ? null : new Date(row.at).getTime();
|
||||
},
|
||||
|
||||
async cleanupExpiredSessions(input: { now?: Date } = {}) {
|
||||
async cleanupExpiredSessions(input: { now?: Date; singleBatch?: boolean } = {}) {
|
||||
const now = input.now ?? new Date();
|
||||
const expiredTokens = await db.select({ id: toolMcpGatewayTokens.id })
|
||||
.from(toolMcpGatewayTokens)
|
||||
.where(lte(toolMcpGatewayTokens.expiresAt, now))
|
||||
.orderBy(asc(toolMcpGatewayTokens.expiresAt), asc(toolMcpGatewayTokens.id))
|
||||
.limit(500);
|
||||
if (expiredTokens.length > 0) {
|
||||
await db.delete(toolMcpGatewayTokens)
|
||||
let deletedCount = 0;
|
||||
while (true) {
|
||||
const expiredTokens = await db.select({ id: toolMcpGatewayTokens.id })
|
||||
.from(toolMcpGatewayTokens)
|
||||
.where(lte(toolMcpGatewayTokens.expiresAt, now))
|
||||
.orderBy(asc(toolMcpGatewayTokens.expiresAt), asc(toolMcpGatewayTokens.id))
|
||||
.limit(500);
|
||||
if (expiredTokens.length > 0) {
|
||||
await db.delete(toolMcpGatewayTokens)
|
||||
.where(and(inArray(toolMcpGatewayTokens.id, expiredTokens.map((token) => token.id)), lte(toolMcpGatewayTokens.expiresAt, now)))
|
||||
.returning({ id: toolMcpGatewayTokens.id });
|
||||
}
|
||||
const expiredSessions = await db.select({ id: toolGatewaySessions.id })
|
||||
.from(toolGatewaySessions)
|
||||
.where(lte(toolGatewaySessions.expiresAt, now))
|
||||
.orderBy(asc(toolGatewaySessions.expiresAt), asc(toolGatewaySessions.id))
|
||||
.limit(500);
|
||||
if (expiredSessions.length > 0) {
|
||||
const rows = await db.delete(toolGatewaySessions)
|
||||
.where(and(inArray(toolGatewaySessions.id, expiredSessions.map((session) => session.id)), lte(toolGatewaySessions.expiresAt, now)))
|
||||
.returning({ id: toolGatewaySessions.id });
|
||||
deletedCount += rows.length;
|
||||
}
|
||||
// The worker schedules the next page; one-shot startup/manual callers
|
||||
// must finish the backlog without relying on another scheduled pass.
|
||||
if (input.singleBatch || (expiredTokens.length < 500 && expiredSessions.length < 500)) break;
|
||||
}
|
||||
const expiredSessions = await db.select({ id: toolGatewaySessions.id })
|
||||
.from(toolGatewaySessions)
|
||||
.where(lte(toolGatewaySessions.expiresAt, now))
|
||||
.orderBy(asc(toolGatewaySessions.expiresAt), asc(toolGatewaySessions.id))
|
||||
.limit(500);
|
||||
if (expiredSessions.length === 0) return { deletedCount: 0 };
|
||||
const rows = await db.delete(toolGatewaySessions)
|
||||
.where(and(inArray(toolGatewaySessions.id, expiredSessions.map((session) => session.id)), lte(toolGatewaySessions.expiresAt, now)))
|
||||
.returning({ id: toolGatewaySessions.id });
|
||||
return { deletedCount: rows.length };
|
||||
return { deletedCount };
|
||||
},
|
||||
|
||||
async listRuntimeSlots(companyId?: string) {
|
||||
|
||||
Reference in new issue
Block a user