feat(webstatement): refactor job sinkronisasi BiayaKartu

- Mengubah pelaksanaan scheduling delayed job untuk UpdateAtmCardBranchCurrencyJob.
- Memperbarui logging untuk proses sinkronisasi kartu ATM.

Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
This commit is contained in:
Daeng Deni Mardaeni
2025-05-11 14:46:16 +07:00
parent f899fed8a7
commit 204675716b

View File

@@ -1,323 +1,333 @@
<?php <?php
namespace Modules\Webstatement\Jobs; namespace Modules\Webstatement\Jobs;
use Exception; use Carbon\Carbon;
use Illuminate\Bus\Queueable; use Exception;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Bus\Queueable;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\DB; use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\DB;
use Carbon\Carbon; use Illuminate\Support\Facades\Log;
use Modules\Webstatement\Models\Atmcard; use Modules\Webstatement\Models\Atmcard;
use Modules\Webstatement\Models\KartuSyncLog; use Modules\Webstatement\Models\KartuSyncLog;
class BiayaKartu implements ShouldQueue class BiayaKartu implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Database constants
*/
private const DB_TABLE = 'IST77.CMS_VCARD';
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 use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
$this->periode = $periode ?? Carbon::now()->format('Y-m');
}
/** /**
* Execute the job. * Database constants
*/ */
public function handle(): void private const DB_TABLE = 'IST77.CMS_VCARD';
{ private const BATCH_SIZE = 1000;
set_time_limit(self::MAX_EXECUTION_TIME); private const MAX_EXECUTION_TIME = 86400; // 24 jam dalam detik
// Inisialisasi atau ambil log sinkronisasi /**
$this->initSyncLog(); * Log model untuk menyimpan status sinkronisasi
*/
protected $syncLog;
try { /**
// Tandai sinkronisasi dimulai * Periode yang sedang disinkronkan (YYYY-MM)
$this->updateSyncLogStart(); */
protected $periode;
// Proses sinkronisasi /**
$this->syncAtmCards(); * Statistik sinkronisasi
*/
protected $totalRecords = 0;
protected $successRecords = 0;
protected $failedRecords = 0;
// Jadwalkan job untuk update branch dan currency /**
$this->scheduleUpdateBranchCurrencyJobs(); * Create a new job instance.
*/
// Tandai sinkronisasi selesai public function __construct($periode = null)
$this->updateSyncLogSuccess(); {
} catch (Exception $e) { // Jika periode tidak diberikan, gunakan bulan saat ini
// Catat error $this->periode = $periode ?? Carbon::now()->format('Y-m');
$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 * Execute the job.
*/ */
private function initSyncLog(): void public function handle()
{ : void
// Cek apakah sudah ada log untuk periode ini {
$this->syncLog = KartuSyncLog::where('periode', $this->periode)->first(); set_time_limit(self::MAX_EXECUTION_TIME);
// Jika belum ada, buat log baru // Inisialisasi atau ambil log sinkronisasi
if (!$this->syncLog) { $this->initSyncLog();
$this->syncLog = KartuSyncLog::create([
'periode' => $this->periode, try {
'is_sync' => false, // Tandai sinkronisasi dimulai
'is_csv' => false, $this->updateSyncLogStart();
'is_ftp' => false,
'total_records' => 0, // Proses sinkronisasi
'success_records' => 0, $this->syncAtmCards();
'failed_records' => 0,
]); // 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
]);
}
} }
}
/** /**
* Update log sinkronisasi saat dimulai * Inisialisasi atau ambil log sinkronisasi
*/ */
private function updateSyncLogStart(): void private function initSyncLog()
{ : void
$this->syncLog->update([ {
'sync_notes' => 'Sinkronisasi data kartu untuk periode ' . $this->periode . ' dimulai', // Cek apakah sudah ada log untuk periode ini
'is_sync' => false, $this->syncLog = KartuSyncLog::where('periode', $this->periode)->first();
'sync_at' => null,
]);
Log::info('Memulai sinkronisasi data kartu untuk periode ' . $this->periode); // 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 berhasil * Update log sinkronisasi saat dimulai
*/ */
private function updateSyncLogSuccess(): void private function updateSyncLogStart()
{ : void
$this->syncLog->update([ {
'is_sync' => true, $this->syncLog->update([
'sync_at' => Carbon::now(), 'sync_notes' => 'Sinkronisasi data kartu untuk periode ' . $this->periode . ' dimulai',
'sync_notes' => 'Sinkronisasi selesai: ' . $this->totalRecords . ' total, ' 'is_sync' => false,
. $this->successRecords . ' berhasil, ' 'sync_at' => null,
. $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); Log::info('Memulai sinkronisasi data kartu untuk periode ' . $this->periode);
} }
/** /**
* Update log sinkronisasi saat gagal * Synchronize ATM cards data from Oracle database
*/ *
private function updateSyncLogFailed($errorMessage): void * @return void
{ */
$this->syncLog->update([ private function syncAtmCards()
'is_sync' => false, : void
'sync_notes' => 'Sinkronisasi gagal: ' . $errorMessage, {
'total_records' => $this->totalRecords, try {
'success_records' => $this->successRecords, $offset = 0;
'failed_records' => $this->failedRecords, $hasMoreRecords = true;
]);
}
/** while ($hasMoreRecords) {
* Synchronize ATM cards data from Oracle database $cards = $this->fetchCardBatch($offset);
* $this->processCardBatch($cards);
* @return void
*/
private function syncAtmCards(): void
{
try {
$offset = 0;
$hasMoreRecords = true;
while ($hasMoreRecords) { // Update total records
$cards = $this->fetchCardBatch($offset); $this->totalRecords += count($cards);
$this->processCardBatch($cards);
// Update total records // Update progress di log
$this->totalRecords += count($cards); 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,
]);
}
// Update progress di log $hasMoreRecords = count($cards) === self::BATCH_SIZE;
if ($offset % 5000 === 0) { $offset += self::BATCH_SIZE;
$this->syncLog->update([ }
'sync_notes' => 'Sinkronisasi dalam proses: ' . $this->totalRecords . ' data diproses, ' } catch (Exception $e) {
. $this->successRecords . ' berhasil, ' Log::error('BiayaKartu: syncAtmCards: ' . $e->getMessage(), [
. $this->failedRecords . ' gagal', 'file' => $e->getFile(),
'total_records' => $this->totalRecords, 'line' => $e->getLine(),
'success_records' => $this->successRecords, 'periode' => $this->periode
'failed_records' => $this->failedRecords, ]);
]);
throw $e;
}
}
/**
* Fetch a batch of ATM cards from the database
*
* @param int $offset
*
* @return \Illuminate\Support\Collection
*/
private function fetchCardBatch(int $offset)
{
return DB::connection('oracle')
->table(self::DB_TABLE)
->select('CRDNO', 'ACCFLAG', 'CRACC1', 'CRACC2', 'CRACC3', 'CRACC4', 'CRACC5', 'CRACCNAM1', 'CRACCNAM2', 'CRACCNAM3', 'CRACCNAM4', 'CRACCNAM5', 'CRSTS', 'CTTYPE', 'CTDESC', 'CRDATE', 'LASTUPDATE')
->join('IST77.CMS_VCARDTYP', 'IST77.CMS_VCARD.CRTYPE', '=', 'IST77.CMS_VCARDTYP.CTTYPE')
->where('crsts', 1)
->whereNotNull('ACCFLAG')
->where('ACCFLAG', '>', 0)
->skip($offset)
->take(self::BATCH_SIZE)
->get();
}
/**
* Process a batch of ATM cards
*
* @param \Illuminate\Support\Collection $cards
*
* @return void
*/
private function processCardBatch($cards)
: void
{
foreach ($cards as $card) {
try {
// 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());
}
}
}
/**
* Get base data for card update
*
* @param object $card
*
* @return array
*/
private function getCardBaseData(object $card)
: array
{
return [
'accflag' => $card->accflag,
'cracc1' => $card->cracc1,
'cracc2' => $card->cracc2,
'cracc3' => $card->cracc3,
'cracc4' => $card->cracc4,
'cracc5' => $card->cracc5,
'craccnam1' => $card->craccnam1,
'craccnam2' => $card->craccnam2,
'craccnam3' => $card->craccnam3,
'craccnam4' => $card->craccnam4,
'craccnam5' => $card->craccnam5,
'crsts' => $card->crsts,
'cttype' => $card->cttype,
'ctdesc' => $card->ctdesc,
'crdate' => $card->crdate,
'last_update' => $card->lastupdate,
];
}
/**
* Schedule update branch and currency jobs for cards that need update
*
* @return void
*/
private function scheduleUpdateBranchCurrencyJobs()
: void
{
try {
// Ambil semua kartu yang perlu diperbarui branch dan currency
$cards = Atmcard::where('crsts', 1)
->whereNotNull('accflag')
->where('accflag', '!=', '')
->where(function ($query) {
$query->whereNull('branch')
->orWhere('branch', '')
->orWhereNull('currency')
->orWhere('currency', '');
})
->get();
$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) {
foreach ($cards as $index => $card) {
UpdateAtmCardBranchCurrencyJob::dispatch($card, $this->syncLog->id)
->delay(now()->addSeconds(1 * ($index + 1)));
}
} }
$hasMoreRecords = count($cards) === self::BATCH_SIZE; Log::info('Semua job pembaruan branch dan currency telah dijadwalkan');
$offset += self::BATCH_SIZE;
}
} catch (Exception $e) {
Log::error('BiayaKartu: syncAtmCards: ' . $e->getMessage(), [
'file' => $e->getFile(),
'line' => $e->getLine(),
'periode' => $this->periode
]);
throw $e;
}
}
/**
* Fetch a batch of ATM cards from the database
*
* @param int $offset
*
* @return \Illuminate\Support\Collection
*/
private function fetchCardBatch(int $offset)
{
return DB::connection('oracle')
->table(self::DB_TABLE)
->select('CRDNO', 'ACCFLAG', 'CRACC1', 'CRACC2', 'CRACC3', 'CRACC4', 'CRACC5', 'CRACCNAM1', 'CRACCNAM2', 'CRACCNAM3', 'CRACCNAM4', 'CRACCNAM5', 'CRSTS','CTTYPE','CTDESC','CRDATE','LAST_UPDATE')
->join('IST77.CMS_VCARDTYP', 'IST77.CMS_VCARD.CRTYPE', '=','IST77.CMS_VCARD.CTTYPE')
->where('crsts', 1)
->whereNotNull('ACCFLAG')
->where('ACCFLAG', '>', 0)
->skip($offset)
->take(self::BATCH_SIZE)
->get();
}
/**
* Process a batch of ATM cards
*
* @param \Illuminate\Support\Collection $cards
*
* @return void
*/
private function processCardBatch($cards): void
{
foreach ($cards as $card) {
try {
// Perbarui data kartu dasar
$cardData = $this->getCardBaseData($card);
Atmcard::updateOrCreate(['crdno' => $card->crdno], $cardData);
// Tambah hitungan sukses
$this->successRecords++;
} catch (Exception $e) { } catch (Exception $e) {
// Tambah hitungan gagal Log::error('Gagal menjadwalkan job pembaruan branch dan currency: ' . $e->getMessage(), [
$this->failedRecords++; 'file' => $e->getFile(),
'line' => $e->getLine()
]);
Log::warning("Gagal memproses kartu {$card->crdno}: " . $e->getMessage()); $this->syncLog->update([
'sync_notes' => $this->syncLog->sync_notes . "\nError: Gagal menjadwalkan job pembaruan branch dan currency: " . $e->getMessage()
]);
} }
} }
}
/** /**
* Schedule update branch and currency jobs for cards that need update * Update log sinkronisasi saat berhasil
* */
* @return void private function updateSyncLogSuccess()
*/ : void
private function scheduleUpdateBranchCurrencyJobs(): void {
{
try {
// Ambil semua kartu yang perlu diperbarui branch dan currency
$cards = Atmcard::where('crsts', 1)
->whereNotNull('accflag')
->where('accflag', '!=', '')
->where(function($query) {
$query->whereNull('branch')
->orWhere('branch', '')
->orWhereNull('currency')
->orWhere('currency', '');
})
->get();
$totalCards = $cards->count();
Log::info("Menjadwalkan {$totalCards} job pembaruan branch dan currency");
// Update log
$this->syncLog->update([ $this->syncLog->update([
'sync_notes' => $this->syncLog->sync_notes . "\nMenjadwalkan {$totalCards} job pembaruan branch dan currency" '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,
]); ]);
foreach ($cards as $card) { Log::info('Sinkronisasi kartu ATM berhasil diselesaikan untuk periode ' . $this->periode);
// Jadwalkan job dengan delay untuk menghindari terlalu banyak request bersamaan }
UpdateAtmCardBranchCurrencyJob::dispatch($card, $this->syncLog->id)
->delay(now()->addSeconds(rand(1, 300))); // Random delay antara 1-300 detik
}
Log::info('Semua job pembaruan branch dan currency telah dijadwalkan');
} catch (Exception $e) {
Log::error('Gagal menjadwalkan job pembaruan branch dan currency: ' . $e->getMessage(), [
'file' => $e->getFile(),
'line' => $e->getLine()
]);
/**
* Update log sinkronisasi saat gagal
*/
private function updateSyncLogFailed($errorMessage)
: void
{
$this->syncLog->update([ $this->syncLog->update([
'sync_notes' => $this->syncLog->sync_notes . "\nError: Gagal menjadwalkan job pembaruan branch dan currency: " . $e->getMessage() 'is_sync' => false,
'sync_notes' => 'Sinkronisasi gagal: ' . $errorMessage,
'total_records' => $this->totalRecords,
'success_records' => $this->successRecords,
'failed_records' => $this->failedRecords,
]); ]);
} }
} }
/**
* Get base data for card update
*
* @param object $card
*
* @return array
*/
private function getCardBaseData(object $card): array
{
return [
'accflag' => $card->accflag,
'cracc1' => $card->cracc1,
'cracc2' => $card->cracc2,
'cracc3' => $card->cracc3,
'cracc4' => $card->cracc4,
'cracc5' => $card->cracc5,
'craccnam1' => $card->craccnam1,
'craccnam2' => $card->craccnam2,
'craccnam3' => $card->craccnam3,
'craccnam4' => $card->craccnam4,
'craccnam5' => $card->craccnam5,
'crsts' => $card->crsts,
'cttype' => $card->cttype,
'ctdesc' => $card->ctdesc,
'crdate' => $card->crdate,
'last_update' => $card->lastupdate,
];
}
}