fix: One source of transaction deadlock when invites > pg pool (#1696)
* fix: One source of transaction deadlock when invites > pg pool * lint
This commit is contained in:
@ -2,7 +2,6 @@
|
||||
import { uniqBy } from "lodash";
|
||||
import mailer from "../mailer";
|
||||
import { User, Event, Team } from "../models";
|
||||
import { sequelize } from "../sequelize";
|
||||
|
||||
type Invite = { name: string, email: string };
|
||||
|
||||
@ -47,22 +46,15 @@ export default async function userInviter({
|
||||
let users = [];
|
||||
|
||||
// send and record remaining invites
|
||||
await Promise.all(
|
||||
filteredInvites.map(async (invite) => {
|
||||
const transaction = await sequelize.transaction();
|
||||
try {
|
||||
const newUser = await User.create(
|
||||
{
|
||||
for (const invite of filteredInvites) {
|
||||
const newUser = await User.create({
|
||||
teamId: user.teamId,
|
||||
name: invite.name,
|
||||
email: invite.email,
|
||||
service: null,
|
||||
},
|
||||
{ transaction }
|
||||
);
|
||||
});
|
||||
users.push(newUser);
|
||||
await Event.create(
|
||||
{
|
||||
await Event.create({
|
||||
name: "users.invite",
|
||||
actorId: user.id,
|
||||
teamId: user.teamId,
|
||||
@ -71,9 +63,7 @@ export default async function userInviter({
|
||||
name: invite.name,
|
||||
},
|
||||
ip,
|
||||
},
|
||||
{ transaction }
|
||||
);
|
||||
});
|
||||
await mailer.invite({
|
||||
to: invite.email,
|
||||
name: invite.name,
|
||||
@ -82,13 +72,7 @@ export default async function userInviter({
|
||||
teamName: team.name,
|
||||
teamUrl: team.url,
|
||||
});
|
||||
await transaction.commit();
|
||||
} catch (err) {
|
||||
await transaction.rollback();
|
||||
throw err;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
return { sent: filteredInvites, users };
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
// @flow
|
||||
import { Transaction } from "sequelize";
|
||||
import { ValidationError } from "../errors";
|
||||
import { User, Event, GroupUser } from "../models";
|
||||
import { sequelize } from "../sequelize";
|
||||
@ -16,29 +17,27 @@ export default async function userSuspender({
|
||||
throw new ValidationError("Unable to suspend the current user");
|
||||
}
|
||||
|
||||
let transaction;
|
||||
try {
|
||||
transaction = await sequelize.transaction();
|
||||
|
||||
await user.update({
|
||||
await sequelize.transaction(async (transaction: Transaction) => {
|
||||
await user.update(
|
||||
{
|
||||
suspendedById: actorId,
|
||||
suspendedAt: new Date(),
|
||||
});
|
||||
},
|
||||
{ transaction }
|
||||
);
|
||||
|
||||
await GroupUser.destroy({ where: { userId: user.id } });
|
||||
} catch (err) {
|
||||
if (transaction) {
|
||||
await transaction.rollback();
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
await GroupUser.destroy({ where: { userId: user.id }, transaction });
|
||||
|
||||
await Event.create({
|
||||
await Event.create(
|
||||
{
|
||||
name: "users.suspend",
|
||||
actorId,
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
data: { name: user.name },
|
||||
ip,
|
||||
},
|
||||
{ transaction }
|
||||
);
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user