1- import "source-map-support/register" ;
1+ import "source-map-support/register.js" ;
2+
3+ import { CronJob } from "cron" ;
24
35import * as Sentry from "@sentry/node" ;
46import { Integrations } from "@sentry/tracing" ;
57import debug from "debug" ;
68import { MongoClient } from "mongodb" ;
79import { 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
1214if ( 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
1519Sentry . init ( {
1620 dsn : process . env . SENTRY_DSN ,
@@ -20,7 +24,7 @@ Sentry.init({
2024} ) ;
2125
2226export 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
3034redis . 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 ( ) ;
0 commit comments