Files
outline/server/queues/tasks/CleanupDemotedUserTask.ts
T
Tom Moor f95761dddf perf: Use FOR NO KEY UPDATE where locked rows are only updated (#14559)
* perf: Use FOR NO KEY UPDATE where locked rows are only updated

Rows that other tables reference by foreign key are now locked with
FOR NO KEY UPDATE when the transaction only updates or soft-deletes
them, so concurrent inserts that reference the row are not blocked.

* test: Collaborative save does not wait for foreign key inserts
2026-11-12 18:54:37 -05:00

73 lines
2.0 KiB
TypeScript

import Logger from "@server/logging/Logger";
import { WebhookSubscription, ApiKey, User } from "@server/models";
import { cannot } from "@server/policies";
import { sequelize } from "@server/storage/database";
import { BaseTask, TaskPriority } from "./base/BaseTask";
type Props = {
userId: string;
};
/**
* Task to disable mechanisms for exporting data from a suspended or demoted user,
* currently this is done by destroying associated Api Keys and disabling webhooks.
*/
export default class CleanupDemotedUserTask extends BaseTask<Props> {
protected jobId({ userId }: Props) {
return `cleanup-demoted-user:${userId}`;
}
public async perform(props: Props) {
const user = await User.scope("withTeam").findByPk(props.userId);
if (!user) {
return;
}
await sequelize.transaction(async (transaction) => {
if (cannot(user, "createWebhookSubscription", user.team)) {
const subscriptions = await WebhookSubscription.findAll({
where: {
createdById: props.userId,
enabled: true,
},
transaction,
lock: transaction.LOCK.NO_KEY_UPDATE,
});
await Promise.all(
subscriptions.map((subscription) =>
subscription.disable({ transaction })
)
);
Logger.info(
"task",
`Disabled ${subscriptions.length} webhooks for user ${props.userId}`
);
}
if (cannot(user, "createApiKey", user.team)) {
const apiKeys = await ApiKey.findAll({
where: {
userId: props.userId,
},
transaction,
lock: transaction.LOCK.UPDATE,
});
await Promise.all(
apiKeys.map((apiKey) => apiKey.destroy({ transaction }))
);
Logger.info(
"task",
`Destroyed ${apiKeys.length} api keys for user ${props.userId}`
);
}
});
}
public get options() {
return {
priority: TaskPriority.Background,
};
}
}