Membuat service layer yang menyediakan interface fleksibel untuk menjalankan cleanup data inspeksi secara async/sync dengan error handling dan logging komprehensif - Implementasi pattern service layer untuk separation of concerns - Method utama cleanupInspeksiData dengan parameter sync untuk fleksibilitas eksekusi - Method wrapper cleanupAsync() untuk eksekusi asynchronous default - Method wrapper cleanupSync() untuk eksekusi synchronous langsung - Error handling dengan try-catch dan re-throw exception untuk propagasi error - Logging informatif untuk setiap proses cleanup (mulai, selesai, error) - Integrasi dengan CleanupInspeksiDataJob untuk delegasi proses bisnis - Parameter lengkap: permohonanId, createdBy, dokumentId, dan sync mode - Dokumentasi PHPDoc yang komprehensif untuk setiap method - Menggunakan Laravel dispatch() untuk queue management yang optimal
83 lines
2.5 KiB
PHP
83 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace Modules\Lpj\Services;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
use Modules\Lpj\Jobs\CleanupInspeksiDataJob;
|
|
|
|
/**
|
|
* Service untuk membersihkan data inspeksi yang tidak memiliki dokument_id
|
|
*
|
|
* Class ini menyediakan method untuk menjalankan cleanup data inspeksi
|
|
* ketika ada data baru dengan dokument_id yang sama
|
|
*/
|
|
class InspeksiCleanupService
|
|
{
|
|
/**
|
|
* Dispatch job untuk cleanup data inspeksi
|
|
*
|
|
* @param int $permohonanId
|
|
* @param int $createdBy
|
|
* @param int|null $dokumentId
|
|
* @param bool $sync
|
|
* @return void
|
|
*/
|
|
public function cleanupInspeksiData(int $permohonanId, int $createdBy, ?int $dokumentId = null, bool $sync = false): void
|
|
{
|
|
Log::info('InspeksiCleanupService: Memulai cleanup data inspeksi', [
|
|
'permohonan_id' => $permohonanId,
|
|
'created_by' => $createdBy,
|
|
'dokument_id' => $dokumentId,
|
|
'sync' => $sync
|
|
]);
|
|
|
|
try {
|
|
$job = new CleanupInspeksiDataJob($permohonanId, $createdBy, $dokumentId);
|
|
|
|
if ($sync) {
|
|
// Jalankan secara synchronous (langsung)
|
|
$job->handle();
|
|
Log::info('InspeksiCleanupService: Cleanup selesai dijalankan secara sync');
|
|
} else {
|
|
// Dispatch ke queue
|
|
dispatch($job);
|
|
Log::info('InspeksiCleanupService: Cleanup job berhasil di-dispatch ke queue');
|
|
}
|
|
} catch (\Exception $e) {
|
|
Log::error('InspeksiCleanupService: Gagal menjalankan cleanup', [
|
|
'permohonan_id' => $permohonanId,
|
|
'created_by' => $createdBy,
|
|
'error' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString()
|
|
]);
|
|
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Dispatch job untuk cleanup data inspeksi secara async (default)
|
|
*
|
|
* @param int $permohonanId
|
|
* @param int $createdBy
|
|
* @param int|null $dokumentId
|
|
* @return void
|
|
*/
|
|
public function cleanupAsync(int $permohonanId, int $createdBy, ?int $dokumentId = null): void
|
|
{
|
|
$this->cleanupInspeksiData($permohonanId, $createdBy, $dokumentId, false);
|
|
}
|
|
|
|
/**
|
|
* Jalankan cleanup data inspeksi secara sync
|
|
*
|
|
* @param int $permohonanId
|
|
* @param int $createdBy
|
|
* @param int|null $dokumentId
|
|
* @return void
|
|
*/
|
|
public function cleanupSync(int $permohonanId, int $createdBy, ?int $dokumentId = null): void
|
|
{
|
|
$this->cleanupInspeksiData($permohonanId, $createdBy, $dokumentId, true);
|
|
}
|
|
} |