Skip to content

[server] Log ticks of periodic db deleter #17089

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions components/gitpod-db/src/periodic-deleter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,50 @@ export class PeriodicDbDeleter {
@inject(TypeORM) protected readonly typeORM: TypeORM;

start() {
log.error("[PeriodicDbDeleter] Start ...");
log.info("[PeriodicDbDeleter] Start ...");
this.sync().catch((err) => log.error("[PeriodicDbDeleter] sync failed", err));
}

protected async sync() {
const doSync = async () => {
const tickID = new Date().toISOString();
log.info("[PeriodicDbDeleter] Starting to collect deleted rows.", {
periodicDeleterTickId: tickID,
});
const sortedTables = this.tableProvider.getSortedTables();
const toBeDeleted: { table: string; deletions: string[] }[] = [];
for (const table of sortedTables) {
toBeDeleted.push(await this.collectRowsToBeDeleted(table));
const rowsForTableToDelete = await this.collectRowsToBeDeleted(table);
log.info(
`[PeriodicDbDeleter] Identified ${rowsForTableToDelete.deletions.length} entries in ${rowsForTableToDelete.table} to be deleted.`,
{
periodicDeleterTickId: tickID,
},
);
toBeDeleted.push(rowsForTableToDelete);
}
// when collecting the deletions do so in the inverse order as during update (delete dependency targes first)
const pendingDeletions: Promise<void>[] = [];
for (const { deletions } of toBeDeleted.reverse()) {
for (const deletion of deletions) {
pendingDeletions.push(
this.query(deletion).catch((err) => log.error(`[PeriodicDbDeleter] sync error`, err)),
this.query(deletion).catch((err) =>
log.error(
`[PeriodicDbDeleter] sync error`,
{
periodicDeleterTickId: tickID,
query: deletion,
},
err,
),
),
);
}
}
await Promise.all(pendingDeletions);
log.info("[PeriodicDbDeleter] Finished deleting records.", {
periodicDeleterTickId: tickID,
});
};
repeat(doSync, 30000); // deletion is never time-critical, so we should ensure we do not spam ourselves
}
Expand Down