mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 17:38:19 +08:00
core: handle sync delay in database cluster
This commit is contained in:
4 files changed
+16
-12
No files matched your search
@@ -925,14 +925,14 @@ export class ProblemSolutionHandler extends ProblemDetailHandler {
|
|||||||
|
|
||||||
@param('psid', Types.ObjectId)
|
@param('psid', Types.ObjectId)
|
||||||
async postUpvote(domainId: string, psid: ObjectId) {
|
async postUpvote(domainId: string, psid: ObjectId) {
|
||||||
const [psdoc, pssdoc] = await solution.vote(domainId, psid, this.user._id, 1);
|
const psdoc = await solution.vote(domainId, psid, this.user._id, 1);
|
||||||
this.back({ vote: psdoc.vote, user_vote: pssdoc.vote });
|
this.back({ vote: psdoc.vote, user_vote: 1 });
|
||||||
}
|
}
|
||||||
|
|
||||||
@param('psid', Types.ObjectId)
|
@param('psid', Types.ObjectId)
|
||||||
async postDownvote(domainId: string, psid: ObjectId) {
|
async postDownvote(domainId: string, psid: ObjectId) {
|
||||||
const [psdoc, pssdoc] = await solution.vote(domainId, psid, this.user._id, -1);
|
const psdoc = await solution.vote(domainId, psid, this.user._id, -1);
|
||||||
this.back({ vote: psdoc.vote, user_vote: pssdoc.vote });
|
this.back({ vote: psdoc.vote, user_vote: -1 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -299,7 +299,7 @@ export async function setStatus<K extends keyof DocStatusType>(
|
|||||||
): Promise<DocStatusType[K]> {
|
): Promise<DocStatusType[K]> {
|
||||||
return await collStatus.findOneAndUpdate(
|
return await collStatus.findOneAndUpdate(
|
||||||
{ domainId, docType, docId, uid },
|
{ domainId, docType, docId, uid },
|
||||||
{ $set: args, $inc: { counter: 1 } },
|
{ $set: args },
|
||||||
{
|
{
|
||||||
upsert: true,
|
upsert: true,
|
||||||
returnDocument,
|
returnDocument,
|
||||||
|
|||||||
@@ -78,12 +78,9 @@ class SolutionModel {
|
|||||||
);
|
);
|
||||||
let inc = value;
|
let inc = value;
|
||||||
if (before?.vote) inc -= before.vote;
|
if (before?.vote) inc -= before.vote;
|
||||||
return [
|
return inc
|
||||||
inc
|
? await document.inc(domainId, document.TYPE_PROBLEM_SOLUTION, psid, 'vote', inc)
|
||||||
? await document.inc(domainId, document.TYPE_PROBLEM_SOLUTION, psid, 'vote', inc)
|
: doc;
|
||||||
: doc,
|
|
||||||
await document.getStatus(domainId, document.TYPE_PROBLEM_SOLUTION, psid, uid),
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static async getListStatus(domainId: string, psids: ObjectId[], uid: number) {
|
static async getListStatus(domainId: string, psids: ObjectId[], uid: number) {
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import pwhash from '../lib/hash.hydro';
|
|||||||
import bus from '../service/bus';
|
import bus from '../service/bus';
|
||||||
import db from '../service/db';
|
import db from '../service/db';
|
||||||
import { Value } from '../typeutils';
|
import { Value } from '../typeutils';
|
||||||
import { ArgMethod, buildProjection, randomstring } from '../utils';
|
import { ArgMethod, buildProjection, randomstring, sleep } from '../utils';
|
||||||
import { PERM, PRIV } from './builtin';
|
import { PERM, PRIV } from './builtin';
|
||||||
import domain from './domain';
|
import domain from './domain';
|
||||||
import * as setting from './setting';
|
import * as setting from './setting';
|
||||||
@@ -346,6 +346,13 @@ class UserModel {
|
|||||||
{ $set: { join: true } },
|
{ $set: { join: true } },
|
||||||
{ upsert: true },
|
{ upsert: true },
|
||||||
);
|
);
|
||||||
|
// make sure user is immediately available after creation
|
||||||
|
// give some time for database to sync in replicas
|
||||||
|
for (let i = 1; i <= 10; i++) {
|
||||||
|
const udoc = await UserModel.getById('system', uid); // eslint-disable-line no-await-in-loop
|
||||||
|
if (udoc) break;
|
||||||
|
await sleep(500); // eslint-disable-line no-await-in-loop
|
||||||
|
}
|
||||||
return uid;
|
return uid;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e?.code === 11000) {
|
if (e?.code === 11000) {
|
||||||
|
|||||||
Reference in new issue
Block a user