From 7498d1408745afce1f133c78adb467352462f902 Mon Sep 17 00:00:00 2001 From: Daeng Deni Mardaeni Date: Thu, 29 May 2025 21:57:39 +0700 Subject: [PATCH] refactor(webstatement): optimize ProcessTellerDataJob batching Ubah alur proses batch pada job untuk meningkatkan efisiensi dan manajemen memori. - Mengganti metode `upsert` dengan pendekatan manual yang memecah data batch menjadi potongan lebih kecil. - Menambahkan proses penghapusan record lama berdasarkan `id_teller` sebelum melakukan insert baru untuk menghindari konflik data. - Menggunakan loop untuk memproses batch dalam potongan-potongan kecil, sehingga mengurangi beban memori. - Memastikan batch direset setelah selesai diproses untuk menghindari kebocoran data atau konflik. Signed-off-by: Daeng Deni Mardaeni --- app/Jobs/ProcessTellerDataJob.php | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/app/Jobs/ProcessTellerDataJob.php b/app/Jobs/ProcessTellerDataJob.php index 0601cda..ea0bd05 100644 --- a/app/Jobs/ProcessTellerDataJob.php +++ b/app/Jobs/ProcessTellerDataJob.php @@ -293,14 +293,19 @@ { try { if (!empty($this->tellerBatch)) { - // Bulk insert/update teller records - Teller::upsert( - $this->tellerBatch, - ['id_teller'], // Unique key - array_diff(array_values(self::HEADER_MAP), ['id_teller']) // Update columns - ); + // Process in smaller chunks for better memory management + foreach ($this->tellerBatch as $entry) { + // Extract all stmt_entry_ids from the current chunk + $entryIds = array_column($entry, 'id_teller'); - // Reset batch after processing + // Delete existing records with these IDs to avoid conflicts + Teller::whereIn('id_teller', $entryIds)->delete(); + + // Insert all records in the chunk at once + Teller::insert($entry); + } + + // Reset entry batch after processing $this->tellerBatch = []; } } catch (Exception $e) {