Fix memory usage by batching syncTasks

This commit is contained in:
Jamie Kyle 2024-12-04 14:03:29 -08:00 committed by GitHub
parent ab1e6f847d
commit 4027f4604f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 154 additions and 75 deletions

View file

@ -31,6 +31,7 @@ import {
viewSyncTaskSchema,
} from '../messageModifiers/ViewSyncs';
import { safeParseUnknown } from './schemas';
import { DataWriter } from '../sql/Client';
const syncTaskDataSchema = z.union([
deleteMessageSchema,
@ -85,7 +86,7 @@ export async function queueSyncTasks(
log.error(`${innerLogId}: Schema not found. Deleting.`);
// eslint-disable-next-line no-await-in-loop
await removeSyncTaskById(id);
return;
continue;
}
const parseResult = safeParseUnknown(syncTaskDataSchema, data);
if (!parseResult.success) {
@ -94,7 +95,7 @@ export async function queueSyncTasks(
);
// eslint-disable-next-line no-await-in-loop
await removeSyncTaskById(id);
return;
continue;
}
const { data: parsed } = parseResult;
@ -223,4 +224,37 @@ export async function queueSyncTasks(
await removeSyncTaskById(id);
}
}
// Note: There may still be some tasks in the database, but we expect to be
// called again some time later to process them.
}
async function processSyncTasksBatch(
logId: string,
previousRowId: number | null
): Promise<number | null> {
log.info('syncTasks: Fetching tasks');
const result = await DataWriter.dequeueOldestSyncTasks(previousRowId);
const syncTasks = result.tasks;
if (syncTasks.length === 0) {
log.info(`${logId}/syncTasks: No sync tasks to process, stopping`);
} else {
log.info(`${logId}/syncTasks: Queueing ${syncTasks.length} sync tasks`);
await queueSyncTasks(syncTasks, DataWriter.removeSyncTaskById);
}
return result.lastRowId;
}
const A_TICK = Promise.resolve();
export async function runAllSyncTasks(): Promise<void> {
let lastRowId: number | null = null;
do {
// eslint-disable-next-line no-await-in-loop
lastRowId = await processSyncTasksBatch('Startup', lastRowId);
// eslint-disable-next-line no-await-in-loop
await A_TICK;
} while (lastRowId != null);
}