45 lines
2.4 KiB
TypeScript
45 lines
2.4 KiB
TypeScript
|
|
import {
|
||
|
|
AsyncLocalStorageOperationContextStore,
|
||
|
|
AuditChangeModel,
|
||
|
|
AuditEventModel,
|
||
|
|
OperationAuditContextProvider,
|
||
|
|
SequelizeAuditStore,
|
||
|
|
SequelizeTransactionContext,
|
||
|
|
auditModelInitializers,
|
||
|
|
closeSequelize,
|
||
|
|
createSequelize,
|
||
|
|
initModules,
|
||
|
|
registerModels,
|
||
|
|
} from "@erp/core/api";
|
||
|
|
import { Audit } from "@repo/rdx-audit";
|
||
|
|
import { configureLogger, setLoggerSingleton } from "@repo/rdx-logger";
|
||
|
|
|
||
|
|
import { loadWorkerConfig } from "./config/worker-config";
|
||
|
|
import { registerWorkerModules } from "./modules/register-modules";
|
||
|
|
import { Scheduler } from "./scheduler/scheduler";
|
||
|
|
import { registerJobs } from "./scheduler/register-jobs";
|
||
|
|
|
||
|
|
const logger = configureLogger("console", { service: "worker" });
|
||
|
|
setLoggerSingleton(logger);
|
||
|
|
const config = loadWorkerConfig(logger);
|
||
|
|
const operationContext = new AsyncLocalStorageOperationContextStore();
|
||
|
|
const transactionContext = new SequelizeTransactionContext();
|
||
|
|
let stopping: Promise<void> | null = null;
|
||
|
|
|
||
|
|
async function bootstrap(): Promise<void> {
|
||
|
|
logger.info("Worker starting");
|
||
|
|
const database = await createSequelize(config.database, logger);
|
||
|
|
const audit = new Audit({ source: { type: "application", id: "factuges-worker" }, contextProvider: new OperationAuditContextProvider(operationContext), store: new SequelizeAuditStore(AuditEventModel, AuditChangeModel, transactionContext) });
|
||
|
|
registerWorkerModules();
|
||
|
|
registerModels(auditModelInitializers, "worker-infrastructure");
|
||
|
|
const modulesRuntime = await initModules({ config, database, logger, operationContext, transactionContext, audit, warmup: { timeoutMs: 10_000, strict: false }, databaseSyncMode: "none", getService: () => { throw new Error("Services are available only in module scope."); } });
|
||
|
|
const scheduler = new Scheduler(registerJobs({ config, logger, runtime: modulesRuntime }), logger);
|
||
|
|
scheduler.start();
|
||
|
|
logger.info("Worker started");
|
||
|
|
const shutdown = async () => { if (stopping) return stopping; stopping = (async () => { logger.info("Worker stopping"); await scheduler.stop(); await closeSequelize(database, logger); logger.info("Worker stopped"); })(); return stopping; };
|
||
|
|
process.on("SIGINT", () => { shutdown().then(() => process.exit(0)).catch(() => process.exit(1)); });
|
||
|
|
process.on("SIGTERM", () => { shutdown().then(() => process.exit(0)).catch(() => process.exit(1)); });
|
||
|
|
}
|
||
|
|
|
||
|
|
bootstrap().catch((error) => { logger.error("Worker bootstrap failed", { error }); process.exit(1); });
|