Files
speech2text/app/Jobs/OllamaPostProcessJob.php
2026-06-02 17:35:45 +08:00

77 lines
2.4 KiB
PHP

<?php
namespace App\Jobs;
use App\Models\AuditLog;
use App\Models\TranscriptVersion;
use App\Models\TranscriptionProject;
use App\Services\OllamaService;
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;
class OllamaPostProcessJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public int $timeout = 180;
public int $tries = 2;
public int $backoff = 15;
public function __construct(private readonly TranscriptionProject $project) {}
public function handle(OllamaService $ollama): void
{
$project = $this->project;
// Reload fresh — status may have changed between dispatch and execution
$project->refresh();
if (! $project->isCompleted() || ! $project->transcript_text) {
return;
}
if (! $ollama->isAvailable()) {
Log::info("OllamaPostProcessJob: Ollama tidak tersedia, skip projek #{$project->id}");
return;
}
$cleaned = $ollama->cleanTranscript($project->transcript_text, $project->language);
if ($cleaned === null) {
Log::warning("OllamaPostProcessJob: hasil null untuk projek #{$project->id}");
return;
}
$oldText = $project->transcript_text;
$nextVersion = ($project->transcriptVersions()->max('version_number') ?? 0) + 1;
TranscriptVersion::create([
'project_id' => $project->id,
'edited_by' => $project->owner_user_id,
'version_number' => $nextVersion,
'old_text' => $oldText,
'new_text' => $cleaned,
'change_summary' => 'Dibersihkan oleh Ollama',
'created_at' => now(),
]);
$project->update(['transcript_text' => $cleaned]);
AuditLog::create([
'actor_user_id' => null,
'actor_role' => 'system',
'action' => 'transcript_postprocessed',
'project_id' => $project->id,
'new_values' => [
'project_uuid' => $project->uuid,
'model' => config('speech2text.ollama.model'),
],
'created_at' => now(),
]);
}
}