games_app/backend/dist/bootstrap/appBootstrap.js

176 lines
7.3 KiB
JavaScript

"use strict";
/**
* Shared application bootstrap (migrations, background jobs, game registration).
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.bootstrapCore = bootstrapCore;
const config_1 = require("../config");
const connection_1 = require("../db/connection");
const bootstrap_1 = require("../games/bootstrap");
const engineRunner_1 = require("../games/engineRunner");
const depositMonitorService_1 = require("../services/depositMonitorService");
const securityService_1 = require("../services/securityService");
const fileIntegrityService_1 = require("../services/fileIntegrityService");
const dbTamperService_1 = require("../services/dbTamperService");
const sweepBatchService_1 = require("../services/sweepBatchService");
const sweepService_1 = require("../services/sweepService");
const countryService_1 = require("../services/countryService");
const userAdminStatsService_1 = require("../services/userAdminStatsService");
const adminUserExportService_1 = require("../services/adminUserExportService");
const referralService_1 = require("../services/referralService");
const analyticsMaterializedViews_1 = require("../utils/analyticsMaterializedViews");
const encryptionRotationReminderService_1 = require("../services/encryptionRotationReminderService");
const networkGuard_1 = require("../safety/networkGuard");
const productionSecretsGuard_1 = require("../safety/productionSecretsGuard");
const client_1 = require("../realtime/redis/client");
const DEPOSIT_POLL_INTERVAL_MS = 60 * 1000;
async function startDepositMonitor() {
const run = async () => {
try {
await (0, depositMonitorService_1.processAllDeposits)();
}
catch (e) {
console.error('Deposit monitor error:', e);
}
setTimeout(run, DEPOSIT_POLL_INTERVAL_MS);
};
setTimeout(run, 10 * 1000);
}
function startLedgerIntegrityMonitor() {
const intervalMs = (0, securityService_1.getIntegrityCheckIntervalMs)();
const run = async () => {
try {
const issues = await (0, securityService_1.runLedgerIntegrityCheck)();
if (issues.length > 0) {
console.warn(`[security] Ledger integrity check found ${issues.length} issue(s)`);
}
}
catch (e) {
console.error('Ledger integrity check error:', e);
}
setTimeout(run, intervalMs);
};
setTimeout(run, 30000);
}
function startFileIntegrityMonitor() {
const intervalMs = (0, securityService_1.getIntegrityCheckIntervalMs)();
const run = async () => {
try {
const issues = await (0, fileIntegrityService_1.runFileIntegrityCheck)();
if (issues.length > 0) {
console.warn(`[security] File integrity check found ${issues.length} issue(s)`);
}
}
catch (e) {
console.error('File integrity check error:', e);
}
setTimeout(run, intervalMs);
};
setTimeout(run, 45000);
}
function startDbTamperMonitor() {
const intervalMs = (0, dbTamperService_1.getTamperCheckIntervalMs)();
const run = async () => {
try {
const n = await (0, dbTamperService_1.processDbTamperEvents)();
if (n > 0) {
console.warn(`[security] Processed ${n} unauthorized DB change event(s)`);
}
}
catch (e) {
console.error('DB tamper monitor error:', e);
}
setTimeout(run, intervalMs);
};
setTimeout(run, 15000);
}
function startSweepBatchMonitor() {
const auto = config_1.config.treasury.sweepEnabled && config_1.config.treasury.sweepAutoExecute;
console.log(`[sweep] monitor start: SWEEP_ENABLED=${config_1.config.treasury.sweepEnabled} ` +
`SWEEP_AUTO_EXECUTE=${config_1.config.treasury.sweepAutoExecute} ` +
`allowlist=${config_1.config.treasury.sweepColdAllowlist.length} addr(s) ` +
`maxRaw=${config_1.config.treasury.sweepAutoMaxAmountRaw || '(none)'}`);
const run = async () => {
try {
if (config_1.config.treasury.sweepEnabled && config_1.config.treasury.sweepAutoExecute) {
const nAuto = await (0, sweepService_1.processAutoEligibleSweeps)();
if (nAuto > 0) {
console.log(`[sweep] auto-executed ${nAuto} sweep(s)`);
}
}
// Creates Approvals only when needed; also cancels orphan batches already auto-swept.
const n = await (0, sweepBatchService_1.createSweepBatchApprovalRequests)();
if (n > 0) {
console.log(`[sweep-batch] Enqueued ${n} batch approval request(s)`);
}
}
catch (e) {
console.error('Sweep batch job error:', e);
}
const nextMs = auto
? Math.min(sweepBatchService_1.SWEEP_BATCH_INTERVAL_MS, (0, sweepService_1.getSweepPollIntervalMs)())
: sweepBatchService_1.SWEEP_BATCH_INTERVAL_MS;
setTimeout(run, nextMs);
};
// First pass quickly so a pending sweep after deploy does not wait a full minute
setTimeout(run, auto ? 10000 : 60000);
}
function startReferralCommissionMonitor() {
const intervalMs = (0, referralService_1.getReferralCommissionPollIntervalMs)();
const run = async () => {
try {
const n = await (0, referralService_1.processPendingReferralCommissions)();
if (n > 0 && process.env.NODE_ENV !== 'production') {
console.log(`[referral] Settled ${n} pending commission(s)`);
}
}
catch (e) {
console.error('Referral commission monitor error:', e);
}
setTimeout(run, intervalMs);
};
setTimeout(run, 60000);
}
/** Core bootstrap shared by API and engine processes. */
async function bootstrapCore(opts = {}) {
const { startEngines = config_1.config.engineEnabled, startPlatformMonitors = true, runDevMigrations = config_1.config.env === 'development', } = opts;
(0, networkGuard_1.validateTestnetSafetyOrThrow)();
(0, networkGuard_1.validateProductionMainnetOrThrow)();
(0, productionSecretsGuard_1.validateProductionSecretsOrThrow)();
if (runDevMigrations) {
try {
await (0, connection_1.runMigrations)();
console.log('Migrations OK');
}
catch (e) {
console.warn('Migrations warning:', e);
}
}
await (0, countryService_1.ensureCountriesSeeded)();
if (config_1.config.redis.enabled) {
try {
await (0, client_1.connectRedis)();
console.log('[redis] Connected');
}
catch (e) {
console.warn('[redis] Connection failed (continuing without cache):', e);
}
}
(0, bootstrap_1.registerBuiltinGames)();
if (startPlatformMonitors) {
(0, userAdminStatsService_1.scheduleAdminStatsBackfill)();
(0, adminUserExportService_1.startAdminExportWorker)();
(0, analyticsMaterializedViews_1.scheduleAnalyticsMaterializedViewRefresh)();
startDepositMonitor();
startLedgerIntegrityMonitor();
startFileIntegrityMonitor();
startDbTamperMonitor();
startSweepBatchMonitor();
startReferralCommissionMonitor();
(0, encryptionRotationReminderService_1.scheduleEncryptionKeyRotationReminder)();
}
if (startEngines) {
(0, engineRunner_1.startRegisteredGameEngines)();
}
}
//# sourceMappingURL=appBootstrap.js.map