"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const amqp = __importStar(require("amqplib")); const os_1 = __importDefault(require("os")); const config_1 = require("./config"); const processManager_1 = require("./processManager"); const statusReporter_1 = require("./statusReporter"); const queryManager_1 = require("./queryManager"); const logger_1 = require("./logger"); const logger = (0, logger_1.getLogger)("Wrapper"); class Wrapper { constructor() { this.processManager = new processManager_1.ProcessManager(); this.queryManager = new queryManager_1.QueryManager(this.processManager); } async start() { const config = (0, config_1.loadConfig)(); logger.info("Konfiguration geladen"); if (config.wrapperId) { this.id = config.wrapperId; logger.debug("Starte mit vorhandener ID:", this.id); } else { logger.warn("Keine Wrapper ID vorhanden - neue Registrierung"); } this.connection = await amqp.connect(config_1.RABBITMQ_URL); this.channel = await this.connection.createChannel(); logger.info("RabbitMQ Verbindung hergestellt"); this.queryManager.start(); await this.register(); } async register() { const reply = await this.channel.assertQueue("", { exclusive: true }); const replyQueue = reply.queue; await this.channel.consume(replyQueue, msg => { if (!msg) return; const data = JSON.parse(msg.content.toString()); logger.debug("Antwort vom Manager:", data.type); if (data.type === "wrapper.registered") { this.id = data.wrapperId; (0, config_1.saveConfig)({ wrapperId: this.id }); logger.info("Registrierung beim Manager erfolgreich"); logger.debug("Wrapper ID gespeichert:", this.id); if (!this.id) { logger.error("Keine Wrapper ID erhalten!"); return; } this.listenCommands(data.commandQueue); const reporter = new statusReporter_1.StatusReporter(this.channel, this.processManager, this.queryManager, this.id); reporter.start(); } this.channel.ack(msg); }); const message = { type: "wrapper.register", // vorhandene ID mitsenden wrapperId: this.id, hostname: os_1.default.hostname(), cpu: os_1.default.cpus().length, ram: Math.round(os_1.default.totalmem() / 1024 / 1024), replyQueue }; await this.channel.sendToQueue("wrapper.register", Buffer.from(JSON.stringify(message))); logger.debug("Registrierung gesendet", this.id ?? "ohne ID"); } async listenCommands(queue) { await this.channel.consume(queue, msg => { if (!msg) return; const rawCommand = JSON.parse(msg.content.toString()); const commandType = rawCommand.type; switch (commandType) { case "test": { const command = rawCommand; logger.debug("TEST COMMAND:", command.message); break; } case "server.start": { const command = rawCommand; this.processManager.startServer(command); break; } case "server.stop": { const command = rawCommand; this.processManager.stopServer(command.serverId); break; } default: logger.warn("Unbekannter Command:", commandType); } this.channel.ack(msg); }); logger.debug("Warte auf Commands:", queue); this.heartbeat(); } heartbeat() { logger.debug("Heartbeat gestartet"); setInterval(() => { if (!this.id) return; const heartbeat = { type: "wrapper.heartbeat", wrapperId: this.id }; this.channel.sendToQueue("wrapper.register", Buffer.from(JSON.stringify(heartbeat))); logger.debug("Heartbeat gesendet"); }, config_1.WRAPPER_HEARTBEAT_INTERVAL); } } new Wrapper().start();