Skip to content

Commit e904bb0

Browse files
committed
feat: Add p-limit library for concurrency control
1 parent 8cb111c commit e904bb0

File tree

3 files changed

+40
-19
lines changed

3 files changed

+40
-19
lines changed

apps/master/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"debug": "^4.3.4",
1515
"ioredis": "^5.3.2",
1616
"mongodb": "^5.1.0",
17+
"p-limit": "^6.1.0",
1718
"redis": "^4.6.12",
1819
"source-map-support": "^0.5.21"
1920
},
Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
import { mainLog, mongo, redis } from "../index.js";
2+
import pLimit from "p-limit";
23

3-
export default async function () {
4-
const log = mainLog.extend("calculatePresenceUsage");
4+
const limit = pLimit(1);
55

6-
log("Calculating...");
6+
export default function () {
7+
return limit(async () => {
8+
const log = mainLog.extend("calculatePresenceUsage");
79

8-
const res = Object.assign(
9-
{},
10-
...(await mongo
11-
.db("PreMiD")
12-
.collection("science")
13-
.aggregate([
14-
{ $unwind: "$presences" },
15-
{ $group: { _id: "$presences", count: { $sum: 1 } } }
16-
])
17-
.sort({ count: -1 })
18-
.map(d => ({ [d._id]: d.count }))
19-
.toArray())
20-
);
10+
log("Calculating...");
2111

22-
log("Saving...");
12+
const res = Object.assign(
13+
{},
14+
...(await mongo
15+
.db("PreMiD")
16+
.collection("science")
17+
.aggregate([
18+
{ $unwind: "$presences" },
19+
{ $group: { _id: "$presences", count: { $sum: 1 } } }
20+
])
21+
.sort({ count: -1 })
22+
.map(d => ({ [d._id]: d.count }))
23+
.toArray())
24+
);
2325

24-
await redis.set("pmd-api.presence-usage", JSON.stringify(res));
26+
log("Saving...");
2527

26-
log("Updated!");
28+
await redis.set("pmd-api.presence-usage", JSON.stringify(res));
29+
30+
log("Updated!");
31+
});
2732
}

pnpm-lock.yaml

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)