fix: Add try-catch to prevent cronjob from hanging

- Wrap updatePrices() in try-catch for fatal errors
- Separate try-catch for GlobalNotify and mail sending
- Prevents SMTP timeouts from blocking the entire cronjob

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Eduard Wisch 2026-03-10 14:07:48 +01:00
parent c478e5003b
commit af137e7d83

44
class/preisbot.class.php Normal file → Executable file
View file

@ -54,6 +54,7 @@ class PreisBot
{
global $conf, $user, $langs;
try {
require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
$priceSource = getDolGlobalString('PREISBOT_PRICE_SOURCE', 'cheapest');
@ -63,7 +64,7 @@ class PreisBot
$updated = 0;
$skipped = 0;
$errors = 0;
$priceChanges = array(); // Für Mail-Bericht
$priceChanges = array();
// Hole alle Produkte mit gesetztem Gewinnaufschlag (nur zum Verkauf stehende)
$sql = "SELECT p.rowid, p.ref, p.label, p.price, pe.preisbot_margin";
@ -71,7 +72,7 @@ class PreisBot
$sql .= " LEFT JOIN ".$this->db->prefix()."product_extrafields as pe ON pe.fk_object = p.rowid";
$sql .= " WHERE pe.preisbot_margin IS NOT NULL";
$sql .= " AND pe.preisbot_margin >= ".((float) $minMargin);
$sql .= " AND p.tosell = 1"; // Nur Produkte die zum Verkauf stehen
$sql .= " AND p.tosell = 1";
$sql .= " AND p.entity IN (".getEntity('product').")";
$resql = $this->db->query($sql);
@ -113,21 +114,19 @@ class PreisBot
$product = new Product($this->db);
$product->fetch($productId);
// updatePrice($newprice, $newpricebase, $user, $newvat, $newminprice, $level, $newnpr, $newpbq, $ignore_autogen, $localtaxes_array, $newdefaultvatcode, $price_label, $notrigger)
// Wir aktualisieren NUR den Preis, behalten aber alle anderen Werte bei
$result = $product->updatePrice(
$newPrice, // neuer Preis
$product->price_base_type, // HT oder TTC - BEIBEHALTEN
$user, // User
$product->tva_tx, // MwSt-Satz - BEIBEHALTEN
$product->price_min, // Mindestpreis - BEIBEHALTEN
0, // Level (Preisstufe)
$product->tva_npr, // NPR - BEIBEHALTEN
0, // price by quantity
0, // ignore autogen
array(), // localtaxes
$product->default_vat_code, // default vat code - BEIBEHALTEN
'Preisbot' // price_label - Bezeichnung der Preisänderung
$newPrice,
$product->price_base_type,
$user,
$product->tva_tx,
$product->price_min,
0,
$product->tva_npr,
0,
0,
array(),
$product->default_vat_code,
'Preisbot'
);
if ($result > 0) {
@ -160,11 +159,24 @@ class PreisBot
// Benachrichtigung und Mail nur wenn es Änderungen gab
if ($updated > 0) {
try {
$this->sendNotification($priceChanges, $updated, $errors);
} catch (Exception $e) {
$this->output .= "Warnung: GlobalNotify fehlgeschlagen: ".$e->getMessage()."\n";
}
try {
$this->sendMailReport($priceChanges, $updated, $skipped, $errors);
} catch (Exception $e) {
$this->output .= "Warnung: Mail-Versand fehlgeschlagen: ".$e->getMessage()."\n";
}
}
return ($errors > 0) ? -1 : 0;
} catch (Exception $e) {
$this->error = 'PreisBot Exception: '.$e->getMessage();
$this->output .= 'FEHLER: '.$e->getMessage()."\n";
return -1;
}
}
/**