iching-broker-level1/broker.ts
Christopher Hase 0c32957d7f
All checks were successful
ci / build (push) Successful in 1m3s
avoid CrashLoopBackOff by sleeping after sending E-Mail
2025-03-26 12:44:42 +01:00

61 lines
No EOL
1.4 KiB
TypeScript

import { exec } from 'child_process';
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(`Ergebnis:\n${stdout}`);
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
});
// Send E-Mail
async function sendEmail(content: string) {
try {
const info = await transporter.sendMail({
from: '"Test Sender" <test@example.com>',
to: "Christopher.Hase@telekom.com;test@mailhog.local",
subject: "Horoscope from MailHog",
text: content,
html: "<p> ${content} </p>"
});
console.log("E-Mail sent: ", info.messageId);
} catch (error) {
console.error("Error Sending E-Mail:", error);
}
}
//Sleep. To avoid: Completed -> Terminated -> CrashLoopBackOff
async function sleep() {
console.log("Go to sleep: ", new Date().toISOString());
// sleep 1h (3600000 ms)
setTimeout(() => {
sleep(); // recursion after 1h
}, 3600000);
}