Codex commited on
Commit
6b2fff3
·
1 Parent(s): 533dfc4

Add SMTP timeout handling and better status email diagnostics

Browse files
Files changed (1) hide show
  1. server.js +13 -5
server.js CHANGED
@@ -28,7 +28,7 @@ const CHECK_INTERVAL = 60000; // 1 minute
28
  const SMTP_HOST = process.env.SMTP_HOST || 'smtp-relay.brevo.com';
29
  const SMTP_PORT = parseInt(process.env.SMTP_PORT || '587', 10);
30
  const SMTP_SECURE = process.env.SMTP_SECURE === 'true' || SMTP_PORT === 465;
31
- const SMTP_TIMEOUT = parseInt(process.env.SMTP_TIMEOUT_MS || '15000', 10);
32
  let emailTransport = null;
33
  let emailConfigChecked = false;
34
 
@@ -239,7 +239,7 @@ async function sendStatusReportEmail() {
239
  }
240
 
241
  const { subject, text, html } = buildStatusReportEmail();
242
- console.log(`Sending status report email via ${SMTP_HOST}:${SMTP_PORT} secure=${SMTP_SECURE} as ${smtpUser} to ${emailTo}`);
243
 
244
  await sendMailWithTimeout(transport, {
245
  from: emailFrom,
@@ -454,11 +454,12 @@ app.delete('/api/sites', async (req, res) => {
454
 
455
  // Manual status report email
456
  app.post('/api/status-email', async (req, res) => {
 
457
  try {
458
  await sendStatusReportEmail();
459
  res.json({ success: true });
460
  } catch (err) {
461
- console.error('Error sending status report:', err);
462
  res.status(500).json({ error: err.message || 'Failed to send status report email' });
463
  }
464
  });
@@ -926,8 +927,10 @@ app.get('/', (req, res) => {
926
  }
927
 
928
  async function sendStatusEmail() {
 
 
929
  try {
930
- const resp = await fetch('/api/status-email', { method: 'POST' });
931
  if (resp.ok) {
932
  alert('Raport został wysłany na e-mail.');
933
  } else {
@@ -936,8 +939,13 @@ app.get('/', (req, res) => {
936
  }
937
  } catch (error) {
938
  console.error('Error:', error);
939
- alert('Wystąpił błąd podczas wysyłania raportu.');
 
 
 
 
940
  }
 
941
  }
942
  </script>
943
  </body>
 
28
  const SMTP_HOST = process.env.SMTP_HOST || 'smtp-relay.brevo.com';
29
  const SMTP_PORT = parseInt(process.env.SMTP_PORT || '587', 10);
30
  const SMTP_SECURE = process.env.SMTP_SECURE === 'true' || SMTP_PORT === 465;
31
+ const SMTP_TIMEOUT = parseInt(process.env.SMTP_TIMEOUT_MS || '10000', 10);
32
  let emailTransport = null;
33
  let emailConfigChecked = false;
34
 
 
239
  }
240
 
241
  const { subject, text, html } = buildStatusReportEmail();
242
+ console.log(`Sending status report email via ${SMTP_HOST}:${SMTP_PORT} secure=${SMTP_SECURE} as ${smtpUser} to ${emailTo}, timeout ${SMTP_TIMEOUT}ms`);
243
 
244
  await sendMailWithTimeout(transport, {
245
  from: emailFrom,
 
454
 
455
  // Manual status report email
456
  app.post('/api/status-email', async (req, res) => {
457
+ console.log('Incoming status email request');
458
  try {
459
  await sendStatusReportEmail();
460
  res.json({ success: true });
461
  } catch (err) {
462
+ console.error('Error sending status report:', err && err.stack ? err.stack : err);
463
  res.status(500).json({ error: err.message || 'Failed to send status report email' });
464
  }
465
  });
 
927
  }
928
 
929
  async function sendStatusEmail() {
930
+ const controller = new AbortController();
931
+ const timer = setTimeout(() => controller.abort(), 12000);
932
  try {
933
+ const resp = await fetch('/api/status-email', { method: 'POST', signal: controller.signal });
934
  if (resp.ok) {
935
  alert('Raport został wysłany na e-mail.');
936
  } else {
 
939
  }
940
  } catch (error) {
941
  console.error('Error:', error);
942
+ if (error.name === 'AbortError') {
943
+ alert('Przekroczono czas oczekiwania na wysłanie raportu (timeout).');
944
+ } else {
945
+ alert('Wystąpił błąd podczas wysyłania raportu.');
946
+ }
947
  }
948
+ clearTimeout(timer);
949
  }
950
  </script>
951
  </body>