From f715f6425aaeee44977f53b5964c674e23a0e02a Mon Sep 17 00:00:00 2001 From: Christopher Hase Date: Fri, 28 Mar 2025 14:36:48 +0100 Subject: [PATCH] schedule next run part 8 --- broker.ts | 65 ++++++++++++++++++++----------------------------------- 1 file changed, 24 insertions(+), 41 deletions(-) diff --git a/broker.ts b/broker.ts index 7649575..ceb6a7f 100644 --- a/broker.ts +++ b/broker.ts @@ -1,11 +1,19 @@ import { exec } from 'child_process'; import * as fs from 'fs'; +// Config for scheduling the next time the main process (sending of horoscope) is run interface Config { daysInterval: number; 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; const nodemailer = require('nodemailer'); @@ -37,30 +45,27 @@ function executeCommand(): void { sendEmail(stdout); + //Load config once if (config == undefined) { config = loadConfig(); - } else { //TODO: JUST FOR DEBUGGING - console.log(`Config already read...`); - } + } console.log(config.daysInterval, config.timeOfDay); - //sleep(); - - // Berechne das nächste Ausführungsdatum + // Calculate next execution time const nextRunDate = calculateNextRunDate(config.daysInterval, config.timeOfDay); - console.log(`Gegenwärtig: ` + new Date()); - console.log(`Nächste Ausführung: ${nextRunDate}`); + //console.log(`Gegenwärtig: ` + new Date()); + console.log(`Next execution: ${nextRunDate}`); - // Starte das Scheduling + //Start Scheduling scheduleNextRun(nextRunDate); }); } -// **Hier rufen wir die Funktion beim Start der Datei auf** +// Run function once initially, called from Dockerfile executeCommand(); -// Funktion, um die Konfiguration zu laden +// Load the Configuration function loadConfig(): Config { console.log(`Load Config`); @@ -86,17 +91,8 @@ async function sendEmail(content: string) { } } -//Sleep. To avoid issue in pod: Completed -> Terminated -> CrashLoopBackOff -async function sleep() { - console.log("Go to sleep: ", new Date().toISOString()); - - // sleep 1h (3600000 ms) - setTimeout(() => { - sleep(); // recursion after 1h - }, 3600000); -} - - +//Calculate the time for the next execution of the main process, depending on configuration. +//Returns the time of the next process loop. function calculateNextRunDate(daysInterval: number, timeOfDay: string): Date { const currentDate = new Date(); @@ -116,41 +112,28 @@ function calculateNextRunDate(daysInterval: number, timeOfDay: string): Date { return currentDate;*/ - currentDate.setMinutes(currentDate.getMinutes() + 1); //TODO: JUST FOR DEBUGGING!!! + currentDate.setMinutes(currentDate.getMinutes() + 2); //TODO: JUST FOR DEBUGGING!!! return currentDate; } +//Schedule the next process loop. 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!!! + //run the main process executeCommand(); - // Berechne das nächste Ausführungsdatum und plane es erneut - - //TODO: TESTWEISE ENTFERNEN... - //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" ; - child?: Node; - value?: string; -} - -//generate 1) Parse Tree and 2) HTML +// Generate 1) Parse Tree and 2) HTML function html(inputText: string): string { const parseTree = parse(inputText); const htmlOutput = render(parseTree); @@ -158,7 +141,7 @@ function html(inputText: string): string { return htmlOutput; } -//generate the Parse Tree +// Generate the Parse Tree function parse(input: string): Node { console.log("Parse input text"); @@ -197,7 +180,7 @@ function parse(input: string): Node { return root; } -//generate HTML from Parse Tree +// Generate HTML from Parse Tree function render(node: Node): string { if (node == undefined) {