diff --git a/broker.ts b/broker.ts index d04b2e0..a9218c2 100644 --- a/broker.ts +++ b/broker.ts @@ -1,33 +1,62 @@ import { exec } from 'child_process'; +import * as fs from 'fs'; + +interface Config { + daysInterval: number; + timeOfDay: string; +} + +var config : Config; const nodemailer = require('nodemailer'); -exec('iching divine', (error, stdout, stderr) => { - - console.log(`Begin`); - - if (error) { - console.error(`Error: ${error.message}`); - return; - } - - if (stderr) { - console.error(`Stderr: ${stderr}`); - return; - } - - console.log(`Send E-Mail`); - - sendEmail(stdout); - sleep(); -}); - // Create Transporter const transporter = nodemailer.createTransport({ - host: "mailhog.mailhog.svc.cluster.local", // MailHog ClusterIP - port: 1025, // Standard-MailHog SMTP-Port - secure: false // MailHog needs no encryption + host: "mailhog.mailhog.svc.cluster.local", // MailHog ClusterIP + port: 1025, // Standard-MailHog SMTP-Port + secure: false // MailHog needs no encryption }); + +exec('iching divine', (error, stdout, stderr) => { + + console.log(`Begin`); + + if (error) { + console.error(`Error: ${error.message}`); + return; + } + + if (stderr) { + console.error(`Stderr: ${stderr}`); + return; + } + + console.log(`Send E-Mail`); + + sendEmail(stdout); + + if (config == undefined) { + config = loadConfig(); + } + + console.log(config.daysInterval, config.timeOfDay); + + //sleep(); + + // Berechne das nächste Ausführungsdatum + const nextRunDate = calculateNextRunDate(config.daysInterval, config.timeOfDay); + console.log(`Nächste Ausführung: ${nextRunDate}`); + + // Starte das Scheduling + scheduleNextRun(nextRunDate); +}); + +// Funktion, um die Konfiguration zu laden +function loadConfig(): Config { + const data = fs.readFileSync('config.json', 'utf-8'); + return JSON.parse(data); +} + // Send E-Mail async function sendEmail(content: string) { @@ -56,6 +85,46 @@ async function sleep() { }, 3600000); } + +function calculateNextRunDate(daysInterval: number, timeOfDay: string): Date { + const currentDate = new Date(); + + // Splitte die Uhrzeit in Stunden und Minuten + const [hours, minutes] = timeOfDay.split(':').map(Number); + + // Berechne das Datum für die nächste Ausführung + currentDate.setHours(hours, minutes, 0, 0); // Setze die Uhrzeit auf Y (z.B. 14:30) + + // Wenn die Uhrzeit bereits vorbei ist, verschiebe das Datum auf den nächsten Tag + if (currentDate < new Date()) { + currentDate.setDate(currentDate.getDate() + 1); // Setze auf den nächsten Tag + } + + // Berechne das Datum für den nächsten Ausführungszeitpunkt unter Berücksichtigung von X Tagen + currentDate.setDate(currentDate.getDate() + daysInterval); + + return currentDate; +} + +function scheduleNextRun(nextRunDate: Date) { + const now = new Date(); + const timeUntilNextRun = nextRunDate.getTime() - now.getTime(); + + if (timeUntilNextRun > 0) { + setTimeout(() => { + // Hier wird der Prozess ausgeführt + console.log('Prozess wird ausgeführt!'); + + exec('iching divine'); //TODO: ÜBERPRÜFEN!!! + + // Berechne das nächste Ausführungsdatum und plane es erneut + const newNextRunDate = calculateNextRunDate(config.daysInterval, config.timeOfDay); + scheduleNextRun(newNextRunDate); + }, timeUntilNextRun); + } +} + + //node structure for the Parse Tree (it's a degenerated tree with one or zero children per node) interface Node { type: "Root" | "Hexagram" | "Judgement" | "Images" | "ChangingLines" ; @@ -127,7 +196,6 @@ function render(node: Node): string { case "Root": return render(node.child!); case "Hexagram": - //node.value = node.value?.replace("", "

"); node.value = node.value?.replace("

", "

"); node.value = node.value?.replace("
", " - "); outputHTML = "

" + node.value + "

"; @@ -138,7 +206,6 @@ function render(node: Node): string { case "ChangingLines" : const regex = new RegExp("~", "g"); node.value = node.value?.replace(regex, ""); - //outputHTML = "

" + node.value + "

"; outputHTML = "

" + node.value + "

"; outputHTML = outputHTML + render(node.child!); diff --git a/config.json b/config.json new file mode 100644 index 0000000..5b54a20 --- /dev/null +++ b/config.json @@ -0,0 +1,4 @@ +{ + "daysInterval": 2, + "timeOfDay": "8:00" + } \ No newline at end of file