FactuGES_ERP/apps/worker/src/index.ts
David d57b71c200 refactor: standardize import statements and formatting across components
- Updated import statements in `textarea.tsx`, `toggle.tsx`, and `tooltip.tsx` for consistency.
- Added missing semicolons and adjusted formatting in `use-mobile.ts` and `use-mobile.tsx`.
- Cleaned up `package.json` and `root.json` files by removing unnecessary line breaks.
- Simplified `turbo.json` by consolidating array formatting for better readability.
2026-09-10 13:26:33 +02:00

86 lines
2.6 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 { registerJobs } from "./scheduler/register-jobs";
import { Scheduler } from "./scheduler/scheduler";
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);
});