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

This commit is contained in:
Christopher Hase 2025-03-28 14:36:48 +01:00
parent 1a7d09c17e
commit f715f6425a

View file

@ -1,11 +1,19 @@
import { exec } from 'child_process'; import { exec } from 'child_process';
import * as fs from 'fs'; import * as fs from 'fs';
// Config for scheduling the next time the main process (sending of horoscope) is run
interface Config { interface Config {
daysInterval: number; daysInterval: number;
timeOfDay: string; timeOfDay: string;
} }
// 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" ;
child?: Node;
value?: string;
}
var config : Config; var config : Config;
const nodemailer = require('nodemailer'); const nodemailer = require('nodemailer');
@ -37,30 +45,27 @@ function executeCommand(): void {
sendEmail(stdout); sendEmail(stdout);
//Load config once
if (config == undefined) { if (config == undefined) {
config = loadConfig(); config = loadConfig();
} else { //TODO: JUST FOR DEBUGGING
console.log(`Config already read...`);
} }
console.log(config.daysInterval, config.timeOfDay); console.log(config.daysInterval, config.timeOfDay);
//sleep(); // Calculate next execution time
// Berechne das nächste Ausführungsdatum
const nextRunDate = calculateNextRunDate(config.daysInterval, config.timeOfDay); const nextRunDate = calculateNextRunDate(config.daysInterval, config.timeOfDay);
console.log(`Gegenwärtig: ` + new Date()); //console.log(`Gegenwärtig: ` + new Date());
console.log(`Nächste Ausführung: ${nextRunDate}`); console.log(`Next execution: ${nextRunDate}`);
// Starte das Scheduling //Start Scheduling
scheduleNextRun(nextRunDate); scheduleNextRun(nextRunDate);
}); });
} }
// **Hier rufen wir die Funktion beim Start der Datei auf** // Run function once initially, called from Dockerfile
executeCommand(); executeCommand();
// Funktion, um die Konfiguration zu laden // Load the Configuration
function loadConfig(): Config { function loadConfig(): Config {
console.log(`Load Config`); console.log(`Load Config`);
@ -86,17 +91,8 @@ async function sendEmail(content: string) {
} }
} }
//Sleep. To avoid issue in pod: Completed -> Terminated -> CrashLoopBackOff //Calculate the time for the next execution of the main process, depending on configuration.
async function sleep() { //Returns the time of the next process loop.
console.log("Go to sleep: ", new Date().toISOString());
// sleep 1h (3600000 ms)
setTimeout(() => {
sleep(); // recursion after 1h
}, 3600000);
}
function calculateNextRunDate(daysInterval: number, timeOfDay: string): Date { function calculateNextRunDate(daysInterval: number, timeOfDay: string): Date {
const currentDate = new Date(); const currentDate = new Date();
@ -116,41 +112,28 @@ function calculateNextRunDate(daysInterval: number, timeOfDay: string): Date {
return currentDate;*/ return currentDate;*/
currentDate.setMinutes(currentDate.getMinutes() + 1); //TODO: JUST FOR DEBUGGING!!! currentDate.setMinutes(currentDate.getMinutes() + 2); //TODO: JUST FOR DEBUGGING!!!
return currentDate; return currentDate;
} }
//Schedule the next process loop.
function scheduleNextRun(nextRunDate: Date) { function scheduleNextRun(nextRunDate: Date) {
const now = new Date(); const now = new Date();
const timeUntilNextRun = nextRunDate.getTime() - now.getTime(); const timeUntilNextRun = nextRunDate.getTime() - now.getTime();
if (timeUntilNextRun > 0) { if (timeUntilNextRun > 0) {
setTimeout(() => { setTimeout(() => {
// Hier wird der Prozess ausgeführt
console.log('Prozess wird ausgeführt!'); console.log('Prozess wird ausgeführt!');
//exec('iching divine'); //TODO: ÜBERPRÜFEN!!! //run the main process
executeCommand(); executeCommand();
// Berechne das nächste Ausführungsdatum und plane es erneut
//TODO: TESTWEISE ENTFERNEN...
//const newNextRunDate = calculateNextRunDate(config.daysInterval, config.timeOfDay);
//scheduleNextRun(newNextRunDate);
}, timeUntilNextRun); }, timeUntilNextRun);
} }
} }
// Generate 1) Parse Tree and 2) HTML
//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" ;
child?: Node;
value?: string;
}
//generate 1) Parse Tree and 2) HTML
function html(inputText: string): string { function html(inputText: string): string {
const parseTree = parse(inputText); const parseTree = parse(inputText);
const htmlOutput = render(parseTree); const htmlOutput = render(parseTree);
@ -158,7 +141,7 @@ function html(inputText: string): string {
return htmlOutput; return htmlOutput;
} }
//generate the Parse Tree // Generate the Parse Tree
function parse(input: string): Node { function parse(input: string): Node {
console.log("Parse input text"); console.log("Parse input text");
@ -197,7 +180,7 @@ function parse(input: string): Node {
return root; return root;
} }
//generate HTML from Parse Tree // Generate HTML from Parse Tree
function render(node: Node): string { function render(node: Node): string {
if (node == undefined) { if (node == undefined) {