mirror of
https://github.com/outline/outline.git
synced 2026-11-21 05:24:03 +03:00
* 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
72 lines
1.9 KiB
TypeScript
72 lines
1.9 KiB
TypeScript
import { subDays } from "date-fns";
|
|
import { Op } from "sequelize";
|
|
import InviteReminderEmail from "@server/emails/templates/InviteReminderEmail";
|
|
import { User } from "@server/models";
|
|
import { UserFlag } from "@server/models/User";
|
|
import { sequelize } from "@server/storage/database";
|
|
import { TaskPriority } from "./base/BaseTask";
|
|
import { CronTask, TaskInterval } from "./base/CronTask";
|
|
|
|
export default class InviteReminderTask extends CronTask {
|
|
public async perform() {
|
|
const users = await User.scope("invited").findAll({
|
|
attributes: ["id"],
|
|
where: {
|
|
createdAt: {
|
|
[Op.lt]: subDays(new Date(), 2),
|
|
[Op.gt]: subDays(new Date(), 3),
|
|
},
|
|
},
|
|
});
|
|
const userIds = users.map((user) => user.id);
|
|
|
|
for (const userId of userIds) {
|
|
await sequelize.transaction(async (transaction) => {
|
|
const user = await User.scope("withTeam").findByPk(userId, {
|
|
lock: {
|
|
level: transaction.LOCK.NO_KEY_UPDATE,
|
|
of: User,
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
const invitedBy = user?.invitedById
|
|
? await User.findByPk(user?.invitedById, { transaction })
|
|
: undefined;
|
|
|
|
if (
|
|
user &&
|
|
invitedBy &&
|
|
user.getFlag(UserFlag.InviteReminderSent) === 0
|
|
) {
|
|
await new InviteReminderEmail({
|
|
to: user.email,
|
|
language: user.language,
|
|
name: user.name,
|
|
actorName: invitedBy.name,
|
|
actorEmail: invitedBy.email,
|
|
teamName: user.team.name,
|
|
teamUrl: user.team.url,
|
|
}).schedule();
|
|
|
|
user.incrementFlag(UserFlag.InviteReminderSent);
|
|
await user.save({ transaction });
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
public get cron() {
|
|
return {
|
|
interval: TaskInterval.Day,
|
|
};
|
|
}
|
|
|
|
public get options() {
|
|
return {
|
|
attempts: 1,
|
|
priority: TaskPriority.Background,
|
|
};
|
|
}
|
|
}
|