mirror of
https://github.com/outline/outline.git
synced 2026-11-22 14:01:41 +03:00
chore: Cleanup working tables left in db if midrun abort (#11786)
This commit is contained in:
1 parent
85072dab92
commit
62cfd4e9bc
1 file changed
+38
@@ -72,6 +72,9 @@ export default class UpdateDocumentsPopularityScoreTask extends CronTask {
|
||||
this.workingTable = `${WORKING_TABLE_PREFIX}_${dateStr}_${uniqueId}`;
|
||||
|
||||
try {
|
||||
// Clean up any stale working tables left behind by previous crashed runs
|
||||
await this.cleanupStaleWorkingTables();
|
||||
|
||||
// Setup: Create working table and populate with active document IDs
|
||||
await this.setupWorkingTable(threshold, partition);
|
||||
|
||||
@@ -443,6 +446,41 @@ export default class UpdateDocumentsPopularityScoreTask extends CronTask {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops any stale working tables from previous dates that were left behind
|
||||
* by runs interrupted before cleanup could occur (e.g. worker killed mid-run).
|
||||
* Only removes tables from before the current date to avoid race conditions
|
||||
* with concurrent runs.
|
||||
*/
|
||||
private async cleanupStaleWorkingTables(): Promise<void> {
|
||||
try {
|
||||
const todayStr = new Date().toISOString().slice(0, 10).replace(/-/g, "");
|
||||
const tables = await sequelize.query<{ tablename: string }>(
|
||||
`SELECT tablename FROM pg_tables
|
||||
WHERE schemaname = 'public'
|
||||
AND tablename LIKE :prefix`,
|
||||
{
|
||||
replacements: {
|
||||
prefix: `${WORKING_TABLE_PREFIX}%`,
|
||||
},
|
||||
type: QueryTypes.SELECT,
|
||||
}
|
||||
);
|
||||
|
||||
const prefixLen = WORKING_TABLE_PREFIX.length + 1; // +1 for underscore
|
||||
|
||||
for (const { tablename } of tables) {
|
||||
const dateStr = tablename.slice(prefixLen, prefixLen + 8);
|
||||
if (dateStr < todayStr) {
|
||||
Logger.info("task", `Dropping stale working table: ${tablename}`);
|
||||
await sequelize.query(`DROP TABLE IF EXISTS "${tablename}" CASCADE`);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
Logger.warn("Failed to clean up stale working tables", { error });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the working table
|
||||
*/
|
||||
|
||||
Reference in new issue
Block a user