Skip to content

Commit

Permalink
Add missing sequelize transactions rollbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Hatry1337 committed Jun 15, 2023
1 parent 28b2d3b commit 0929743
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 50 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "synergy3",
"version": "3.14.2",
"version": "3.14.3",
"description": "Synergy 3 - Powerful Discord BOT Framework.",
"license": "MIT",
"author": {
Expand Down
20 changes: 14 additions & 6 deletions src/GuildManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Discord from "discord.js";

import { Op } from "sequelize";
import { Op, Transaction } from "sequelize";
import { sequelize } from "./Database";

import { GlobalLogger } from "./GlobalLogger";
Expand Down Expand Up @@ -90,15 +90,23 @@ export default class GuildManager extends CachedManager<Guild> {
}

public override async destroy() {
let t = await sequelize().transaction();

for(let k of this.cacheStorage.keys()) {
await this.onCacheEntryDeleted(k, this.cacheStorage.get(k)!);
await this.onCacheEntryDeleted(k, this.cacheStorage.get(k)!, t);
}

try {
await t.commit();
} catch (e) {
GlobalLogger.root.error("GuildManager.destroy Error:", e, "\nTransaction:", t);
await t.rollback();
}

await super.destroy();
}

private async onCacheEntryDeleted(discordId: string, guild: Guild) {
let t = await sequelize().transaction();

private async onCacheEntryDeleted(discordId: string, guild: Guild, transaction?: Transaction) {
await StorageGuild.update({
id: guild.id,
group: guild.group,
Expand All @@ -113,7 +121,7 @@ export default class GuildManager extends CachedManager<Guild> {
where: {
id: guild.id
},
transaction: t
transaction
}).catch(err => GlobalLogger.root.error("GuildManager.syncCacheEntry Error Updating StorageGuild:", err));
}
}
85 changes: 43 additions & 42 deletions src/ModuleDataManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,66 +72,67 @@ export default class ModuleDataManager{
this.bot.events.once("Stop", () => { clearInterval(this.timer); });
}

public getContainer(uuid: string) {
return new Promise<ModuleDataContainer>(async (resolve, reject) => {
let container = dataContainers.get(uuid);
if(container){
return resolve(container);
}

StorageModuleDataContainer.findOrCreate({
where: {
uuid
},
defaults: {
uuid: uuid,
kvData: {}
} as StorageModuleDataContainer
}).then(async storage_container => {
container = new ModuleDataContainer(this.bot, uuid, storage_container[0].kvData);
dataContainers.set(uuid, container);
return resolve(container);
}).catch(reject);
});
public async getContainer(uuid: string) {
let container = dataContainers.get(uuid);
if(container){
return container;
}

let storageContainer = await StorageModuleDataContainer.findOrCreate({
where: {
uuid
},
defaults: {
uuid: uuid,
kvData: {}
} as StorageModuleDataContainer
})
container = new ModuleDataContainer(this.bot, uuid, storageContainer[0].kvData);
dataContainers.set(uuid, container);
return container;
}

/**
* Don't execute this function directly! It is for internal calls
*/
public _loadFromStorage() {
return new Promise<void>(async (resolve, reject) => {
StorageModuleDataContainer.findAll().then(async containers => {
for(let c of containers){
dataContainers.set(c.uuid, new ModuleDataContainer(this.bot, c.uuid, c.kvData));
}
return resolve();
}).catch(reject);
});
public async _loadFromStorage() {
let containers = await StorageModuleDataContainer.findAll();
for(let c of containers){
dataContainers.set(c.uuid, new ModuleDataContainer(this.bot, c.uuid, c.kvData));
}
}

/**
* Don't execute this function directly! It is for internal calls
*/
public _syncStorage(){
return new Promise<void>(async (resolve) => {
GlobalLogger.root.info("[ModuleDataManager] Saving data to storage...");
let t = await sequelize().transaction();
for(let c of dataContainers){
let mdata = moduleDatas.get(c[0]);
if(!mdata) continue;
public async _syncStorage(){
GlobalLogger.root.info("[ModuleDataManager] Saving data to storage...");

let t = await sequelize().transaction();

for(let c of dataContainers){
let mdata = moduleDatas.get(c[0]);
if(!mdata) continue;

try {
await StorageModuleDataContainer.update({
kvData: mdata
}, {
where: {
uuid: c[0]
},
transaction: t
}).catch(err => GlobalLogger.root.warn("ModuleDataManager.syncStorage Error Updating StorageModuleDataContainer:", err));

});
} catch (e) {
GlobalLogger.root.error(`ModuleDataManager._syncStorage Error updating container "${c[0]}":`, e);
}
await t.commit().catch(err => GlobalLogger.root.error("ModuleDataManager.syncStorage Error Committing:", err));
return resolve();
});
}

try {
await t.commit();
} catch (e) {
GlobalLogger.root.error("ModuleDataManager.syncStorage Error committing transaction:", e, "\nTransaction:", t);
await t.rollback();
}
}
}
5 changes: 4 additions & 1 deletion src/UserManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,10 @@ export default class UserManager extends CachedManager<User>{
await t.commit();
}
} catch(e) {
GlobalLogger.root.warn("UserManager.forceStorageUpdate Error:", e);
GlobalLogger.root.error("UserManager.forceStorageUpdate Error:", e, "\nTransaction:", t);
if (!transaction) {
await t.rollback();
}
}
}

Expand Down

0 comments on commit 0929743

Please sign in to comment.