From 3ae5262797efe0ac607bbb712f727072bf744ce6 Mon Sep 17 00:00:00 2001 From: Menci Date: Wed, 20 Mar 2019 20:21:42 +0800 Subject: [PATCH] Workaround Sequelize's .save() possibly won't resolve bug --- models/common.js | 3 ++- utility.js | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/models/common.js b/models/common.js index 952e1a1..7e1342f 100644 --- a/models/common.js +++ b/models/common.js @@ -34,7 +34,8 @@ class Model { for (let key in obj) this.record.set(key, obj[key]); let isNew = this.record.isNewRecord; - await this.record.save(); + + await syzoj.utils.withTimeoutRetry(() => this.record.save()); if (!isNew) return; await this.reload(); diff --git a/utility.js b/utility.js index 54eec06..21936c2 100644 --- a/utility.js +++ b/utility.js @@ -294,5 +294,18 @@ module.exports = { async saveConfig() { let fs = require('fs-extra'); fs.writeFileAsync(syzoj.configDir, JSON.stringify(syzoj.config, null, 2)); + }, + withTimeoutRetry(func) { + let attemptCount = 0; + return new Promise((resolve, reject) => { + function attempt() { + if (attemptCount++) console.log(`syzoj.utils.withTimeout(): attemptCount = ${attemptCount}`); + Promise.method(func)().timeout(5000) + .then(resolve) + .catch(Promise.TimeoutError, attempt) + .catch(reject); + } + attempt(); + }); } };