setting: retry write on duplicate_key_error

This commit is contained in:
masnn committed 2021-04-10 04:36:41 +00:00
1 parent ea38940aee
commit b4ac4da1fd
2 files changed
+24 -2

No files matched your search

+2 -2
View File
@@ -2,6 +2,7 @@
import moment from 'moment-timezone';
import { Dictionary } from 'lodash';
import yaml from 'js-yaml';
import { retry } from '@hydrooj/utils/lib/utils';
import * as builtin from './builtin';
import { Setting as _Setting } from '../interface';
import { Logger } from '../logger';
@@ -193,8 +194,7 @@ bus.once('app/started', async () => {
if (setting.value) {
const current = await global.Hydro.service.db.collection('system').findOne({ _id: setting.key });
if (!current || current.value == null || current.value === '') {
// @ts-ignore
await system.set(setting.key, setting.value);
await retry(system.set, setting.key, setting.value);
}
}
}
+22
View File
@@ -296,3 +296,25 @@ export function formatSeconds(_seconds = '0') {
_digit2(seconds % 60),
);
}
export async function retry(func: Function, ...args: any[]): Promise<any>;
export async function retry(times: number, func: Function, ...args: any[]): Promise<any>;
// eslint-disable-next-line consistent-return
export async function retry(arg0: number | Function, func: any, ...args: any[]) {
let res;
if (typeof arg0 !== 'number') {
args = [func, ...args];
func = arg0;
arg0 = 3;
}
for (let i = 1; i <= arg0; i++) {
try {
// eslint-disable-next-line no-await-in-loop
res = await func(...args);
} catch (e) {
if (i === arg0) throw e;
continue;
}
return res;
}
}