diff --git a/app.js b/app.js index 01c6dcf..a8eb5bd 100644 --- a/app.js +++ b/app.js @@ -22,6 +22,14 @@ global.syzoj = { console.log(obj); }, async run() { + // Check config + if (syzoj.config.session_secret === '@SESSION_SECRET@' + || syzoj.config.email_jwt_secret === '@EMAIL_JWT_SECRET@' + || syzoj.config.db.password === '@DATABASE_PASSWORD@') { + console.log('Please generate and fill the secrets in config!'); + process.exit(); + } + let Express = require('express'); global.app = Express(); diff --git a/models/common.js b/models/common.js index bdce62a..48e0c67 100644 --- a/models/common.js +++ b/models/common.js @@ -36,7 +36,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/modules/problem.js b/modules/problem.js index afa41dc..84df7e2 100644 --- a/modules/problem.js +++ b/modules/problem.js @@ -683,7 +683,9 @@ app.post('/problem/:id/submit', app.multer.fields([{ name: 'answer', maxCount: 1 code: formatted }); - await formattedCode.save(); + try { + await formattedCode.save(); + } catch (e) {} } } } 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(); + }); } };