diff --git a/server/src/database.mts b/server/src/database.mts index 447b6f13..e9804624 100644 --- a/server/src/database.mts +++ b/server/src/database.mts @@ -7,11 +7,8 @@ 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; @@ -41,7 +38,6 @@ export async function connectToDatabase() { await connect .then(async (db) => { logger.debug("Connected"); - await cache.loadFromFile(ENV.CACHE_DIRECTORY); applySpeedGooseCacheLayer(mongoose, { sharedCacheStrategy: SharedCacheStrategies.IN_MEMORY, defaultTtl: 60 * 10, @@ -58,7 +54,5 @@ 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 46555bc8..87eada7a 100644 --- a/server/src/jobs/getVatsimData.mts +++ b/server/src/jobs/getVatsimData.mts @@ -6,9 +6,13 @@ import mainLogger, { flush } from "../logger.mjs"; import { VatsimEndpointModel } from "../models/VatsimEndpoint.mjs"; import { getVatsimData } from "../services/vatsim.mjs"; import postMessage from "../utils/postMessage.mjs"; +import { CacheManager, CacheName } from "../cacheManager.mjs"; +import { AirportInfoDocument } from "../models/AirportInfo.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. const dispose = DotLocker.lockSync("airports", { @@ -34,14 +38,16 @@ if (!dispose) { } catch (error) { logger.error(`Unable to retrieve VATSIM data: ${error}`); } finally { + await disconnectFromDatabase(); + await cache.saveToFile(ENV.CACHE_DIRECTORY); + cache.printStatistics(); + await flush(); dispose(); } - await disconnectFromDatabase(); postMessage("sendUpdates"); } -await flush(); if (!postMessage("done")) { process.exit(0); } diff --git a/server/src/main.mts b/server/src/main.mts index b31a2870..4e842401 100644 --- a/server/src/main.mts +++ b/server/src/main.mts @@ -4,9 +4,13 @@ import * as db from "./database.mjs"; import { ENV } from "./env.mjs"; import mainLogger from "./logger.mjs"; import * as WebServer from "./server.mjs"; +import { CacheManager, CacheName } from "./cacheManager.mjs"; +import { AirportInfoDocument } from "./models/AirportInfo.mjs"; const logger = mainLogger.child({ service: "main" }); +const cache = CacheManager.getInstance(CacheName.AirportInfo); + // If startup fails restart is reattempted 5 times every 30 seconds. const restartAttemptWaitTime = 30 * 1000; const maxRestartAttempts = 5; @@ -27,6 +31,7 @@ async function startup() { }); } await db.connectToDatabase(); + await cache.loadFromFile(ENV.CACHE_DIRECTORY); await WebServer.startServer(ENV.PORT); // At this point startup succeeded so reset the restart count. This is in case @@ -65,6 +70,8 @@ async function shutdown() { } async function handleDeath() { + await cache.saveToFile(ENV.CACHE_DIRECTORY); + cache.printStatistics(); await shutdown(); process.exit(); }