Improve Scheduler task with try-catch-finally. Jobs should be deleted from db on error to prevent looping. Also errors should be logged

This commit is contained in:
Andreas Palm 2024-04-23 22:18:11 +02:00
parent b3b516d0ad
commit c834816e19

View File

@ -15,6 +15,7 @@ use DateTimeImmutable;
use DateTimeInterface; use DateTimeInterface;
use Exception; use Exception;
use Xentral\Components\Database\Database; use Xentral\Components\Database\Database;
use Xentral\Components\Logger\Logger;
use Xentral\Modules\SubscriptionCycle\Service\SubscriptionCycleJobService; use Xentral\Modules\SubscriptionCycle\Service\SubscriptionCycleJobService;
use Xentral\Modules\SubscriptionCycle\SubscriptionCycleModuleInterface; use Xentral\Modules\SubscriptionCycle\SubscriptionCycleModuleInterface;
use Xentral\Modules\SubscriptionCycle\SubscriptionModuleInterface; use Xentral\Modules\SubscriptionCycle\SubscriptionModuleInterface;
@ -58,15 +59,23 @@ final class SubscriptionCycleManualJobTask
$this->taskMutexService->setMutex('rechnungslauf_manual'); $this->taskMutexService->setMutex('rechnungslauf_manual');
$jobs = $this->cycleJobService->listAll(100); $jobs = $this->cycleJobService->listAll(100);
foreach ($jobs as $job) { foreach ($jobs as $job) {
switch ($job['document_type']) { try {
case 'rechnung': switch ($job['document_type']) {
$this->subscriptionModule->CreateInvoice((int)$job['address_id']); case 'rechnung':
break; $this->subscriptionModule->CreateInvoice((int)$job['address_id']);
case 'auftrag': break;
$this->subscriptionModule->CreateOrder((int)$job['address_id']); case 'auftrag':
break; $this->subscriptionModule->CreateOrder((int)$job['address_id']);
} break;
$this->cycleJobService->delete((int)$job['id']); }
}
catch (Exception $e) {
/** @var Logger $logger */
$logger = $this->app->Container->get('Logger');
$logger->error($e->getMessage(), $job);
} finally {
$this->cycleJobService->delete((int)$job['id']);
}
} }
} }