This commit is contained in:
undefined committed 2020-06-26 16:34:37 +08:00
1 parent 560e920624
commit d59710ddf1
13 files changed
+181 -49

No files matched your search

+1
View File
@@ -24,6 +24,7 @@ function add(domainId, parentType, parentId, owner, title, content, ip = null, h
highlight,
nReply: 0,
updateAt: new Date(),
views: 0,
},
);
}
+7 -5
View File
@@ -98,11 +98,12 @@ async function getList(domainId, rids, showHidden = false) {
return r;
}
function getUserInProblemMulti(domainId, uid, pid, getHidden = false) {
if (getHidden) return coll.find({ domainId, owner: uid, pid });
return coll.find({
domainId, owner: uid, pid, hidden: false,
});
function getUserInProblemMulti(domainId, uid, pid) {
return coll.find({ domainId, uid, pid });
}
function getByUid(domainId, uid) {
return coll.find({ domainId, uid }).toArray();
}
async function judge(domainId, rid) {
@@ -144,6 +145,7 @@ global.Hydro.model.record = module.exports = {
reset,
getList,
getUserInProblemMulti,
getByUid,
judge,
rejudge,
};
+19 -6
View File
@@ -4,8 +4,10 @@ const db = require('../service/db');
const coll = db.collection('task');
async function add(task) {
task.executeAfter = task.executeAfter || new Date();
const res = await coll.insertOne(task);
const t = { ...task };
if (typeof t.executeAfter === 'object') t.executeAfter = t.executeAfter.getTime();
t.executeAfter = t.executeAfter || new Date().getTime();
const res = await coll.insertOne(t);
return res.insertedId;
}
@@ -22,8 +24,9 @@ function del(_id) {
}
async function getFirst(query) {
query.executeAfter = query.executeAfter || { $lte: new Date() };
const res = await coll.find(query).sort('_id', 1).limit(1).toArray();
const q = { ...query };
q.executeAfter = q.executeAfter || { $lt: new Date().getTime() };
const res = await coll.find(q).sort('_id', 1).limit(1).toArray();
if (res.length) {
await coll.deleteOne({ _id: res[0]._id });
if (res[0].interval) {
@@ -37,9 +40,19 @@ async function getFirst(query) {
}
async function consume(query, cb) {
setInterval(async () => {
let isRunning = false;
const interval = setInterval(async () => {
if (isRunning) return;
isRunning = true;
const res = await getFirst(query);
if (res) cb(res);
if (res) {
try {
await cb(res);
} catch (e) {
clearInterval(interval);
}
}
isRunning = false;
}, 100);
}