mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 17:36:08 +08:00
多worker共享事件(不稳定
This commit is contained in:
1 parent
d59710ddf1
commit
dd5a212422
9 files changed
+113
-20
No files matched your search
+11
-1
@@ -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(),
|
||||
|
||||
Reference in new issue
Block a user