oneuptime/Worker/Index.ts

91 lines
2.4 KiB
TypeScript

import WorkerRoutes from "./Routes";
import { PromiseVoidFunction } from "Common/Types/FunctionTypes";
import InfrastructureStatus from "Common/Server/Infrastructure/Status";
import logger from "Common/Server/Utils/Logger";
import App from "Common/Server/Utils/StartServer";
import Telemetry from "Common/Server/Utils/Telemetry";
import Realtime from "Common/Server/Utils/Realtime";
import PostgresAppInstance from "Common/Server/Infrastructure/PostgresDatabase";
import Redis from "Common/Server/Infrastructure/Redis";
import { ClickhouseAppInstance } from "Common/Server/Infrastructure/ClickhouseDatabase";
import "ejs";
const APP_NAME: string = "worker";
const init: PromiseVoidFunction = async (): Promise<void> => {
try {
logger.debug("Initializing Worker");
// Initialize telemetry
Telemetry.init({
serviceName: APP_NAME,
});
logger.debug("Telemetry initialized");
const statusCheck: PromiseVoidFunction = async (): Promise<void> => {
// Check the status of infrastructure components
return await InfrastructureStatus.checkStatus({
checkClickhouseStatus: true,
checkPostgresStatus: true,
checkRedisStatus: true,
});
};
// Initialize the app with service name and status checks
await App.init({
appName: APP_NAME,
statusOptions: {
liveCheck: statusCheck,
readyCheck: statusCheck,
},
});
logger.debug("App initialized");
// Connect to Postgres database
await PostgresAppInstance.connect();
logger.debug("Postgres connected");
// Connect to Redis
await Redis.connect();
logger.debug("Redis connected");
// Connect to Clickhouse database
await ClickhouseAppInstance.connect(
ClickhouseAppInstance.getDatasourceOptions(),
);
logger.debug("Clickhouse connected");
// Initialize real-time functionalities
await Realtime.init();
logger.debug("Realtime initialized");
// Add default routes to the app
await App.addDefaultRoutes();
logger.debug("Default routes added");
// Initialize home routes at the end since it has a catch-all route
await WorkerRoutes.init();
logger.debug("Routes initialized");
logger.info("Worker Initialized Successfully");
} catch (err) {
logger.error("Worker Init Failed:");
logger.error(err);
throw err;
}
};
init().catch((err: Error) => {
logger.error(err);
logger.error("Exiting node process");
process.exit(1);
});