多worker共享事件(不稳定

This commit is contained in:
undefined committed 2020-06-26 18:30:43 +08:00
1 parent d59710ddf1
commit dd5a212422
9 files changed
+113 -20

No files matched your search

+11 -1
View File
@@ -1,3 +1,4 @@
const cluster = require('cluster');
const moment = require('moment-timezone');
const db = require('../service/db');
@@ -6,6 +7,8 @@ const coll = db.collection('task');
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;
@@ -26,9 +29,16 @@ 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) {
await coll.deleteOne({ _id: res[0]._id });
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 } },
);
}
if (res[0].interval) {
await coll.insertOne({
...res[0], executeAfter: moment().add(...res[0].interval).toDate(),
+5 -2
View File
@@ -109,8 +109,11 @@ function setPassword(uid, password) {
);
}
function setById(uid, args) {
return coll.findOneAndUpdate({ _id: uid }, { $set: args });
function setById(uid, $set, $unset) {
const op = {};
if ($set && Object.keys($set).length) op.$set = $set;
if ($unset && Object.keys($unset).length) op.$unset = $unset;
return coll.findOneAndUpdate({ _id: uid }, op);
}
function setEmail(uid, mail) {