diff --git a/apps/master/package.json b/apps/master/package.json index 11520938..bf77fb4f 100644 --- a/apps/master/package.json +++ b/apps/master/package.json @@ -1,5 +1,6 @@ { - "main": "index.js", + "name": "master", + "type": "module", "license": "MPL-2.0", "scripts": { "start": "cd dist && node index", @@ -7,18 +8,20 @@ "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" } diff --git a/apps/master/src/index.ts b/apps/master/src/index.ts index e1988a8f..fcb9cd24 100644 --- a/apps/master/src/index.ts +++ b/apps/master/src/index.ts @@ -1,4 +1,6 @@ -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"; @@ -6,11 +8,13 @@ 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, @@ -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" @@ -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(); diff --git a/apps/master/src/util/calculatePresenceUsage.ts b/apps/master/src/util/calculatePresenceUsage.ts index 002a00d4..99a4f543 100644 --- a/apps/master/src/util/calculatePresenceUsage.ts +++ b/apps/master/src/util/calculatePresenceUsage.ts @@ -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"); @@ -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()) ); diff --git a/apps/master/src/util/updateScience.ts b/apps/master/src/util/updateScience.ts index 1711a72a..e523181a 100644 --- a/apps/master/src/util/updateScience.ts +++ b/apps/master/src/util/updateScience.ts @@ -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"), @@ -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: { diff --git a/apps/master/tsconfig.json b/apps/master/tsconfig.json index f1cc5b7f..de60af3b 100644 --- a/apps/master/tsconfig.json +++ b/apps/master/tsconfig.json @@ -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. */ diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6ca31971..dcd67815 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -7,36 +7,42 @@ importers: apps/master: dependencies: '@sentry/node': - specifier: ^6.16.1 - version: 6.19.7 + specifier: ^7.46.0 + version: 7.46.0 '@sentry/tracing': - specifier: ^6.16.1 - version: 6.19.7 + specifier: ^7.46.0 + version: 7.46.0 + cron: + specifier: ^2.3.0 + version: 2.3.0 debug: - specifier: ^4.3.3 + specifier: ^4.3.4 version: 4.3.4 mongodb: - specifier: ^4.3.0 - version: 4.14.0 + specifier: ^5.1.0 + version: 5.1.0 redis: - specifier: ^4.0.1 + specifier: ^4.6.5 version: 4.6.5 source-map-support: specifier: ^0.5.21 version: 0.5.21 devDependencies: + '@types/cron': + specifier: ^2.0.1 + version: 2.0.1 '@types/debug': specifier: ^4.1.7 version: 4.1.7 dotenv: - specifier: ^16.0.0 + specifier: ^16.0.3 version: 16.0.3 ts-devscript: - specifier: ^3.0.6 + specifier: ^3.0.7 version: 3.0.7 typescript: - specifier: ^4.5.4 - version: 4.9.5 + specifier: ^5.0.3 + version: 5.0.3 apps/worker: dependencies: @@ -1315,6 +1321,16 @@ packages: '@redis/client': 1.5.6 dev: false + /@sentry-internal/tracing@7.46.0: + resolution: {integrity: sha512-KYoppa7PPL8Er7bdPoxTNUfIY804JL7hhOEomQHYD22rLynwQ4AaLm3YEY75QWwcGb0B7ZDMV+tSumW7Rxuwuw==} + engines: {node: '>=8'} + dependencies: + '@sentry/core': 7.46.0 + '@sentry/types': 7.46.0 + '@sentry/utils': 7.46.0 + tslib: 1.14.1 + dev: false + /@sentry/core@6.19.7: resolution: {integrity: sha512-tOfZ/umqB2AcHPGbIrsFLcvApdTm9ggpi/kQZFkej7kMphjT+SGBiQfYtjyg9jcRW+ilAR4JXC9BGKsdEQ+8Vw==} engines: {node: '>=6'} @@ -1326,6 +1342,15 @@ packages: tslib: 1.14.1 dev: false + /@sentry/core@7.46.0: + resolution: {integrity: sha512-BnNHGh/ZTztqQedFko7vb2u6yLs/kWesOQNivav32ZbsEpVCjcmG1gOJXh2YmGIvj3jXOC9a4xfIuh+lYFcA6A==} + engines: {node: '>=8'} + dependencies: + '@sentry/types': 7.46.0 + '@sentry/utils': 7.46.0 + tslib: 1.14.1 + dev: false + /@sentry/hub@6.19.7: resolution: {integrity: sha512-y3OtbYFAqKHCWezF0EGGr5lcyI2KbaXW2Ik7Xp8Mu9TxbSTuwTe4rTntwg8ngPjUQU3SUHzgjqVB8qjiGqFXCA==} engines: {node: '>=6'} @@ -1360,6 +1385,22 @@ packages: - supports-color dev: false + /@sentry/node@7.46.0: + resolution: {integrity: sha512-+GrgJMCye2WXGarRiU5IJHCK27xg7xbPc2XjGojBKbBoZfqxVAWbXEK4bnBQgRGP1pCmrU/M6ZhVgR3dP580xA==} + engines: {node: '>=8'} + dependencies: + '@sentry-internal/tracing': 7.46.0 + '@sentry/core': 7.46.0 + '@sentry/types': 7.46.0 + '@sentry/utils': 7.46.0 + cookie: 0.4.2 + https-proxy-agent: 5.0.1 + lru_map: 0.3.3 + tslib: 1.14.1 + transitivePeerDependencies: + - supports-color + dev: false + /@sentry/tracing@6.19.7: resolution: {integrity: sha512-ol4TupNnv9Zd+bZei7B6Ygnr9N3Gp1PUrNI761QSlHtPC25xXC5ssSD3GMhBgyQrcvpuRcCFHVNNM97tN5cZiA==} engines: {node: '>=6'} @@ -1371,11 +1412,23 @@ packages: tslib: 1.14.1 dev: false + /@sentry/tracing@7.46.0: + resolution: {integrity: sha512-7qBtzmu7CDHclSKp+ZRrxoDcMyrev6/rxD2rSVJgB3o8gd2XGcO5vx9vuUOoYF0xTfOMXscR6Ft6JXE49xovYg==} + engines: {node: '>=8'} + dependencies: + '@sentry-internal/tracing': 7.46.0 + dev: false + /@sentry/types@6.19.7: resolution: {integrity: sha512-jH84pDYE+hHIbVnab3Hr+ZXr1v8QABfhx39KknxqKWr2l0oEItzepV0URvbEhB446lk/S/59230dlUUIBGsXbg==} engines: {node: '>=6'} dev: false + /@sentry/types@7.46.0: + resolution: {integrity: sha512-2FMEMgt2h6u7AoELhNhu9L54GAh67KKfK2pJ1kEXJHmWxM9FSCkizjLs/t+49xtY7jEXr8qYq8bV967VfDPQ9g==} + engines: {node: '>=8'} + dev: false + /@sentry/utils@6.19.7: resolution: {integrity: sha512-z95ECmE3i9pbWoXQrD/7PgkBAzJYR+iXtPuTkpBjDKs86O3mT+PXOT3BAn79w2wkn7/i3vOGD2xVr1uiMl26dA==} engines: {node: '>=6'} @@ -1384,6 +1437,14 @@ packages: tslib: 1.14.1 dev: false + /@sentry/utils@7.46.0: + resolution: {integrity: sha512-elRezDAF84guMG0OVIIZEWm6wUpgbda4HGks98CFnPsrnMm3N1bdBI9XdlxYLtf+ir5KsGR5YlEIf/a0kRUwAQ==} + engines: {node: '>=8'} + dependencies: + '@sentry/types': 7.46.0 + tslib: 1.14.1 + dev: false + /@sindresorhus/is@0.14.0: resolution: {integrity: sha512-9NET910DNaIPngYnLLPeg+Ogzqsi9uM4mSboU5y6p8S5DzMTVEsJZrawi+BoDNUVBa2DhJqQYUFvMDfgU062LQ==} engines: {node: '>=6'} @@ -1402,6 +1463,13 @@ packages: '@types/readdir-glob': 1.1.1 dev: true + /@types/cron@2.0.1: + resolution: {integrity: sha512-WHa/1rtNtD2Q/H0+YTTZoty+/5rcE66iAFX2IY+JuUoOACsevYyFkSYu/2vdw+G5LrmO7Lxowrqm0av4k3qWNQ==} + dependencies: + '@types/luxon': 3.2.2 + '@types/node': 18.15.11 + dev: true + /@types/debug@4.1.7: resolution: {integrity: sha512-9AonUzyTjXXhEOa0DnqpzZi6VHlqKMswga9EXjpXnnqxwLtdvPPtlO8evrI5D9S6asFRCQ6v+wpiUKbw+vKqyg==} dependencies: @@ -1417,7 +1485,7 @@ packages: /@types/keyv@3.1.4: resolution: {integrity: sha512-BQ5aZNSCpj7D6K2ksrRCTmKRLEpnPvWDiLPfoGyhZ++8YtiK9d/3DBKPJgry359X/P1PfruyYwvnvwFjuEiEIg==} dependencies: - '@types/node': 17.0.45 + '@types/node': 18.15.11 dev: true /@types/lodash@4.14.192: @@ -1428,6 +1496,10 @@ packages: resolution: {integrity: sha512-MqTGEo5bj5t157U6fA/BiDynNkn0YknVdh48CMPkTSpFTVmvao5UQmm7uEF6xBEo7qIMAlY/JSleYaE6VOdpaA==} dev: false + /@types/luxon@3.2.2: + resolution: {integrity: sha512-CuF9hIlsxGpJO4EztrW23/q1L9ctQfb5JM9mnLCJhhA8z81K2b4LTVjQYySXWhFV5SMyUsPYH/IcvvXDCKwa2g==} + dev: true + /@types/ms@0.7.31: resolution: {integrity: sha512-iiUgKzV9AuaEkZqkOLDIvlQiL6ltuZd9tGcW3gwpnX8JbuiuhFlEGmmFXEXkN50Cvq7Os88IY2v0dkDqXYWVgA==} dev: true @@ -1438,6 +1510,10 @@ packages: /@types/node@17.0.45: resolution: {integrity: sha512-w+tIMs3rq2afQdsPJlODhoUEKzFP1ayaoyl1CcnwtIlsVe7K7bA1NGm4s3PraqTLlXnbIN84zuBlxBWo1u9BLw==} + dev: true + + /@types/node@18.15.11: + resolution: {integrity: sha512-E5Kwq2n4SbMzQOn6wnmBjuK9ouqlURrcZDVfbo9ftDDTFt3nk7ZKK4GMOzoYgnpQJKcxwQw+lGaBvvlMo0qN/Q==} /@types/readdir-glob@1.1.1: resolution: {integrity: sha512-ImM6TmoF8bgOwvehGviEj3tRdRBbQujr1N+0ypaln/GWjaerOB26jb93vsRHmdMtvVQZQebOlqt2HROark87mQ==} @@ -1448,7 +1524,7 @@ packages: /@types/responselike@1.0.0: resolution: {integrity: sha512-85Y2BjiufFzaMIlvJDvTTB8Fxl2xfLo4HgmHzVBz08w4wDePCTjYw66PdrolO0kzli3yam/YCgRufyo1DdQVTA==} dependencies: - '@types/node': 17.0.45 + '@types/node': 18.15.11 dev: true /@types/validator@13.7.14: @@ -1462,12 +1538,12 @@ packages: /@types/whatwg-url@8.2.2: resolution: {integrity: sha512-FtQu10RWgn3D9U4aazdwIE2yzphmTJREDqNdODHrbrZmmMqI0vMheC/6NE/J1Yveaj8H+ela+YwWTjq5PGmuhA==} dependencies: - '@types/node': 17.0.45 + '@types/node': 18.15.11 '@types/webidl-conversions': 7.0.0 dev: false - /@yarnpkg/parsers@3.0.0-rc.41: - resolution: {integrity: sha512-Qenu313thHJTriR1PqECfPhNVtem1DowFw8r9xRn09T35c4iF+3lO66nB5PUsMCW/mnHux7ZtAStQzxX1waXlw==} + /@yarnpkg/parsers@3.0.0-rc.42: + resolution: {integrity: sha512-eW9Mbegmb5bJjwawJM9ghjUjUqciNMhC6L7XrQPF/clXS5bbP66MstsgCT5hy9VlfUh/CfBT+0Wucf531dMjHA==} engines: {node: '>=14.15.0'} dependencies: js-yaml: 3.14.1 @@ -1846,6 +1922,11 @@ packages: buffer: 5.7.1 dev: false + /bson@5.1.0: + resolution: {integrity: sha512-FEecNHkhYRBe7X9KDkdG12xNuz5VHGeH6mCE0B5sBmYtiR/Ux/9vUH/v4NUoBCDr6NuEhvahjoLiiRogptVW0A==} + engines: {node: '>=14.20.1'} + dev: false + /buffer-crc32@0.2.13: resolution: {integrity: sha512-VO9Ht/+p3SN7SKWqcrgEzjGbRSJYTx+Q1pTQC0wrWqHx0vpJraQ6GtHx8tvcg1rlK1byhU5gccxgOgj7B0TDkQ==} dev: false @@ -2024,6 +2105,12 @@ packages: readable-stream: 3.6.2 dev: false + /cron@2.3.0: + resolution: {integrity: sha512-ZN5HP8zDY41sJolMsbc+GksRATcbvkPKF5wR/qc8FrV4NBVi9ORQa1HmYa5GydaysUB80X9XpRlRkooa5uEtTA==} + dependencies: + luxon: 3.3.0 + dev: false + /cssfilter@0.0.10: resolution: {integrity: sha512-FAaLDaplstoRsDR8XGYH51znUN0UY7nMc6Z9/fvE8EXGwvJE9hu7W2vHwx1+bd6gCYnln9nLbzxFTrcO9YQDZw==} dev: false @@ -2465,7 +2552,7 @@ packages: mute-stream: 0.0.8 ora: 5.4.1 run-async: 2.4.1 - rxjs: 7.6.0 + rxjs: 7.8.0 string-width: 4.2.3 strip-ansi: 6.0.1 through: 2.3.8 @@ -2725,6 +2812,11 @@ packages: resolution: {integrity: sha512-Pn9cox5CsMYngeDbmChANltQl+5pi6XmTrraMSzhPmMBbmgcxmqWry0U3PGapCU1yB4/LqCcom7qhHZiF/jGfQ==} dev: false + /luxon@3.3.0: + resolution: {integrity: sha512-An0UCfG/rSiqtAIiBPO0Y9/zAnHUZxAMiCpTd5h2smgsj7GGmcenvrvww2cqNA8/4A5ZrD1gJpHN2mIHZQF+Mg==} + engines: {node: '>=12'} + dev: false + /memory-pager@1.5.0: resolution: {integrity: sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==} dev: false @@ -2812,6 +2904,28 @@ packages: - aws-crt dev: false + /mongodb@5.1.0: + resolution: {integrity: sha512-qgKb7y+EI90y4weY3z5+lIgm8wmexbonz0GalHkSElQXVKtRuwqXuhXKccyvIjXCJVy9qPV82zsinY0W1FBnJw==} + engines: {node: '>=14.20.1'} + peerDependencies: + '@aws-sdk/credential-providers': ^3.201.0 + mongodb-client-encryption: ^2.3.0 + snappy: ^7.2.2 + peerDependenciesMeta: + '@aws-sdk/credential-providers': + optional: true + mongodb-client-encryption: + optional: true + snappy: + optional: true + dependencies: + bson: 5.1.0 + mongodb-connection-string-url: 2.6.0 + socks: 2.7.1 + optionalDependencies: + saslprep: 1.0.3 + dev: false + /ms@2.1.2: resolution: {integrity: sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==} @@ -3128,8 +3242,8 @@ packages: queue-microtask: 1.2.3 dev: true - /rxjs@7.6.0: - resolution: {integrity: sha512-DDa7d8TFNUalGC9VqXvQ1euWNN7sc63TrUCuM9J998+ViviahMIjKSOU7rfcgFOF+FCD71BhDRv4hrFz+ImDLQ==} + /rxjs@7.8.0: + resolution: {integrity: sha512-F2+gxDshqmIub1KdvZkaEfGDwLNpPvk9Fs6LD/MyQxNgMds/WH9OdDDXOmxUZpME+iSK3rQCctkL0DYyytUqMg==} dependencies: tslib: 2.5.0 dev: true @@ -3358,7 +3472,7 @@ packages: resolution: {integrity: sha512-jmoL19o9CDJuIIMkdeXPWcNKplxfQhpYzZ6tUOJgljbs+QreYeKiIfl6iaa2/xBQTqkOc8L3DU4jG4FPvYifIg==} hasBin: true dependencies: - '@yarnpkg/parsers': 3.0.0-rc.41 + '@yarnpkg/parsers': 3.0.0-rc.42 autopm: 1.3.1 chalk: 4.1.2 chokidar: 3.5.3 @@ -3396,6 +3510,12 @@ packages: hasBin: true dev: true + /typescript@5.0.3: + resolution: {integrity: sha512-xv8mOEDnigb/tN9PSMTwSEqAnUvkoXMQlicOb0IUVDBSQCgBSaAAROUZYy2IcUy5qU6XajK5jjjO7TMWqBTKZA==} + engines: {node: '>=12.20'} + hasBin: true + dev: true + /typical@4.0.0: resolution: {integrity: sha512-VAH4IvQ7BDFYglMd7BPRDfLgxZZX4O4TFcRDA6EN5X7erNJJq+McIEp8np9aVtxrCJ6qx4GTYVfOWNjcqwZgRw==} engines: {node: '>=8'}