Skip to content

Commit

Permalink
Move cachemanager in to database class
Browse files Browse the repository at this point in the history
  • Loading branch information
neilenns committed Feb 13, 2024
1 parent e91ad70 commit b77424f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
8 changes: 7 additions & 1 deletion server/src/database.mts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import "./models/AirportInfo.mjs";
import "./models/Departure.mjs";
import "./models/FlightAwareRoute.mjs";
import "./models/FlightPlan.mjs";
import { CacheManager, CacheName } from "./cacheManager.mjs";
import { AirportInfoDocument } from "./models/AirportInfo.mjs";

const logger = mainLogger.child({ service: "database" });
const cache = CacheManager.getInstance<AirportInfoDocument>(CacheName.AirportInfo);

export async function connectToDatabase() {
const url = ENV.MONGO_DB_CONNECTION_STRING;
Expand Down Expand Up @@ -36,8 +39,9 @@ export async function connectToDatabase() {
});

await connect
.then((db) => {
.then(async (db) => {
logger.debug("Connected");
await cache.loadFromFile(ENV.CACHE_DIRECTORY);
applySpeedGooseCacheLayer(mongoose, {
sharedCacheStrategy: SharedCacheStrategies.IN_MEMORY,
defaultTtl: 60 * 10,
Expand All @@ -54,5 +58,7 @@ export async function connectToDatabase() {
export async function disconnectFromDatabase() {
logger.debug("Disconnecting...");
await mongoose.disconnect();
await cache.saveToFile(ENV.CACHE_DIRECTORY);
cache.printStatistics();
logger.debug("Disconnected");
}
6 changes: 0 additions & 6 deletions server/src/jobs/getVatsimData.mts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import DotLocker from "dotlocker";
import process from "node:process";
import { CacheManager, CacheName } from "../cacheManager.mjs";
import { connectToDatabase, disconnectFromDatabase } from "../database.mjs";
import { ENV } from "../env.mjs";
import mainLogger, { flush } from "../logger.mjs";
import { AirportInfoDocument } from "../models/AirportInfo.mjs";
import { VatsimEndpointModel } from "../models/VatsimEndpoint.mjs";
import { getVatsimData } from "../services/vatsim.mjs";
import postMessage from "../utils/postMessage.mjs";

const logger = mainLogger.child({ service: "getVatsimData" });
const cache = CacheManager.getInstance<AirportInfoDocument>(CacheName.AirportInfo);

// Using lockSync since this is the only thing running in this process
// and node was incorrectly exiting with code 13 when using the async method.
Expand All @@ -24,7 +21,6 @@ if (!dispose) {
logger.warn(`Airport updates in progress, skipping VATSIM data update`);
} else {
await connectToDatabase();
await cache.loadFromFile(ENV.CACHE_DIRECTORY);

try {
const dataEndpoint = await VatsimEndpointModel.findEndpoint("v3");
Expand All @@ -39,8 +35,6 @@ if (!dispose) {
logger.error(`Unable to retrieve VATSIM data: ${error}`);
} finally {
dispose();
cache.printStatistics();
await cache.saveToFile(ENV.CACHE_DIRECTORY);
}

await disconnectFromDatabase();
Expand Down

0 comments on commit b77424f

Please sign in to comment.