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:
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace Modules\Webstatement\Jobs;
|
namespace Modules\Webstatement\Jobs;
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Illuminate\Bus\Queueable;
|
use Illuminate\Bus\Queueable;
|
||||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
@@ -10,7 +11,6 @@ use Illuminate\Queue\InteractsWithQueue;
|
|||||||
use Illuminate\Queue\SerializesModels;
|
use Illuminate\Queue\SerializesModels;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
use Carbon\Carbon;
|
|
||||||
use Modules\Webstatement\Models\Atmcard;
|
use Modules\Webstatement\Models\Atmcard;
|
||||||
use Modules\Webstatement\Models\KartuSyncLog;
|
use Modules\Webstatement\Models\KartuSyncLog;
|
||||||
|
|
||||||
@@ -54,7 +54,8 @@ class BiayaKartu implements ShouldQueue
|
|||||||
/**
|
/**
|
||||||
* Execute the job.
|
* Execute the job.
|
||||||
*/
|
*/
|
||||||
public function handle(): void
|
public function handle()
|
||||||
|
: void
|
||||||
{
|
{
|
||||||
set_time_limit(self::MAX_EXECUTION_TIME);
|
set_time_limit(self::MAX_EXECUTION_TIME);
|
||||||
|
|
||||||
@@ -88,7 +89,8 @@ class BiayaKartu implements ShouldQueue
|
|||||||
/**
|
/**
|
||||||
* Inisialisasi atau ambil log sinkronisasi
|
* Inisialisasi atau ambil log sinkronisasi
|
||||||
*/
|
*/
|
||||||
private function initSyncLog(): void
|
private function initSyncLog()
|
||||||
|
: void
|
||||||
{
|
{
|
||||||
// Cek apakah sudah ada log untuk periode ini
|
// Cek apakah sudah ada log untuk periode ini
|
||||||
$this->syncLog = KartuSyncLog::where('periode', $this->periode)->first();
|
$this->syncLog = KartuSyncLog::where('periode', $this->periode)->first();
|
||||||
@@ -110,7 +112,8 @@ class BiayaKartu implements ShouldQueue
|
|||||||
/**
|
/**
|
||||||
* Update log sinkronisasi saat dimulai
|
* Update log sinkronisasi saat dimulai
|
||||||
*/
|
*/
|
||||||
private function updateSyncLogStart(): void
|
private function updateSyncLogStart()
|
||||||
|
: void
|
||||||
{
|
{
|
||||||
$this->syncLog->update([
|
$this->syncLog->update([
|
||||||
'sync_notes' => 'Sinkronisasi data kartu untuk periode ' . $this->periode . ' dimulai',
|
'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);
|
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
|
* Synchronize ATM cards data from Oracle database
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
private function syncAtmCards(): void
|
private function syncAtmCards()
|
||||||
|
: void
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$offset = 0;
|
$offset = 0;
|
||||||
@@ -209,8 +180,8 @@ class BiayaKartu implements ShouldQueue
|
|||||||
{
|
{
|
||||||
return DB::connection('oracle')
|
return DB::connection('oracle')
|
||||||
->table(self::DB_TABLE)
|
->table(self::DB_TABLE)
|
||||||
->select('CRDNO', 'ACCFLAG', 'CRACC1', 'CRACC2', 'CRACC3', 'CRACC4', 'CRACC5', 'CRACCNAM1', 'CRACCNAM2', 'CRACCNAM3', 'CRACCNAM4', 'CRACCNAM5', 'CRSTS','CTTYPE','CTDESC','CRDATE','LAST_UPDATE')
|
->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_VCARD.CTTYPE')
|
->join('IST77.CMS_VCARDTYP', 'IST77.CMS_VCARD.CRTYPE', '=', 'IST77.CMS_VCARDTYP.CTTYPE')
|
||||||
->where('crsts', 1)
|
->where('crsts', 1)
|
||||||
->whereNotNull('ACCFLAG')
|
->whereNotNull('ACCFLAG')
|
||||||
->where('ACCFLAG', '>', 0)
|
->where('ACCFLAG', '>', 0)
|
||||||
@@ -226,7 +197,8 @@ class BiayaKartu implements ShouldQueue
|
|||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
private function processCardBatch($cards): void
|
private function processCardBatch($cards)
|
||||||
|
: void
|
||||||
{
|
{
|
||||||
foreach ($cards as $card) {
|
foreach ($cards as $card) {
|
||||||
try {
|
try {
|
||||||
@@ -245,12 +217,43 @@ class BiayaKartu implements ShouldQueue
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
* Schedule update branch and currency jobs for cards that need update
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
private function scheduleUpdateBranchCurrencyJobs(): void
|
private function scheduleUpdateBranchCurrencyJobs()
|
||||||
|
: void
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
// Ambil semua kartu yang perlu diperbarui branch dan currency
|
// Ambil semua kartu yang perlu diperbarui branch dan currency
|
||||||
@@ -274,9 +277,10 @@ class BiayaKartu implements ShouldQueue
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
foreach ($cards as $card) {
|
foreach ($cards as $card) {
|
||||||
// Jadwalkan job dengan delay untuk menghindari terlalu banyak request bersamaan
|
foreach ($cards as $index => $card) {
|
||||||
UpdateAtmCardBranchCurrencyJob::dispatch($card, $this->syncLog->id)
|
UpdateAtmCardBranchCurrencyJob::dispatch($card, $this->syncLog->id)
|
||||||
->delay(now()->addSeconds(rand(1, 300))); // Random delay antara 1-300 detik
|
->delay(now()->addSeconds(1 * ($index + 1)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Log::info('Semua job pembaruan branch dan currency telah dijadwalkan');
|
Log::info('Semua job pembaruan branch dan currency telah dijadwalkan');
|
||||||
@@ -293,31 +297,37 @@ class BiayaKartu implements ShouldQueue
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get base data for card update
|
* Update log sinkronisasi saat berhasil
|
||||||
*
|
|
||||||
* @param object $card
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
*/
|
||||||
private function getCardBaseData(object $card): array
|
private function updateSyncLogSuccess()
|
||||||
|
: void
|
||||||
{
|
{
|
||||||
return [
|
$this->syncLog->update([
|
||||||
'accflag' => $card->accflag,
|
'is_sync' => true,
|
||||||
'cracc1' => $card->cracc1,
|
'sync_at' => Carbon::now(),
|
||||||
'cracc2' => $card->cracc2,
|
'sync_notes' => 'Sinkronisasi selesai: ' . $this->totalRecords . ' total, '
|
||||||
'cracc3' => $card->cracc3,
|
. $this->successRecords . ' berhasil, '
|
||||||
'cracc4' => $card->cracc4,
|
. $this->failedRecords . ' gagal',
|
||||||
'cracc5' => $card->cracc5,
|
'total_records' => $this->totalRecords,
|
||||||
'craccnam1' => $card->craccnam1,
|
'success_records' => $this->successRecords,
|
||||||
'craccnam2' => $card->craccnam2,
|
'failed_records' => $this->failedRecords,
|
||||||
'craccnam3' => $card->craccnam3,
|
]);
|
||||||
'craccnam4' => $card->craccnam4,
|
|
||||||
'craccnam5' => $card->craccnam5,
|
Log::info('Sinkronisasi kartu ATM berhasil diselesaikan untuk periode ' . $this->periode);
|
||||||
'crsts' => $card->crsts,
|
}
|
||||||
'cttype' => $card->cttype,
|
|
||||||
'ctdesc' => $card->ctdesc,
|
/**
|
||||||
'crdate' => $card->crdate,
|
* Update log sinkronisasi saat gagal
|
||||||
'last_update' => $card->lastupdate,
|
*/
|
||||||
];
|
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,
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user