schedule next run part 1
All checks were successful
ci / build (push) Successful in 1m6s

This commit is contained in:
Christopher Hase 2025-03-28 13:04:35 +01:00
parent 6b25bc569f
commit cf719e90ac
2 changed files with 96 additions and 25 deletions

View file

@ -1,7 +1,22 @@
import { exec } from 'child_process'; import { exec } from 'child_process';
import * as fs from 'fs';
interface Config {
daysInterval: number;
timeOfDay: string;
}
var config : Config;
const nodemailer = require('nodemailer'); const nodemailer = require('nodemailer');
// 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
});
exec('iching divine', (error, stdout, stderr) => { exec('iching divine', (error, stdout, stderr) => {
console.log(`Begin`); console.log(`Begin`);
@ -19,15 +34,29 @@ exec('iching divine', (error, stdout, stderr) => {
console.log(`Send E-Mail`); console.log(`Send E-Mail`);
sendEmail(stdout); sendEmail(stdout);
sleep();
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);
}); });
// Create Transporter // Funktion, um die Konfiguration zu laden
const transporter = nodemailer.createTransport({ function loadConfig(): Config {
host: "mailhog.mailhog.svc.cluster.local", // MailHog ClusterIP const data = fs.readFileSync('config.json', 'utf-8');
port: 1025, // Standard-MailHog SMTP-Port return JSON.parse(data);
secure: false // MailHog needs no encryption }
});
// Send E-Mail // Send E-Mail
async function sendEmail(content: string) { async function sendEmail(content: string) {
@ -56,6 +85,46 @@ async function sleep() {
}, 3600000); }, 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) //node structure for the Parse Tree (it's a degenerated tree with one or zero children per node)
interface Node { interface Node {
type: "Root" | "Hexagram" | "Judgement" | "Images" | "ChangingLines" ; type: "Root" | "Hexagram" | "Judgement" | "Images" | "ChangingLines" ;
@ -127,7 +196,6 @@ function render(node: Node): string {
case "Root": case "Root":
return render(node.child!); return render(node.child!);
case "Hexagram": case "Hexagram":
//node.value = node.value?.replace("</h1>", "</h1><div style=\"border: 1px solid gray; border-radius: 25px; padding: 0 30px; width: auto; display: inline-block; text-align: center; box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.2);\"><h2>");
node.value = node.value?.replace("</h1>", "</h1><div style=\"border: 1px solid gray; border-radius: 25px; padding-left: 30px; padding-right: 30px; padding-top: 20px; width: auto; display: inline-block; text-align: center; box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.2);\"><h2>"); node.value = node.value?.replace("</h1>", "</h1><div style=\"border: 1px solid gray; border-radius: 25px; padding-left: 30px; padding-right: 30px; padding-top: 20px; width: auto; display: inline-block; text-align: center; box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.2);\"><h2>");
node.value = node.value?.replace("<br>", " - "); node.value = node.value?.replace("<br>", " - ");
outputHTML = "<br><p>" + node.value + "</h2></div></p>"; outputHTML = "<br><p>" + node.value + "</h2></div></p>";
@ -138,7 +206,6 @@ function render(node: Node): string {
case "ChangingLines" : case "ChangingLines" :
const regex = new RegExp("~", "g"); const regex = new RegExp("~", "g");
node.value = node.value?.replace(regex, ""); node.value = node.value?.replace(regex, "");
//outputHTML = "<p style=\"color: gray;\"><div style=\"border: 1px dashed gray; border-radius: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px; width: auto; display: inline-block; text-align: center; box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.2);\">" + node.value + "</div></p>";
outputHTML = "<p><div style=\"border: 1px dashed gray; border-radius: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px; width: auto; display: inline-block; color: gray; text-align: center; box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.2);\">" + node.value + "</div></p>"; outputHTML = "<p><div style=\"border: 1px dashed gray; border-radius: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px; width: auto; display: inline-block; color: gray; text-align: center; box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.2);\">" + node.value + "</div></p>";
outputHTML = outputHTML + render(node.child!); outputHTML = outputHTML + render(node.child!);

4
config.json Normal file
View file

@ -0,0 +1,4 @@
{
"daysInterval": 2,
"timeOfDay": "8:00"
}