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:
parent
c478e5003b
commit
af137e7d83
1 changed files with 107 additions and 95 deletions
44
class/preisbot.class.php
Normal file → Executable file
44
class/preisbot.class.php
Normal file → Executable file
|
|
@ -54,6 +54,7 @@ class PreisBot
|
||||||
{
|
{
|
||||||
global $conf, $user, $langs;
|
global $conf, $user, $langs;
|
||||||
|
|
||||||
|
try {
|
||||||
require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
|
require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
|
||||||
|
|
||||||
$priceSource = getDolGlobalString('PREISBOT_PRICE_SOURCE', 'cheapest');
|
$priceSource = getDolGlobalString('PREISBOT_PRICE_SOURCE', 'cheapest');
|
||||||
|
|
@ -63,7 +64,7 @@ class PreisBot
|
||||||
$updated = 0;
|
$updated = 0;
|
||||||
$skipped = 0;
|
$skipped = 0;
|
||||||
$errors = 0;
|
$errors = 0;
|
||||||
$priceChanges = array(); // Für Mail-Bericht
|
$priceChanges = array();
|
||||||
|
|
||||||
// Hole alle Produkte mit gesetztem Gewinnaufschlag (nur zum Verkauf stehende)
|
// Hole alle Produkte mit gesetztem Gewinnaufschlag (nur zum Verkauf stehende)
|
||||||
$sql = "SELECT p.rowid, p.ref, p.label, p.price, pe.preisbot_margin";
|
$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 .= " LEFT JOIN ".$this->db->prefix()."product_extrafields as pe ON pe.fk_object = p.rowid";
|
||||||
$sql .= " WHERE pe.preisbot_margin IS NOT NULL";
|
$sql .= " WHERE pe.preisbot_margin IS NOT NULL";
|
||||||
$sql .= " AND pe.preisbot_margin >= ".((float) $minMargin);
|
$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').")";
|
$sql .= " AND p.entity IN (".getEntity('product').")";
|
||||||
|
|
||||||
$resql = $this->db->query($sql);
|
$resql = $this->db->query($sql);
|
||||||
|
|
@ -113,21 +114,19 @@ class PreisBot
|
||||||
$product = new Product($this->db);
|
$product = new Product($this->db);
|
||||||
$product->fetch($productId);
|
$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(
|
$result = $product->updatePrice(
|
||||||
$newPrice, // neuer Preis
|
$newPrice,
|
||||||
$product->price_base_type, // HT oder TTC - BEIBEHALTEN
|
$product->price_base_type,
|
||||||
$user, // User
|
$user,
|
||||||
$product->tva_tx, // MwSt-Satz - BEIBEHALTEN
|
$product->tva_tx,
|
||||||
$product->price_min, // Mindestpreis - BEIBEHALTEN
|
$product->price_min,
|
||||||
0, // Level (Preisstufe)
|
0,
|
||||||
$product->tva_npr, // NPR - BEIBEHALTEN
|
$product->tva_npr,
|
||||||
0, // price by quantity
|
0,
|
||||||
0, // ignore autogen
|
0,
|
||||||
array(), // localtaxes
|
array(),
|
||||||
$product->default_vat_code, // default vat code - BEIBEHALTEN
|
$product->default_vat_code,
|
||||||
'Preisbot' // price_label - Bezeichnung der Preisänderung
|
'Preisbot'
|
||||||
);
|
);
|
||||||
|
|
||||||
if ($result > 0) {
|
if ($result > 0) {
|
||||||
|
|
@ -160,11 +159,24 @@ class PreisBot
|
||||||
|
|
||||||
// Benachrichtigung und Mail nur wenn es Änderungen gab
|
// Benachrichtigung und Mail nur wenn es Änderungen gab
|
||||||
if ($updated > 0) {
|
if ($updated > 0) {
|
||||||
|
try {
|
||||||
$this->sendNotification($priceChanges, $updated, $errors);
|
$this->sendNotification($priceChanges, $updated, $errors);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$this->output .= "Warnung: GlobalNotify fehlgeschlagen: ".$e->getMessage()."\n";
|
||||||
|
}
|
||||||
|
try {
|
||||||
$this->sendMailReport($priceChanges, $updated, $skipped, $errors);
|
$this->sendMailReport($priceChanges, $updated, $skipped, $errors);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$this->output .= "Warnung: Mail-Versand fehlgeschlagen: ".$e->getMessage()."\n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ($errors > 0) ? -1 : 0;
|
return ($errors > 0) ? -1 : 0;
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$this->error = 'PreisBot Exception: '.$e->getMessage();
|
||||||
|
$this->output .= 'FEHLER: '.$e->getMessage()."\n";
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue