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:
Tom Moor
2020-12-01 19:20:20 -08:00
committed by GitHub
parent 1851477290
commit fe62048890
2 changed files with 48 additions and 65 deletions

View File

@ -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 sequelize.transaction(async (transaction: Transaction) => {
await user.update(
{
suspendedById: actorId,
suspendedAt: new Date(),
},
{ transaction }
);
await user.update({
suspendedById: actorId,
suspendedAt: new Date(),
});
await GroupUser.destroy({ where: { userId: user.id }, transaction });
await GroupUser.destroy({ where: { userId: user.id } });
} catch (err) {
if (transaction) {
await transaction.rollback();
}
throw err;
}
await Event.create({
name: "users.suspend",
actorId,
userId: user.id,
teamId: user.teamId,
data: { name: user.name },
ip,
await Event.create(
{
name: "users.suspend",
actorId,
userId: user.id,
teamId: user.teamId,
data: { name: user.name },
ip,
},
{ transaction }
);
});
}