Skip to content

Commit

Permalink
Move where cachemanager loads again
Browse files Browse the repository at this point in the history
  • Loading branch information
neilenns committed Feb 13, 2024
1 parent b77424f commit 55b490f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
6 changes: 0 additions & 6 deletions server/src/database.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AirportInfoDocument>(CacheName.AirportInfo);

export async function connectToDatabase() {
const url = ENV.MONGO_DB_CONNECTION_STRING;
Expand Down Expand Up @@ -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,
Expand All @@ -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");
}
10 changes: 8 additions & 2 deletions server/src/jobs/getVatsimData.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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<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.
const dispose = DotLocker.lockSync("airports", {
Expand All @@ -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);
}
7 changes: 7 additions & 0 deletions server/src/main.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AirportInfoDocument>(CacheName.AirportInfo);

// If startup fails restart is reattempted 5 times every 30 seconds.
const restartAttemptWaitTime = 30 * 1000;
const maxRestartAttempts = 5;
Expand All @@ -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
Expand Down Expand Up @@ -65,6 +70,8 @@ async function shutdown() {
}

async function handleDeath() {
await cache.saveToFile(ENV.CACHE_DIRECTORY);
cache.printStatistics();
await shutdown();
process.exit();
}
Expand Down

0 comments on commit 55b490f

Please sign in to comment.