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,21 +1,21 @@
<?php
namespace Modules\Webstatement\Jobs;
namespace Modules\Webstatement\Jobs;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
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;
use Carbon\Carbon;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Modules\Webstatement\Models\Atmcard;
use Modules\Webstatement\Models\KartuSyncLog;
class BiayaKartu implements ShouldQueue
{
class BiayaKartu implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
@@ -54,7 +54,8 @@ class BiayaKartu implements ShouldQueue
/**
* Execute the job.
*/
public function handle(): void
public function handle()
: void
{
set_time_limit(self::MAX_EXECUTION_TIME);
@@ -88,7 +89,8 @@ class BiayaKartu implements ShouldQueue
/**
* Inisialisasi atau ambil log sinkronisasi
*/
private function initSyncLog(): void
private function initSyncLog()
: void
{
// Cek apakah sudah ada log untuk periode ini
$this->syncLog = KartuSyncLog::where('periode', $this->periode)->first();
@@ -110,7 +112,8 @@ class BiayaKartu implements ShouldQueue
/**
* Update log sinkronisasi saat dimulai
*/
private function updateSyncLogStart(): void
private function updateSyncLogStart()
: void
{
$this->syncLog->update([
'sync_notes' => 'Sinkronisasi data kartu untuk periode ' . $this->periode . ' dimulai',
@@ -121,45 +124,13 @@ class BiayaKartu implements ShouldQueue
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,
]);
}
/**
* Synchronize ATM cards data from Oracle database
*
* @return void
*/
private function syncAtmCards(): void
private function syncAtmCards()
: void
{
try {
$offset = 0;
@@ -209,8 +180,8 @@ class BiayaKartu implements ShouldQueue
{
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')
->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)
@@ -226,7 +197,8 @@ class BiayaKartu implements ShouldQueue
*
* @return void
*/
private function processCardBatch($cards): void
private function processCardBatch($cards)
: void
{
foreach ($cards as $card) {
try {
@@ -245,53 +217,6 @@ class BiayaKartu implements ShouldQueue
}
}
/**
* 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) {
// 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()
]);
$this->syncLog->update([
'sync_notes' => $this->syncLog->sync_notes . "\nError: Gagal menjadwalkan job pembaruan branch dan currency: " . $e->getMessage()
]);
}
}
/**
* Get base data for card update
*
@@ -299,7 +224,8 @@ class BiayaKartu implements ShouldQueue
*
* @return array
*/
private function getCardBaseData(object $card): array
private function getCardBaseData(object $card)
: array
{
return [
'accflag' => $card->accflag,
@@ -320,4 +246,88 @@ class BiayaKartu implements ShouldQueue
'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)));
}
}
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()
]);
$this->syncLog->update([
'sync_notes' => $this->syncLog->sync_notes . "\nError: Gagal menjadwalkan job pembaruan branch dan currency: " . $e->getMessage()
]);
}
}
/**
* 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,
]);
}
}