Skip to content

Commit cb0fee9

Browse files
committed
chore: update master
1 parent 084ee65 commit cb0fee9

File tree

6 files changed

+177
-56
lines changed

6 files changed

+177
-56
lines changed

Diff for: apps/master/package.json

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
{
2-
"main": "index.js",
2+
"name": "master",
3+
"type": "module",
34
"license": "MPL-2.0",
45
"scripts": {
56
"start": "cd dist && node index",
67
"dev": "devScript",
78
"build": "tsc && devScript --copyOnly"
89
},
910
"dependencies": {
10-
"@sentry/node": "^6.16.1",
11-
"@sentry/tracing": "^6.16.1",
12-
"debug": "^4.3.3",
13-
"mongodb": "^4.3.0",
14-
"redis": "^4.0.1",
11+
"@sentry/node": "^7.46.0",
12+
"@sentry/tracing": "^7.46.0",
13+
"cron": "^2.3.0",
14+
"debug": "^4.3.4",
15+
"mongodb": "^5.1.0",
16+
"redis": "^4.6.5",
1517
"source-map-support": "^0.5.21"
1618
},
1719
"devDependencies": {
20+
"@types/cron": "^2.0.1",
1821
"@types/debug": "^4.1.7",
19-
"dotenv": "^16.0.0",
20-
"ts-devscript": "^3.0.6",
21-
"typescript": "^4.5.4"
22+
"dotenv": "^16.0.3",
23+
"ts-devscript": "^3.0.7",
24+
"typescript": "^5.0.3"
2225
},
23-
"packageManager": "pnpm@7.30.5"
26+
"packageManager": "pnpm@8.1.0"
2427
}

Diff for: apps/master/src/index.ts

+18-17
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
import "source-map-support/register";
1+
import "source-map-support/register.js";
2+
3+
import { CronJob } from "cron";
24

35
import * as Sentry from "@sentry/node";
46
import { Integrations } from "@sentry/tracing";
57
import debug from "debug";
68
import { MongoClient } from "mongodb";
79
import { createClient } from "redis";
810

9-
import calculatePresenceUsage from "./util/calculatePresenceUsage";
10-
import updateScience from "./util/updateScience";
11+
import calculatePresenceUsage from "./util/calculatePresenceUsage.js";
12+
import updateScience from "./util/updateScience.js";
1113

1214
if (process.env.NODE_ENV !== "production")
13-
require("dotenv").config({ path: "../../.env" });
15+
(await import("dotenv")).config({ path: "../../../.env" });
16+
17+
if (!process.env.MONGO_URL) throw new Error("MONGO_URL is not defined!");
1418

1519
Sentry.init({
1620
dsn: process.env.SENTRY_DSN,
@@ -20,7 +24,7 @@ Sentry.init({
2024
});
2125

2226
export const redis = createClient({
23-
url: process.env.REDIS_URL || "localhost:6379"
27+
url: process.env.REDIS_URL || "redis://localhost:6379/"
2428
}),
2529
mongo = new MongoClient(process.env.MONGO_URL!, {
2630
appName: "PreMiD-API-Master"
@@ -29,19 +33,16 @@ export const redis = createClient({
2933

3034
redis.on("error", err => console.log(err.message));
3135

32-
async function run() {
33-
debug.enable("API-Master*");
34-
35-
await Promise.all([mongo.connect(), redis.connect()]);
36+
debug.enable("API-Master*");
3637

37-
mainLog("Running");
38+
mainLog("Connecting to MongoDB...");
39+
await mongo.connect();
40+
mainLog("Connecting to Redis...");
41+
await redis.connect();
3842

39-
await Promise.all([updateScience(), calculatePresenceUsage()]);
43+
mainLog("Connected!");
4044

41-
setInterval(() => {
42-
updateScience();
43-
calculatePresenceUsage();
44-
}, 60 * 1000);
45-
}
45+
await Promise.all([updateScience(), calculatePresenceUsage()]);
4646

47-
run();
47+
new CronJob("* * * * *", updateScience).start();
48+
new CronJob("* * * * *", calculatePresenceUsage).start();

Diff for: apps/master/src/util/calculatePresenceUsage.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { mainLog, mongo, redis } from "..";
1+
import { mainLog, mongo, redis } from "../index.js";
22

33
export default async function () {
44
const log = mainLog.extend("calculatePresenceUsage");
@@ -15,9 +15,7 @@ export default async function () {
1515
{ $group: { _id: "$presences", count: { $sum: 1 } } }
1616
])
1717
.sort({ count: -1 })
18-
.map(d => {
19-
return { [d._id]: d.count };
20-
})
18+
.map(d => ({ [d._id]: d.count }))
2119
.toArray())
2220
);
2321

Diff for: apps/master/src/util/updateScience.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { mainLog, mongo, redis } from "..";
1+
import { mainLog, mongo, redis } from "../index.js";
22

33
export default async function () {
44
const log = mainLog.extend("updateScience"),
@@ -42,8 +42,7 @@ export default async function () {
4242
if (entries.length) {
4343
const res = await mongo
4444
.db("PreMiD")
45-
//TODO Typings
46-
.collection<{}>("science")
45+
.collection("science")
4746
.bulkWrite(
4847
entries.map(e => ({
4948
updateOne: {

Diff for: apps/master/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
2525

2626
/* Modules */
27-
"module": "commonjs" /* Specify what module code is generated. */,
27+
"module": "NodeNext" /* Specify what module code is generated. */,
2828
"rootDir": "./src" /* Specify the root folder within your source files. */,
2929
// "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
3030
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */

0 commit comments

Comments
 (0)