diff --git a/server/src/database.mts b/server/src/database.mts index 00830b6d..447b6f13 100644 --- a/server/src/database.mts +++ b/server/src/database.mts @@ -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(CacheName.AirportInfo); export async function connectToDatabase() { const url = ENV.MONGO_DB_CONNECTION_STRING; @@ -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, @@ -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"); } diff --git a/server/src/jobs/getVatsimData.mts b/server/src/jobs/getVatsimData.mts index 588f3922..46555bc8 100644 --- a/server/src/jobs/getVatsimData.mts +++ b/server/src/jobs/getVatsimData.mts @@ -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(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. @@ -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"); @@ -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();