Files
ChatbotAI/app/Jobs/ProcessUploadedDocumentJob.php
2026-05-18 08:56:23 +08:00

66 lines
2.1 KiB
PHP

<?php
namespace App\Jobs;
use App\Models\DocumentVersion;
use App\Services\KnowledgeBase\IngestionService;
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\Log;
use Throwable;
/**
* ProcessUploadedDocumentJob
*
* Job utama untuk proses dokumen selepas upload.
* Melaksanakan: extract PDF → chunk → embed → sync Qdrant
*
* Dihantar ke queue supaya upload tidak timeout.
* Retry: 2 kali jika gagal, dengan 60s delay.
*/
class ProcessUploadedDocumentJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public int $tries = 2;
public int $backoff = 60; // saat antara retry
public int $timeout = 600; // 10 minit — PDF besar mungkin ambil masa
public function __construct(
private readonly int $documentVersionId
) {
$this->onQueue(config('knowledgebase.queue.ingestion', 'default'));
}
public function handle(IngestionService $ingestionService): void
{
$version = DocumentVersion::with(['document.category'])->findOrFail($this->documentVersionId);
Log::info("ProcessUploadedDocumentJob mula untuk version {$this->documentVersionId}");
$ingestionService->processDocumentVersion($version);
Log::info("ProcessUploadedDocumentJob selesai untuk version {$this->documentVersionId}");
}
public function failed(Throwable $exception): void
{
Log::error("ProcessUploadedDocumentJob GAGAL untuk version {$this->documentVersionId}", [
'error' => $exception->getMessage(),
'trace' => $exception->getTraceAsString(),
]);
// Kemaskini status ke failed dalam database
$version = DocumentVersion::find($this->documentVersionId);
if ($version) {
$version->updateStatus(
DocumentVersion::STATUS_FAILED,
'Job gagal: ' . $exception->getMessage()
);
}
}
}