This commit is contained in:
2026-03-28 11:06:12 +01:00
parent 29fc4d6edb
commit 913bad66bd
12 changed files with 136652 additions and 1152 deletions

View File

@@ -521,6 +521,17 @@ $processor->registerHandler( \Domain\CronJob\CronJobType::APILO_SEND_ORDER, func
{
$mdb->update( 'pp_shop_orders', [ 'apilo_order_id' => $response['id'] ], [ 'id' => $order['id'] ] );
\Domain\Integrations\ApiloLogger::log( $mdb, 'send_order', (int)$order['id'], 'Zamówienie wysłane do Apilo (apilo_order_id: ' . $response['id'] . ')', [ 'http_code' => $http_code_send, 'response' => $response ] );
// Wyczyść stare stuck joby sync_payment/sync_status dla tego zamówienia
$orderPayloadJson = json_encode(['order_id' => (int)$order['id']]);
$mdb->delete('pp_cron_jobs', [
'AND' => [
'job_type' => [\Domain\CronJob\CronJobType::APILO_SYNC_PAYMENT, \Domain\CronJob\CronJobType::APILO_SYNC_STATUS],
'payload' => $orderPayloadJson,
'status' => [\Domain\CronJob\CronJobType::STATUS_PENDING, \Domain\CronJob\CronJobType::STATUS_FAILED],
]
]);
echo '<p>Wysłałem zamówienie do apilo.com: ID: ' . $order['id'] . ' - ' . $response['id'] . '</p>';
}
}
@@ -760,7 +771,8 @@ $processor->registerHandler( \Domain\CronJob\CronJobType::TRUSTMATE_INVITATION,
$result = $processor->run( 20 );
// Powiadomienie mailowe o trwale nieudanych zadaniach Apilo
// Powiadomienie mailowe o problemach Apilo
// 1. Trwale failed joby (nie-order: token, product sync itp.)
$failedApiloJobs = $mdb->select('pp_cron_jobs', ['id', 'job_type', 'last_error', 'payload', 'attempts', 'completed_at'], [
'AND' => [
'status' => 'failed',
@@ -768,16 +780,51 @@ $failedApiloJobs = $mdb->select('pp_cron_jobs', ['id', 'job_type', 'last_error',
'completed_at[>=]' => date('Y-m-d H:i:s', time() - 120),
]
]);
if (!empty($failedApiloJobs)) {
$emailBody = "Następujące zadania Apilo zakończyły się trwałym błędem (wyczerpano limit prób):\n\n";
foreach ($failedApiloJobs as $fj) {
$emailBody .= "Job #" . $fj['id'] . " (" . $fj['job_type'] . ")\n";
$emailBody .= " Payload: " . $fj['payload'] . "\n";
// 2. Order joby z wieloma próbami (infinite retry, ale wymagają uwagi)
$stuckOrderJobs = $mdb->select('pp_cron_jobs', ['id', 'job_type', 'last_error', 'payload', 'attempts', 'scheduled_at'], [
'AND' => [
'status' => 'pending',
'job_type' => [\Domain\CronJob\CronJobType::APILO_SEND_ORDER, \Domain\CronJob\CronJobType::APILO_SYNC_PAYMENT, \Domain\CronJob\CronJobType::APILO_SYNC_STATUS],
'attempts[>=]' => 10,
]
]);
$allProblems = array_merge($failedApiloJobs, $stuckOrderJobs);
if (!empty($allProblems)) {
$emailBody = "";
$orderNumbers = [];
foreach ($allProblems as $fj) {
$payloadData = is_string($fj['payload']) ? json_decode($fj['payload'], true) : $fj['payload'];
$orderId = isset($payloadData['order_id']) ? (int)$payloadData['order_id'] : 0;
$isOrderJob = \Domain\CronJob\CronJobType::isOrderRelatedApiloJob($fj['job_type']);
$statusLabel = $isOrderJob ? 'PONAWIANY CO 30 MIN' : 'TRWAŁY BŁĄD';
$emailBody .= "Job #" . $fj['id'] . " (" . $fj['job_type'] . ") — " . $statusLabel . "\n";
if ($orderId > 0) {
$order = $mdb->get('pp_shop_orders', ['id', 'client_name', 'client_surname', 'date_order', 'summary'], ['id' => $orderId]);
if ($order) {
$emailBody .= " Zamówienie: #" . $order['id'] . "\n";
$emailBody .= " Klient: " . trim($order['client_name'] . ' ' . $order['client_surname']) . "\n";
$emailBody .= " Data zamówienia: " . $order['date_order'] . "\n";
$emailBody .= " Kwota: " . $order['summary'] . " PLN\n";
$orderNumbers[] = '#' . $order['id'];
}
}
$emailBody .= " Próby: " . $fj['attempts'] . "\n";
$emailBody .= " Błąd: " . $fj['last_error'] . "\n";
$emailBody .= " Data: " . $fj['completed_at'] . "\n\n";
$emailBody .= " Data: " . ($fj['completed_at'] ? $fj['completed_at'] : $fj['scheduled_at']) . "\n\n";
}
\Shared\Helpers\Helpers::send_email('biuro@project-pro.pl', 'shopPRO: Trwały błąd synchronizacji Apilo (' . count($failedApiloJobs) . ' zadań)', $emailBody);
$subject = 'shopPRO: Problemy synchronizacji Apilo';
if (!empty($orderNumbers)) {
$subject .= ' — zamówienia ' . implode(', ', array_unique($orderNumbers));
}
$subject .= ' (' . count($allProblems) . ' zadań)';
\Shared\Helpers\Helpers::send_email('biuro@project-pro.pl', $subject, $emailBody);
}
echo '<hr>';