problem pretest

This commit is contained in:
undefined committed 2020-06-28 13:05:40 +08:00
1 parent dd5a212422
commit 554698189c
17 files changed
+127 -57

No files matched your search

+1 -10
View File
@@ -1,4 +1,3 @@
const cluster = require('cluster');
const moment = require('moment-timezone');
const db = require('../service/db');
@@ -8,7 +7,6 @@ async function add(task) {
const t = { ...task };
if (typeof t.executeAfter === 'object') t.executeAfter = t.executeAfter.getTime();
t.count = t.count || 1;
t.wait = t.wait || Object.keys(cluster.workers);
t.executeAfter = t.executeAfter || new Date().getTime();
const res = await coll.insertOne(t);
return res.insertedId;
@@ -29,16 +27,9 @@ function del(_id) {
async function getFirst(query) {
const q = { ...query };
q.executeAfter = q.executeAfter || { $lt: new Date().getTime() };
q.wait = { $elemMatch: { $eq: cluster.worker.id } };
const res = await coll.find(q).sort('_id', 1).limit(1).toArray();
if (res.length) {
if (res[0].count === 1) await coll.deleteOne({ _id: res[0]._id });
else {
await coll.updateOne(
{ _id: res[0]._id },
{ $inc: { count: -1 }, $pull: { wait: cluster.worker.id } },
);
}
await coll.deleteOne({ _id: res[0]._id });
if (res[0].interval) {
await coll.insertOne({
...res[0], executeAfter: moment().add(...res[0].interval).toDate(),