Initial project structure
This commit is contained in:
@@ -0,0 +1,448 @@
|
||||
import * as amqp from "amqplib";
|
||||
import os from "os";
|
||||
import { loadConfig, saveConfig } from "./config";
|
||||
import { ProcessManager } from "./processManager";
|
||||
import { StatusReporter } from "./statusReporter";
|
||||
import { QueryManager } from "./queryManager";
|
||||
|
||||
|
||||
class Wrapper {
|
||||
|
||||
processManager =
|
||||
new ProcessManager();
|
||||
|
||||
queryManager =
|
||||
new QueryManager(
|
||||
this.processManager
|
||||
);
|
||||
|
||||
connection!: amqp.ChannelModel;
|
||||
channel!: amqp.Channel;
|
||||
|
||||
|
||||
id?: string;
|
||||
|
||||
|
||||
|
||||
async start(){
|
||||
|
||||
|
||||
const config =
|
||||
loadConfig();
|
||||
|
||||
|
||||
|
||||
if(config.wrapperId){
|
||||
|
||||
|
||||
this.id =
|
||||
config.wrapperId;
|
||||
|
||||
|
||||
|
||||
console.log(
|
||||
"Starte mit vorhandener ID:",
|
||||
this.id
|
||||
);
|
||||
|
||||
|
||||
}else{
|
||||
|
||||
|
||||
console.log(
|
||||
"Keine Wrapper ID vorhanden - neue Registrierung"
|
||||
);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
this.connection =
|
||||
await amqp.connect(
|
||||
"amqp://manager:gigi1337@192.168.1.74"
|
||||
);
|
||||
|
||||
|
||||
|
||||
this.channel =
|
||||
await this.connection.createChannel();
|
||||
|
||||
|
||||
|
||||
console.log(
|
||||
"RabbitMQ verbunden"
|
||||
);
|
||||
|
||||
|
||||
|
||||
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()
|
||||
);
|
||||
|
||||
|
||||
|
||||
console.log(
|
||||
"Antwort vom Manager:",
|
||||
data
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
if(data.type==="wrapper.registered"){
|
||||
|
||||
|
||||
|
||||
this.id =
|
||||
data.wrapperId;
|
||||
|
||||
|
||||
|
||||
saveConfig({
|
||||
|
||||
wrapperId:
|
||||
this.id
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
console.log(
|
||||
"Wrapper ID gespeichert:",
|
||||
this.id
|
||||
);
|
||||
|
||||
if(!this.id){
|
||||
|
||||
console.error(
|
||||
"Keine Wrapper ID erhalten!"
|
||||
);
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
|
||||
this.listenCommands(
|
||||
data.commandQueue
|
||||
);
|
||||
|
||||
const reporter =
|
||||
new 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.hostname(),
|
||||
|
||||
|
||||
|
||||
cpu:
|
||||
os.cpus().length,
|
||||
|
||||
|
||||
|
||||
ram:
|
||||
Math.round(
|
||||
os.totalmem()/1024/1024
|
||||
),
|
||||
|
||||
|
||||
|
||||
replyQueue
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await this.channel.sendToQueue(
|
||||
|
||||
"wrapper.register",
|
||||
|
||||
Buffer.from(
|
||||
JSON.stringify(message)
|
||||
)
|
||||
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
console.log(
|
||||
"Registrierung gesendet",
|
||||
this.id ?? "ohne ID"
|
||||
);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
async listenCommands(queue:string){
|
||||
|
||||
|
||||
await this.channel.consume(
|
||||
|
||||
queue,
|
||||
|
||||
msg=>{
|
||||
|
||||
|
||||
if(!msg)
|
||||
return;
|
||||
|
||||
|
||||
|
||||
const command =
|
||||
JSON.parse(
|
||||
msg.content.toString()
|
||||
);
|
||||
|
||||
|
||||
|
||||
switch(command.type){
|
||||
|
||||
|
||||
|
||||
case "test":
|
||||
|
||||
|
||||
console.log(
|
||||
"TEST COMMAND:",
|
||||
command.message
|
||||
);
|
||||
|
||||
|
||||
break;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
case "server.start":
|
||||
|
||||
|
||||
this.processManager.startServer(
|
||||
command
|
||||
);
|
||||
|
||||
|
||||
break;
|
||||
|
||||
|
||||
case "server.stop":
|
||||
|
||||
|
||||
this.processManager.stopServer(
|
||||
command.serverId
|
||||
);
|
||||
|
||||
|
||||
break;
|
||||
|
||||
|
||||
default:
|
||||
|
||||
|
||||
console.log(
|
||||
"Unbekannter Command:",
|
||||
command
|
||||
);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
this.channel.ack(msg);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
);
|
||||
|
||||
|
||||
|
||||
console.log(
|
||||
"Warte auf Commands:",
|
||||
queue
|
||||
);
|
||||
|
||||
|
||||
|
||||
this.heartbeat();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
heartbeat(){
|
||||
|
||||
console.log("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
|
||||
)
|
||||
|
||||
)
|
||||
|
||||
);
|
||||
|
||||
|
||||
|
||||
console.log(
|
||||
"Heartbeat gesendet"
|
||||
);
|
||||
|
||||
|
||||
|
||||
},10000);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
new Wrapper().start();
|
||||
Reference in New Issue
Block a user