mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-17 12:56:29 +00:00
Revert "misc: added health and ready probe endpoints"
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
/* eslint-disable import/extensions */
|
||||
import path from "node:path";
|
||||
import { monitorEventLoopDelay } from "perf_hooks";
|
||||
|
||||
import type { FastifyCookieOptions } from "@fastify/cookie";
|
||||
import cookie from "@fastify/cookie";
|
||||
@@ -25,7 +24,6 @@ import { TQueueServiceFactory } from "@app/queue";
|
||||
import { TKmsRootConfigDALFactory } from "@app/services/kms/kms-root-config-dal";
|
||||
import { TSmtpService } from "@app/services/smtp/smtp-service";
|
||||
import { TSuperAdminDALFactory } from "@app/services/super-admin/super-admin-dal";
|
||||
import { getServerCfg } from "@app/services/super-admin/super-admin-service";
|
||||
|
||||
import { globalRateLimiterCfg } from "./config/rateLimiter";
|
||||
import { addErrorsToResponseSchemas } from "./plugins/add-errors-to-response-schemas";
|
||||
@@ -38,15 +36,6 @@ import { registerServeUI } from "./plugins/serve-ui";
|
||||
import { fastifySwagger } from "./plugins/swagger";
|
||||
import { registerRoutes } from "./routes";
|
||||
|
||||
const histogram = monitorEventLoopDelay({ resolution: 20 });
|
||||
histogram.enable();
|
||||
|
||||
const serverState = {
|
||||
isReady: false,
|
||||
isRunningMigrations: false,
|
||||
isWaitingForMigrations: true // Start as true - containers are unhealthy until they acquire migration lock or complete
|
||||
};
|
||||
|
||||
type TMain = {
|
||||
auditLogDb?: Knex;
|
||||
db: Knex;
|
||||
@@ -156,72 +145,7 @@ export const main = async ({
|
||||
})
|
||||
});
|
||||
|
||||
// Health check - returns 200 only if doing useful work (running migrations or ready)
|
||||
// Returns 503 if waiting for another container to finish migrations
|
||||
server.get("/api/health", async (_, reply) => {
|
||||
if (serverState.isWaitingForMigrations) {
|
||||
return reply.code(503).send({
|
||||
status: "waiting",
|
||||
message: "Waiting for migrations to complete in another container"
|
||||
});
|
||||
}
|
||||
return { status: "ok", message: "Server is alive" };
|
||||
});
|
||||
|
||||
// Global preHandler to block requests during migrations
|
||||
server.addHook("preHandler", async (request, reply) => {
|
||||
if (request.url === "/api/health" || request.url === "/api/ready") {
|
||||
return;
|
||||
}
|
||||
if (!serverState.isReady) {
|
||||
return reply.code(503).send({
|
||||
status: "unavailable",
|
||||
message: "Server is starting up, migrations in progress. Please try again in a moment."
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Readiness check - returns 503 until migrations are complete
|
||||
server.get("/api/ready", async (request, reply) => {
|
||||
const cfg = getConfig();
|
||||
|
||||
const meanLagMs = histogram.mean / 1e6;
|
||||
const maxLagMs = histogram.max / 1e6;
|
||||
const p99LagMs = histogram.percentile(99) / 1e6;
|
||||
|
||||
request.log.info(
|
||||
`Event loop stats - Mean: ${meanLagMs.toFixed(2)}ms, Max: ${maxLagMs.toFixed(2)}ms, p99: ${p99LagMs.toFixed(2)}ms`
|
||||
);
|
||||
|
||||
request.log.info(`Raw event loop stats: ${JSON.stringify(histogram, null, 2)}`);
|
||||
|
||||
if (!serverState.isReady) {
|
||||
return reply.code(503).send({
|
||||
date: new Date(),
|
||||
message: "Server is starting up, migrations in progress",
|
||||
emailConfigured: cfg.isSmtpConfigured,
|
||||
redisConfigured: cfg.isRedisConfigured,
|
||||
secretScanningConfigured: cfg.isSecretScanningConfigured,
|
||||
samlDefaultOrgSlug: cfg.samlDefaultOrgSlug,
|
||||
auditLogStorageDisabled: Boolean(cfg.DISABLE_AUDIT_LOG_STORAGE)
|
||||
});
|
||||
}
|
||||
|
||||
const serverCfg = await getServerCfg();
|
||||
|
||||
return {
|
||||
date: new Date(),
|
||||
message: "Ok",
|
||||
emailConfigured: cfg.isSmtpConfigured,
|
||||
inviteOnlySignup: Boolean(serverCfg.allowSignUp),
|
||||
redisConfigured: cfg.isRedisConfigured,
|
||||
secretScanningConfigured: cfg.isSecretScanningConfigured,
|
||||
samlDefaultOrgSlug: cfg.samlDefaultOrgSlug,
|
||||
auditLogStorageDisabled: Boolean(cfg.DISABLE_AUDIT_LOG_STORAGE)
|
||||
};
|
||||
});
|
||||
|
||||
const completeServerInitialization = await registerRoutes(server, {
|
||||
await server.register(registerRoutes, {
|
||||
smtp,
|
||||
queue,
|
||||
db,
|
||||
@@ -240,21 +164,10 @@ export const main = async ({
|
||||
|
||||
await server.ready();
|
||||
server.swagger();
|
||||
return { server, completeServerInitialization };
|
||||
return server;
|
||||
} catch (err) {
|
||||
server.log.error(err);
|
||||
await queue.shutdown();
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
export const markServerReady = () => {
|
||||
serverState.isReady = true;
|
||||
serverState.isRunningMigrations = false;
|
||||
serverState.isWaitingForMigrations = false;
|
||||
};
|
||||
|
||||
export const markRunningMigrations = () => {
|
||||
serverState.isRunningMigrations = true;
|
||||
serverState.isWaitingForMigrations = false;
|
||||
};
|
||||
|
||||
@@ -2208,7 +2208,7 @@ export const registerRoutes = async (
|
||||
internalCaFns
|
||||
});
|
||||
|
||||
const secretRotationV2Queue = await secretRotationV2QueueServiceFactory({
|
||||
await secretRotationV2QueueServiceFactory({
|
||||
secretRotationV2Service,
|
||||
secretRotationV2DAL,
|
||||
queueService,
|
||||
@@ -2305,6 +2305,8 @@ export const registerRoutes = async (
|
||||
// If FIPS is enabled, we check to ensure that the users license includes FIPS mode.
|
||||
crypto.verifyFipsLicense(licenseService);
|
||||
|
||||
await superAdminService.initServerCfg();
|
||||
|
||||
// Start HSM service if it's configured/enabled.
|
||||
await hsmService.startService();
|
||||
|
||||
@@ -2329,60 +2331,21 @@ export const registerRoutes = async (
|
||||
}
|
||||
}
|
||||
|
||||
const completeServerInitialization = async () => {
|
||||
await superAdminService.initServerCfg();
|
||||
|
||||
await telemetryQueue.startTelemetryCheck();
|
||||
await telemetryQueue.startAggregatedEventsJob();
|
||||
await dailyResourceCleanUp.init();
|
||||
await healthAlert.init();
|
||||
await pkiSyncCleanup.init();
|
||||
await pamAccountRotation.init();
|
||||
await dailyReminderQueueService.startDailyRemindersJob();
|
||||
await dailyReminderQueueService.startSecretReminderMigrationJob();
|
||||
await dailyExpiringPkiItemAlert.startSendingAlerts();
|
||||
await pkiSubscriberQueue.startDailyAutoRenewalJob();
|
||||
await certificateV3Queue.init();
|
||||
await kmsService.startService(hsmStatus);
|
||||
await microsoftTeamsService.start();
|
||||
await dynamicSecretQueueService.init();
|
||||
await secretScanningV2Queue.init();
|
||||
await secretRotationV2Queue.init();
|
||||
await notificationQueue.init();
|
||||
await eventBusService.init();
|
||||
|
||||
const cronJobs: CronJob[] = [];
|
||||
if (appCfg.isProductionMode) {
|
||||
const rateLimitSyncJob = await rateLimitService.initializeBackgroundSync();
|
||||
if (rateLimitSyncJob) {
|
||||
cronJobs.push(rateLimitSyncJob);
|
||||
}
|
||||
const licenseSyncJob = await licenseService.initializeBackgroundSync();
|
||||
if (licenseSyncJob) {
|
||||
cronJobs.push(licenseSyncJob);
|
||||
}
|
||||
|
||||
const microsoftTeamsSyncJob = await microsoftTeamsService.initializeBackgroundSync();
|
||||
if (microsoftTeamsSyncJob) {
|
||||
cronJobs.push(microsoftTeamsSyncJob);
|
||||
}
|
||||
|
||||
const adminIntegrationsSyncJob = await superAdminService.initializeAdminIntegrationConfigSync();
|
||||
if (adminIntegrationsSyncJob) {
|
||||
cronJobs.push(adminIntegrationsSyncJob);
|
||||
}
|
||||
}
|
||||
|
||||
const configSyncJob = await superAdminService.initializeEnvConfigSync();
|
||||
if (configSyncJob) {
|
||||
cronJobs.push(configSyncJob);
|
||||
}
|
||||
|
||||
const oauthConfigSyncJob = await initializeOauthConfigSync();
|
||||
if (oauthConfigSyncJob) {
|
||||
cronJobs.push(oauthConfigSyncJob);
|
||||
}
|
||||
};
|
||||
await telemetryQueue.startTelemetryCheck();
|
||||
await telemetryQueue.startAggregatedEventsJob();
|
||||
await dailyResourceCleanUp.init();
|
||||
await healthAlert.init();
|
||||
await pkiSyncCleanup.init();
|
||||
await pamAccountRotation.init();
|
||||
await dailyReminderQueueService.startDailyRemindersJob();
|
||||
await dailyReminderQueueService.startSecretReminderMigrationJob();
|
||||
await dailyExpiringPkiItemAlert.startSendingAlerts();
|
||||
await pkiSubscriberQueue.startDailyAutoRenewalJob();
|
||||
await certificateV3Queue.init();
|
||||
await kmsService.startService(hsmStatus);
|
||||
await microsoftTeamsService.start();
|
||||
await dynamicSecretQueueService.init();
|
||||
await eventBusService.init();
|
||||
|
||||
// inject all services
|
||||
server.decorate<FastifyZodProvider["services"]>("services", {
|
||||
@@ -2512,6 +2475,38 @@ export const registerRoutes = async (
|
||||
convertor: convertorService
|
||||
});
|
||||
|
||||
const cronJobs: CronJob[] = [];
|
||||
if (appCfg.isProductionMode) {
|
||||
const rateLimitSyncJob = await rateLimitService.initializeBackgroundSync();
|
||||
if (rateLimitSyncJob) {
|
||||
cronJobs.push(rateLimitSyncJob);
|
||||
}
|
||||
const licenseSyncJob = await licenseService.initializeBackgroundSync();
|
||||
if (licenseSyncJob) {
|
||||
cronJobs.push(licenseSyncJob);
|
||||
}
|
||||
|
||||
const microsoftTeamsSyncJob = await microsoftTeamsService.initializeBackgroundSync();
|
||||
if (microsoftTeamsSyncJob) {
|
||||
cronJobs.push(microsoftTeamsSyncJob);
|
||||
}
|
||||
|
||||
const adminIntegrationsSyncJob = await superAdminService.initializeAdminIntegrationConfigSync();
|
||||
if (adminIntegrationsSyncJob) {
|
||||
cronJobs.push(adminIntegrationsSyncJob);
|
||||
}
|
||||
}
|
||||
|
||||
const configSyncJob = await superAdminService.initializeEnvConfigSync();
|
||||
if (configSyncJob) {
|
||||
cronJobs.push(configSyncJob);
|
||||
}
|
||||
|
||||
const oauthConfigSyncJob = await initializeOauthConfigSync();
|
||||
if (oauthConfigSyncJob) {
|
||||
cronJobs.push(oauthConfigSyncJob);
|
||||
}
|
||||
|
||||
server.decorate<FastifyZodProvider["store"]>("store", {
|
||||
user: userDAL,
|
||||
kmipClient: kmipClientDAL
|
||||
@@ -2600,10 +2595,9 @@ export const registerRoutes = async (
|
||||
await server.register(registerV4Routes, { prefix: "/api/v4" });
|
||||
|
||||
server.addHook("onClose", async () => {
|
||||
cronJobs.forEach((job) => job.stop());
|
||||
await telemetryService.flushAll();
|
||||
await eventBusService.close();
|
||||
sseService.close();
|
||||
});
|
||||
|
||||
return completeServerInitialization;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user