Skip to content

Commit

Permalink
Improved API starting times
Browse files Browse the repository at this point in the history
  • Loading branch information
jonit-dev committed Feb 1, 2024
1 parent d95bbac commit 2ce4755
Showing 1 changed file with 8 additions and 31 deletions.
39 changes: 8 additions & 31 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import "reflect-metadata";

import "express-async-errors";
import "reflect-metadata";

import { appEnv } from "@providers/config/env";
import {
Expand All @@ -17,8 +16,6 @@ import { router } from "@providers/server/Router";
import { app } from "@providers/server/app";
import { NewRelicTransactionCategory } from "@providers/types/NewRelicTypes";
import { EnvType } from "@rpg-engine/shared/dist";
import * as Sentry from "@sentry/node";
import * as Tracing from "@sentry/tracing";
import dayjs from "dayjs";
import duration from "dayjs/plugin/duration";

Expand All @@ -37,47 +34,27 @@ const server = app.listen(port, async () => {
NewRelicTransactionCategory.Operation,
"ServerBootstrap",
async () => {
const endTime = Date.now();
const bootstrapTime = endTime - startTime;

await database.initialize();
await redisManager.connect();
// Run database and Redis initialization tasks in parallel
await Promise.all([database.initialize(), redisManager.connect(), socketAdapter.init(appEnv.socket.type)]);

// Start cron jobs
cronJobs.start();
await socketAdapter.init(appEnv.socket.type);

app.use(Sentry.Handlers.requestHandler());
app.use(Sentry.Handlers.tracingHandler());
app.use(router);

app.use(Sentry.Handlers.errorHandler());
app.use(errorHandlerMiddleware);

//! Dev Warning: If you want to add a new operation on server bootstrap, make sure to add it to one of the methods below (check if needs to be executed in all PM2 instances or not.)

// Perform server bootstrap operations
await serverBootstrap.performOneTimeOperations();

await serverBootstrap.performMultipleInstancesOperations();

if (appEnv.general.ENV === EnvType.Production) {
Sentry.init({
dsn: appEnv.general.SENTRY_DNS_URL,
integrations: [
// enable HTTP calls tracing
new Sentry.Integrations.Http({ tracing: true }),
// enable Express.js middleware tracing
new Tracing.Integrations.Express({ app }),
],

// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production
tracesSampleRate: 1.0,
});

console.log(`✅ Application started succesfully on PMID ${process.env.pm_id}`);
}

const endTime = Date.now();
const bootstrapTime = endTime - startTime;

serverHelper.showBootstrapMessage({
appName: appEnv.general.APP_NAME!,
port: appEnv.general.SERVER_PORT!,
Expand Down

0 comments on commit 2ce4755

Please sign in to comment.