Skip to content

Commit

Permalink
[TECH] Réduire la latence lors de scale massif de containers ou de mi…
Browse files Browse the repository at this point in the history
…se en production en chargeant le référentiel et en initialisant la connexion à la base de données avant de démarrer l'API

 #10486
  • Loading branch information
pix-service-auto-merge authored Nov 6, 2024
2 parents c6a957e + c6bf01e commit db7e130
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
7 changes: 6 additions & 1 deletion api/db/knex-database-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,9 @@ async function emptyAllTables() {
return configuredKnex.raw(`${query}${tables}`);
}

export { disconnect, emptyAllTables, configuredKnex as knex };
async function prepareDatabaseConnection() {
await configuredKnex.raw('SELECT 1');
logger.info('Connection to database established.');
}

export { disconnect, emptyAllTables, configuredKnex as knex, prepareDatabaseConnection };
22 changes: 21 additions & 1 deletion api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,36 @@ import { validateEnvironmentVariables } from './src/shared/infrastructure/valida

validateEnvironmentVariables();

import { disconnect } from './db/knex-database-connection.js';
import { disconnect, prepareDatabaseConnection } from './db/knex-database-connection.js';
import { createServer } from './server.js';
import { config } from './src/shared/config.js';
import { learningContentCache } from './src/shared/infrastructure/caches/learning-content-cache.js';
import { initLearningContent } from './src/shared/infrastructure/datasources/learning-content/datasource.js';
import { temporaryStorage } from './src/shared/infrastructure/temporary-storage/index.js';
import { logger } from './src/shared/infrastructure/utils/logger.js';
import { redisMonitor } from './src/shared/infrastructure/utils/redis-monitor.js';

let server;

async function _setupEcosystem() {
/*
Load learning content from Redis to memory cache can take some time
Hence, we force this loading before the server starts so the requests can
immediately be responded.
*/
await initLearningContent();
/*
First connection with Knex requires infrastructure operations such as
DNS resolution. So we execute one harmless query to our database
so those matters are resolved before starting the server.
*/
await prepareDatabaseConnection();
}

const start = async function () {
if (config.featureToggles.setupEcosystemBeforeStart) {
await _setupEcosystem();
}
server = await createServer();
await server.start();
};
Expand Down
1 change: 1 addition & 0 deletions api/src/shared/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ const configuration = (function () {
isQuestEnabled: toBoolean(process.env.FT_ENABLE_QUESTS),
isTextToSpeechButtonEnabled: toBoolean(process.env.FT_ENABLE_TEXT_TO_SPEECH_BUTTON),
isV3EligibilityCheckEnabled: toBoolean(process.env.FT_ENABLE_V3_ELIGIBILITY_CHECK),
setupEcosystemBeforeStart: toBoolean(process.env.FT_SETUP_ECOSYSTEM_BEFORE_START) || false,
showExperimentalMissions: toBoolean(process.env.FT_SHOW_EXPERIMENTAL_MISSIONS),
showNewCampaignPresentationPage: toBoolean(process.env.FT_SHOW_NEW_CAMPAIGN_PRESENTATION_PAGE),
showNewResultPage: toBoolean(process.env.FT_SHOW_NEW_RESULT_PAGE),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,6 @@ const refreshLearningContentCacheRecords = async function () {
return learningContent;
};

export { extend, refreshLearningContentCacheRecords };
const initLearningContent = _DatasourcePrototype._getLearningContent;

export { extend, initLearningContent, refreshLearningContentCacheRecords };
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('Acceptance | Shared | Application | Controller | feature-toggle', func
'is-self-account-deletion-enabled': false,
'is-text-to-speech-button-enabled': false,
'is-v3-eligibility-check-enabled': false,
'setup-ecosystem-before-start': false,
'show-experimental-missions': false,
'show-new-campaign-presentation-page': false,
'show-new-result-page': false,
Expand Down

0 comments on commit db7e130

Please sign in to comment.