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:
Daeng Deni Mardaeni
2025-05-10 15:59:21 +07:00
parent e1a0167c78
commit 404085f2e7
2 changed files with 297 additions and 9 deletions

View File

@@ -2,6 +2,7 @@
namespace Modules\Webstatement\Jobs;
use Carbon\Carbon;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
@@ -12,20 +13,41 @@
use Illuminate\Support\Facades\Storage;
use Modules\Webstatement\Models\Atmcard;
use Modules\Webstatement\Models\JenisKartu;
use Modules\Webstatement\Models\KartuSyncLog;
use RuntimeException;
class GenerateBiayaKartuCsvJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
// Changed from const to property
private const MAX_EXECUTION_TIME = 3600; // 1 jam dalam detik
private $csvFilename;
/**
* Periode yang akan diproses (YYYY-MM)
*/
protected $periode;
/**
* ID log sinkronisasi
*/
protected $syncLogId;
/**
* Model log sinkronisasi
*/
protected $syncLog;
/**
* Create a new job instance.
*/
public function __construct()
{
$this->csvFilename = env('BIAYA_KARTU_CSV_FILENAME', 'biaya_kartu_atm.csv');
$this->periode = $periode ?? Carbon::now()->format('Y-m');
$this->syncLogId = KartuSyncLog::where('periode', $this->periode)->first();
}
/**
@@ -45,9 +67,22 @@
public function handle()
: void
{
set_time_limit(self::MAX_EXECUTION_TIME);
// Load log sinkronisasi
$this->syncLog = KartuSyncLog::findOrFail($this->syncLogId);
try {
// Update status CSV generation dimulai
$this->updateCsvLogStart();
// Generate CSV file
$result = $this->generateAtmCardCsv();
// Update status CSV generation berhasil
$this->updateCsvLogSuccess($result);
Log::info('Pembuatan dan upload file CSV biaya kartu ATM selesai', [
'file' => $result['localFilePath'],
'jumlah_kartu' => $result['recordCount'],
@@ -59,9 +94,12 @@
Log::warning('File CSV biaya kartu ATM tidak berhasil diunggah ke SFTP, tetapi tersedia secara lokal di: ' . $result['localFilePath']);
}
} catch (Exception $e) {
$this->updateCsvLogFailed($e->getMessage());
Log::error('Gagal membuat atau mengunggah file CSV biaya kartu ATM: ' . $e->getMessage(), [
'file' => $e->getFile(),
'line' => $e->getLine()
'line' => $e->getLine(),
'periode' => $this->periode
]);
throw $e;
}
@@ -222,6 +260,9 @@
: bool
{
try {
// Update status SFTP upload dimulai
$this->updateSftpLogStart();
// Ambil nama file dari path
$filename = basename($localFilePath);
@@ -243,19 +284,116 @@
$result = $disk->put($remoteFilePath, $fileContent);
if ($result) {
$this->updateSftpLogSuccess();
Log::info("File CSV biaya kartu ATM berhasil diunggah ke SFTP: {$remoteFilePath}");
return true;
} else {
$this->updateSftpLogFailed("Gagal mengunggah file CSV biaya kartu ATM ke SFTP: {$remoteFilePath}");
Log::error("Gagal mengunggah file CSV biaya kartu ATM ke SFTP: {$remoteFilePath}");
return false;
}
} catch (Exception $e) {
$this->updateSftpLogFailed($e->getMessage());
Log::error("Error saat mengunggah file ke SFTP: " . $e->getMessage(), [
'file' => $e->getFile(),
'line' => $e->getLine()
'line' => $e->getLine(),
'periode' => $this->periode
]);
return false;
}
}
/**
* Update log saat pembuatan CSV dimulai
*/
private function updateCsvLogStart(): void
{
$this->syncLog->update([
'csv_notes' => 'Pembuatan file CSV untuk periode ' . $this->periode . ' dimulai',
'is_csv' => false,
'csv_at' => null,
]);
Log::info('Memulai pembuatan file CSV untuk periode ' . $this->periode);
}
/**
* Update log saat pembuatan CSV berhasil
*/
private function updateCsvLogSuccess($result): void
{
// Get file info
$fileInfo = pathinfo($result['localFilePath']);
$fileName = $fileInfo['basename'];
$fileSize = Storage::size($result['localFilePath']);
$this->syncLog->update([
'is_csv' => true,
'csv_at' => Carbon::now(),
'csv_notes' => 'File CSV berhasil dibuat: ' . $fileName . ' (' . $this->formatSize($fileSize) . ')',
'file_path' => storage_path('app/' . $result['localFilePath']),
'file_name' => $fileName,
]);
Log::info('File CSV berhasil dibuat: ' . $result['localFilePath']);
}
/**
* Update log saat pembuatan CSV gagal
*/
private function updateCsvLogFailed($errorMessage): void
{
$this->syncLog->update([
'is_csv' => false,
'csv_notes' => 'Pembuatan file CSV gagal: ' . $errorMessage
]);
Log::error('Pembuatan file CSV gagal: ' . $errorMessage);
}
/**
* Update log saat upload SFTP dimulai
*/
private function updateSftpLogStart(): void
{
$this->syncLog->update([
'ftp_notes' => 'Upload ke SFTP untuk file periode ' . $this->periode . ' dimulai',
'is_ftp' => false,
'ftp_at' => null,
]);
Log::info('Memulai upload ke SFTP untuk periode ' . $this->periode);
}
/**
* Update log saat upload SFTP berhasil
*/
private function updateSftpLogSuccess(): void
{
$this->syncLog->update([
'is_ftp' => true,
'ftp_at' => Carbon::now(),
'ftp_notes' => 'File berhasil diupload ke SFTP',
'ftp_destination' => env('SFTP_KARTU_HOST', '/'),
]);
Log::info('File berhasil diupload ke SFTP untuk periode ' . $this->periode);
}
/**
* Update log saat upload SFTP gagal
*/
private function updateSftpLogFailed($errorMessage): void
{
$this->syncLog->update([
'is_ftp' => false,
'ftp_notes' => 'Upload ke SFTP gagal: ' . $errorMessage
]);
Log::error('Upload ke SFTP gagal: ' . $errorMessage);
}
}