Skip to content

Commit

Permalink
chore: update master
Browse files Browse the repository at this point in the history
  • Loading branch information
Timeraa committed Apr 4, 2023
1 parent 084ee65 commit cb0fee9
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 56 deletions.
23 changes: 13 additions & 10 deletions apps/master/package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
{
"main": "index.js",
"name": "master",
"type": "module",
"license": "MPL-2.0",
"scripts": {
"start": "cd dist && node index",
"dev": "devScript",
"build": "tsc && devScript --copyOnly"
},
"dependencies": {
"@sentry/node": "^6.16.1",
"@sentry/tracing": "^6.16.1",
"debug": "^4.3.3",
"mongodb": "^4.3.0",
"redis": "^4.0.1",
"@sentry/node": "^7.46.0",
"@sentry/tracing": "^7.46.0",
"cron": "^2.3.0",
"debug": "^4.3.4",
"mongodb": "^5.1.0",
"redis": "^4.6.5",
"source-map-support": "^0.5.21"
},
"devDependencies": {
"@types/cron": "^2.0.1",
"@types/debug": "^4.1.7",
"dotenv": "^16.0.0",
"ts-devscript": "^3.0.6",
"typescript": "^4.5.4"
"dotenv": "^16.0.3",
"ts-devscript": "^3.0.7",
"typescript": "^5.0.3"
},
"packageManager": "pnpm@7.30.5"
"packageManager": "pnpm@8.1.0"
}
35 changes: 18 additions & 17 deletions apps/master/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import "source-map-support/register";
import "source-map-support/register.js";

import { CronJob } from "cron";

import * as Sentry from "@sentry/node";
import { Integrations } from "@sentry/tracing";
import debug from "debug";
import { MongoClient } from "mongodb";
import { createClient } from "redis";

import calculatePresenceUsage from "./util/calculatePresenceUsage";
import updateScience from "./util/updateScience";
import calculatePresenceUsage from "./util/calculatePresenceUsage.js";
import updateScience from "./util/updateScience.js";

if (process.env.NODE_ENV !== "production")
require("dotenv").config({ path: "../../.env" });
(await import("dotenv")).config({ path: "../../../.env" });

if (!process.env.MONGO_URL) throw new Error("MONGO_URL is not defined!");

Sentry.init({
dsn: process.env.SENTRY_DSN,
Expand All @@ -20,7 +24,7 @@ Sentry.init({
});

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

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

async function run() {
debug.enable("API-Master*");

await Promise.all([mongo.connect(), redis.connect()]);
debug.enable("API-Master*");

mainLog("Running");
mainLog("Connecting to MongoDB...");
await mongo.connect();
mainLog("Connecting to Redis...");
await redis.connect();

await Promise.all([updateScience(), calculatePresenceUsage()]);
mainLog("Connected!");

setInterval(() => {
updateScience();
calculatePresenceUsage();
}, 60 * 1000);
}
await Promise.all([updateScience(), calculatePresenceUsage()]);

run();
new CronJob("* * * * *", updateScience).start();
new CronJob("* * * * *", calculatePresenceUsage).start();
6 changes: 2 additions & 4 deletions apps/master/src/util/calculatePresenceUsage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mainLog, mongo, redis } from "..";
import { mainLog, mongo, redis } from "../index.js";

export default async function () {
const log = mainLog.extend("calculatePresenceUsage");
Expand All @@ -15,9 +15,7 @@ export default async function () {
{ $group: { _id: "$presences", count: { $sum: 1 } } }
])
.sort({ count: -1 })
.map(d => {
return { [d._id]: d.count };
})
.map(d => ({ [d._id]: d.count }))
.toArray())
);

Expand Down
5 changes: 2 additions & 3 deletions apps/master/src/util/updateScience.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mainLog, mongo, redis } from "..";
import { mainLog, mongo, redis } from "../index.js";

export default async function () {
const log = mainLog.extend("updateScience"),
Expand Down Expand Up @@ -42,8 +42,7 @@ export default async function () {
if (entries.length) {
const res = await mongo
.db("PreMiD")
//TODO Typings
.collection<{}>("science")
.collection("science")
.bulkWrite(
entries.map(e => ({
updateOne: {
Expand Down
2 changes: 1 addition & 1 deletion apps/master/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */

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

0 comments on commit cb0fee9

Please sign in to comment.