diff --git a/app/Jobs/ProcessStmtEntryDataJob.php b/app/Jobs/ProcessStmtEntryDataJob.php index 11e129b..92ac2c8 100644 --- a/app/Jobs/ProcessStmtEntryDataJob.php +++ b/app/Jobs/ProcessStmtEntryDataJob.php @@ -1,5 +1,7 @@ entryBatch)) { + $totalProcessed = 0; + // Process in smaller chunks for better memory management - foreach ($this->entryBatch as $entry) { - // Extract all stmt_entry_ids from the current chunk - $entryIds = array_column($entry, 'stmt_entry_id'); + foreach ($this->entryBatch as $entryChunk) { + foreach ($entryChunk as $entryData) { + // Gunakan updateOrCreate untuk menghindari duplicate key error + StmtEntry::updateOrCreate( + [ + 'stmt_entry_id' => $entryData['stmt_entry_id'] + ], + $entryData + ); - // Delete existing records with these IDs to avoid conflicts - StmtEntry::whereIn('stmt_entry_id', $entryIds)->delete(); - - // Insert all records in the chunk at once - StmtEntry::insert($entry); + $totalProcessed++; + } } - // Reset entry batch after processing + DB::commit(); + + Log::info("Berhasil memproses {$totalProcessed} record dengan updateOrCreate"); + + // Reset entry batch after successful processing $this->entryBatch = []; } } catch (Exception $e) { + DB::rollback(); + Log::error("Error in saveBatch: " . $e->getMessage() . "\n" . $e->getTraceAsString()); $this->errorCount += count($this->entryBatch); + // Reset batch even if there's an error to prevent reprocessing the same failed records $this->entryBatch = []; + + throw $e; } }