81 lines
2.5 KiB
PHP
81 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Models\DocumentVersion;
|
|
use App\Services\KnowledgeBase\AuditService;
|
|
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;
|
|
|
|
/**
|
|
* ReindexDocumentJob
|
|
*
|
|
* Reindex (reprocess embedding) untuk document version tertentu.
|
|
* Berguna bila:
|
|
* - model embedding ditukar
|
|
* - chunking strategy dikemaskini
|
|
* - Qdrant collection diset semula
|
|
*
|
|
* Proses: Deactivate chunk lama → Delete chunk lama → Re-chunk → Re-embed → Qdrant
|
|
*/
|
|
class ReindexDocumentJob implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
public int $tries = 2;
|
|
public int $backoff = 60;
|
|
public int $timeout = 600;
|
|
|
|
public function __construct(
|
|
private readonly int $documentVersionId
|
|
) {
|
|
$this->onQueue(config('knowledgebase.queue.ingestion', 'default'));
|
|
}
|
|
|
|
public function handle(IngestionService $ingestionService, AuditService $auditService): void
|
|
{
|
|
$version = DocumentVersion::with(['document.category'])->findOrFail($this->documentVersionId);
|
|
|
|
Log::info("ReindexDocumentJob mula untuk version {$this->documentVersionId}");
|
|
|
|
// Deactivate point lama dalam Qdrant
|
|
$ingestionService->deactivateVersionInQdrant($version);
|
|
|
|
// Padam chunk lama dari MySQL untuk reprocess
|
|
$version->chunks()->delete();
|
|
|
|
// Reset status dan mula proses semula
|
|
$version->update([
|
|
'processing_status' => DocumentVersion::STATUS_PENDING,
|
|
'processing_error' => null,
|
|
'processing_completed_at' => null,
|
|
]);
|
|
|
|
// Reprocess
|
|
$ingestionService->processDocumentVersion($version);
|
|
|
|
$auditService->documentReindexed($version->document, $version);
|
|
|
|
Log::info("ReindexDocumentJob selesai untuk version {$this->documentVersionId}");
|
|
}
|
|
|
|
public function failed(Throwable $exception): void
|
|
{
|
|
Log::error("ReindexDocumentJob GAGAL untuk version {$this->documentVersionId}", [
|
|
'error' => $exception->getMessage(),
|
|
]);
|
|
|
|
$version = DocumentVersion::find($this->documentVersionId);
|
|
$version?->updateStatus(
|
|
DocumentVersion::STATUS_FAILED,
|
|
'Reindex gagal: ' . $exception->getMessage()
|
|
);
|
|
}
|
|
}
|