feat(sync-log): tambahkan integrasi log sinkronisasi untuk BiayaKartu dan GenerateBiayaKartuCsvJob
Penambahan dan perubahan: - Tambah model `KartuSyncLog` untuk mencatat status sinkronisasi, pembuatan CSV, dan upload SFTP. - Implementasi logging status sinkronisasi di `BiayaKartu`: - Logging status saat sinkronisasi dimulai, berhasil, atau gagal. - Statistik sinkronisasi mencakup jumlah data total, sukses, dan gagal. - Catatan progres sinkronisasi secara periodik. - Update proses CSV di `GenerateBiayaKartuCsvJob`: - Pembuatan log untuk status pembuatan CSV dan upload ke SFTP. - Kanalisasi dan pembaruan log saat pembuatan atau upload dimulai, berhasil, atau gagal. - Random delay untuk job update branch dan currency dimodifikasi menggunakan ID log sinkronisasi. Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
This commit is contained in:
@@ -10,7 +10,9 @@ use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Carbon\Carbon;
|
||||
use Modules\Webstatement\Models\Atmcard;
|
||||
use Modules\Webstatement\Models\KartuSyncLog;
|
||||
|
||||
class BiayaKartu implements ShouldQueue
|
||||
{
|
||||
@@ -23,16 +25,133 @@ class BiayaKartu implements ShouldQueue
|
||||
private const BATCH_SIZE = 1000;
|
||||
private const MAX_EXECUTION_TIME = 86400; // 24 jam dalam detik
|
||||
|
||||
/**
|
||||
* Log model untuk menyimpan status sinkronisasi
|
||||
*/
|
||||
protected $syncLog;
|
||||
|
||||
/**
|
||||
* Periode yang sedang disinkronkan (YYYY-MM)
|
||||
*/
|
||||
protected $periode;
|
||||
|
||||
/**
|
||||
* Statistik sinkronisasi
|
||||
*/
|
||||
protected $totalRecords = 0;
|
||||
protected $successRecords = 0;
|
||||
protected $failedRecords = 0;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*/
|
||||
public function __construct($periode = null)
|
||||
{
|
||||
// Jika periode tidak diberikan, gunakan bulan saat ini
|
||||
$this->periode = $periode ?? Carbon::now()->format('Y-m');
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
set_time_limit(self::MAX_EXECUTION_TIME);
|
||||
$this->syncAtmCards();
|
||||
|
||||
// Setelah sync selesai, jadwalkan job untuk memperbarui branch dan currency
|
||||
$this->scheduleUpdateBranchCurrencyJobs();
|
||||
// Inisialisasi atau ambil log sinkronisasi
|
||||
$this->initSyncLog();
|
||||
|
||||
try {
|
||||
// Tandai sinkronisasi dimulai
|
||||
$this->updateSyncLogStart();
|
||||
|
||||
// Proses sinkronisasi
|
||||
$this->syncAtmCards();
|
||||
|
||||
// Jadwalkan job untuk update branch dan currency
|
||||
$this->scheduleUpdateBranchCurrencyJobs();
|
||||
|
||||
// Tandai sinkronisasi selesai
|
||||
$this->updateSyncLogSuccess();
|
||||
} catch (Exception $e) {
|
||||
// Catat error
|
||||
$this->updateSyncLogFailed($e->getMessage());
|
||||
|
||||
Log::error('BiayaKartu: Sinkronisasi gagal: ' . $e->getMessage(), [
|
||||
'file' => $e->getFile(),
|
||||
'line' => $e->getLine(),
|
||||
'periode' => $this->periode
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Inisialisasi atau ambil log sinkronisasi
|
||||
*/
|
||||
private function initSyncLog(): void
|
||||
{
|
||||
// Cek apakah sudah ada log untuk periode ini
|
||||
$this->syncLog = KartuSyncLog::where('periode', $this->periode)->first();
|
||||
|
||||
// Jika belum ada, buat log baru
|
||||
if (!$this->syncLog) {
|
||||
$this->syncLog = KartuSyncLog::create([
|
||||
'periode' => $this->periode,
|
||||
'is_sync' => false,
|
||||
'is_csv' => false,
|
||||
'is_ftp' => false,
|
||||
'total_records' => 0,
|
||||
'success_records' => 0,
|
||||
'failed_records' => 0,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update log sinkronisasi saat dimulai
|
||||
*/
|
||||
private function updateSyncLogStart(): void
|
||||
{
|
||||
$this->syncLog->update([
|
||||
'sync_notes' => 'Sinkronisasi data kartu untuk periode ' . $this->periode . ' dimulai',
|
||||
'is_sync' => false,
|
||||
'sync_at' => null,
|
||||
]);
|
||||
|
||||
Log::info('Memulai sinkronisasi data kartu untuk periode ' . $this->periode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update log sinkronisasi saat berhasil
|
||||
*/
|
||||
private function updateSyncLogSuccess(): void
|
||||
{
|
||||
$this->syncLog->update([
|
||||
'is_sync' => true,
|
||||
'sync_at' => Carbon::now(),
|
||||
'sync_notes' => 'Sinkronisasi selesai: ' . $this->totalRecords . ' total, '
|
||||
. $this->successRecords . ' berhasil, '
|
||||
. $this->failedRecords . ' gagal',
|
||||
'total_records' => $this->totalRecords,
|
||||
'success_records' => $this->successRecords,
|
||||
'failed_records' => $this->failedRecords,
|
||||
]);
|
||||
|
||||
Log::info('Sinkronisasi kartu ATM berhasil diselesaikan untuk periode ' . $this->periode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update log sinkronisasi saat gagal
|
||||
*/
|
||||
private function updateSyncLogFailed($errorMessage): void
|
||||
{
|
||||
$this->syncLog->update([
|
||||
'is_sync' => false,
|
||||
'sync_notes' => 'Sinkronisasi gagal: ' . $errorMessage,
|
||||
'total_records' => $this->totalRecords,
|
||||
'success_records' => $this->successRecords,
|
||||
'failed_records' => $this->failedRecords,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -50,16 +169,32 @@ class BiayaKartu implements ShouldQueue
|
||||
$cards = $this->fetchCardBatch($offset);
|
||||
$this->processCardBatch($cards);
|
||||
|
||||
// Update total records
|
||||
$this->totalRecords += count($cards);
|
||||
|
||||
// Update progress di log
|
||||
if ($offset % 5000 === 0) {
|
||||
$this->syncLog->update([
|
||||
'sync_notes' => 'Sinkronisasi dalam proses: ' . $this->totalRecords . ' data diproses, '
|
||||
. $this->successRecords . ' berhasil, '
|
||||
. $this->failedRecords . ' gagal',
|
||||
'total_records' => $this->totalRecords,
|
||||
'success_records' => $this->successRecords,
|
||||
'failed_records' => $this->failedRecords,
|
||||
]);
|
||||
}
|
||||
|
||||
$hasMoreRecords = count($cards) === self::BATCH_SIZE;
|
||||
$offset += self::BATCH_SIZE;
|
||||
}
|
||||
|
||||
Log::info('Sinkronisasi kartu ATM berhasil diselesaikan');
|
||||
} catch (Exception $e) {
|
||||
Log::error('BiayaKartu: syncAtmCards: ' . $e->getMessage(), [
|
||||
'file' => $e->getFile(),
|
||||
'line' => $e->getLine()
|
||||
'line' => $e->getLine(),
|
||||
'periode' => $this->periode
|
||||
]);
|
||||
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,7 +233,13 @@ class BiayaKartu implements ShouldQueue
|
||||
// Perbarui data kartu dasar
|
||||
$cardData = $this->getCardBaseData($card);
|
||||
Atmcard::updateOrCreate(['crdno' => $card->crdno], $cardData);
|
||||
|
||||
// Tambah hitungan sukses
|
||||
$this->successRecords++;
|
||||
} catch (Exception $e) {
|
||||
// Tambah hitungan gagal
|
||||
$this->failedRecords++;
|
||||
|
||||
Log::warning("Gagal memproses kartu {$card->crdno}: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
@@ -127,9 +268,14 @@ class BiayaKartu implements ShouldQueue
|
||||
$totalCards = $cards->count();
|
||||
Log::info("Menjadwalkan {$totalCards} job pembaruan branch dan currency");
|
||||
|
||||
// Update log
|
||||
$this->syncLog->update([
|
||||
'sync_notes' => $this->syncLog->sync_notes . "\nMenjadwalkan {$totalCards} job pembaruan branch dan currency"
|
||||
]);
|
||||
|
||||
foreach ($cards as $card) {
|
||||
// Jadwalkan job dengan delay untuk menghindari terlalu banyak request bersamaan
|
||||
UpdateAtmCardBranchCurrencyJob::dispatch($card)
|
||||
UpdateAtmCardBranchCurrencyJob::dispatch($card, $this->syncLog->id)
|
||||
->delay(now()->addSeconds(rand(1, 300))); // Random delay antara 1-300 detik
|
||||
}
|
||||
|
||||
@@ -139,6 +285,10 @@ class BiayaKartu implements ShouldQueue
|
||||
'file' => $e->getFile(),
|
||||
'line' => $e->getLine()
|
||||
]);
|
||||
|
||||
$this->syncLog->update([
|
||||
'sync_notes' => $this->syncLog->sync_notes . "\nError: Gagal menjadwalkan job pembaruan branch dan currency: " . $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user