Fix stuck transcriptions and make transcripts stoppable and searchable.

Raise queue retry_after above the job timeout, recover orphaned runs, allow stop/restart with any engine, and keep finished transcripts searchable in the recordings list.
This commit is contained in:
ben
2026-08-12 15:10:33 +02:00
parent eb0f019025
commit bc6cb5efa4
14 changed files with 626 additions and 64 deletions
@@ -0,0 +1,23 @@
<?php
namespace App\Http\Controllers;
use App\Models\Recording;
use Illuminate\Http\RedirectResponse;
class CancelTranscriptionController extends Controller
{
/**
* Stop an in-progress or queued transcription.
*/
public function __invoke(Recording $recording): RedirectResponse
{
if (! $recording->isTranscribing()) {
return back()->with('error', 'No transcription is currently running.');
}
$recording->cancelTranscription();
return back()->with('success', 'Transcription stopped.');
}
}
+13 -8
View File
@@ -17,21 +17,23 @@ class RecordingController extends Controller
*/
public function index(Request $request): View
{
Recording::query()
->whereIn('transcription_status', ['pending', 'processing'])
->orderBy('id')
->each(fn (Recording $recording) => $recording->recoverOrphanedTranscription());
$query = Recording::query()->latest();
if ($search = $request->string('q')->trim()->toString()) {
$query->where(function ($builder) use ($search) {
$builder->where('title', 'like', "%{$search}%")
->orWhere('artist', 'like', "%{$search}%")
->orWhere('album', 'like', "%{$search}%")
->orWhere('original_filename', 'like', "%{$search}%")
->orWhere('transcript', 'like', "%{$search}%");
});
$query->search($search);
}
$recordings = $query->paginate(20)->withQueryString();
return view('recordings.index', compact('recordings', 'search'));
return view('recordings.index', [
'recordings' => $recordings,
'search' => $search ?? '',
]);
}
/**
@@ -77,6 +79,9 @@ class RecordingController extends Controller
*/
public function show(Recording $recording): View
{
$recording->recoverOrphanedTranscription();
$recording->refresh();
return view('recordings.show', compact('recording'));
}
@@ -11,11 +11,14 @@ class TranscribeController extends Controller
{
/**
* Queue transcription for the recording with the chosen engine.
*
* Always allowed: stops any current run first, then starts the new engine.
*/
public function __invoke(TranscribeRecordingRequest $request, Recording $recording): RedirectResponse
{
if ($recording->transcription_status === 'processing') {
return back()->with('error', 'Transcription is already in progress.');
if ($recording->isTranscribing() || $recording->hasActiveTranscriptionJob()) {
$recording->cancelTranscription(silent: true);
$recording->refresh();
}
$driver = $request->validated('driver');
@@ -28,8 +31,8 @@ class TranscribeController extends Controller
'transcription_percent' => 5,
'transcription_started_at' => now(),
'transcription_error' => null,
'transcript' => null,
'transcribed_at' => null,
// Keep the previous transcript until a new run succeeds.
'transcribed_at' => $recording->transcribed_at,
]);
TranscribeRecording::dispatch($recording->fresh());
@@ -12,6 +12,12 @@ class TranscriptionStatusController extends Controller
*/
public function __invoke(Recording $recording): JsonResponse
{
return response()->json($recording->fresh()->transcriptionStatusPayload());
$recording = $recording->fresh();
if ($recording->recoverOrphanedTranscription()) {
$recording->refresh();
}
return response()->json($recording->transcriptionStatusPayload());
}
}
+66 -17
View File
@@ -13,17 +13,28 @@ class TranscribeRecording implements ShouldQueue
{
use Queueable;
/**
* The number of times the job may be attempted.
*/
public int $tries = 1;
/**
* The number of seconds the job can run before timing out.
*/
public int $timeout = 600;
public int $timeout;
/**
* ISO-8601 transcription_started_at this job owns (ignored after cancel/restart).
*/
public ?string $runStartedAt = null;
/**
* Create a new job instance.
*/
public function __construct(public Recording $recording)
{
//
$this->timeout = max(60, (int) config('ai.transcription_timeout', 600));
$this->runStartedAt = $recording->transcription_started_at?->toIso8601String();
}
/**
@@ -31,7 +42,21 @@ class TranscribeRecording implements ShouldQueue
*/
public function handle(TranscriptionService $transcription): void
{
$this->recording->refresh();
$recording = Recording::query()->find($this->recording->id);
if ($recording === null) {
return;
}
$this->recording = $recording;
if ($this->runStartedAt !== null && ! $this->recording->ownsTranscriptionRun($this->runStartedAt)) {
return;
}
if ($this->runStartedAt === null && ! $this->recording->isTranscribing()) {
return;
}
$this->recording->forceFill([
'transcription_status' => 'processing',
@@ -39,15 +64,27 @@ class TranscribeRecording implements ShouldQueue
'transcription_error' => null,
])->save();
$this->recording->reportProgress('Preparing audio file…', 15);
$this->runStartedAt ??= $this->recording->transcription_started_at?->toIso8601String();
$this->reportIfOwned('Preparing audio file…', 15);
try {
$text = $transcription->transcribe(
$this->recording,
fn (string $message, int $percent) => $this->recording->reportProgress($message, $percent),
function (string $message, int $percent): void {
$this->reportIfOwned($message, $percent);
},
);
$this->recording->reportProgress('Saving transcript…', 90);
if (! $this->recording->ownsTranscriptionRun($this->runStartedAt)) {
return;
}
$this->reportIfOwned('Saving transcript…', 90);
if (! $this->recording->ownsTranscriptionRun($this->runStartedAt)) {
return;
}
$this->recording->forceFill([
'transcript' => $text,
@@ -58,18 +95,17 @@ class TranscribeRecording implements ShouldQueue
'transcribed_at' => now(),
])->save();
} catch (Throwable $e) {
if (! $this->recording->ownsTranscriptionRun($this->runStartedAt)) {
return;
}
Log::error('Transcription failed', [
'recording_id' => $this->recording->id,
'driver' => $this->recording->transcription_driver,
'message' => $e->getMessage(),
]);
$this->recording->forceFill([
'transcription_status' => 'failed',
'transcription_progress' => 'Transcription failed',
'transcription_percent' => $this->recording->transcription_percent ?: 0,
'transcription_error' => $e->getMessage(),
])->save();
$this->recording->markTranscriptionFailed($e->getMessage());
throw $e;
}
@@ -80,10 +116,23 @@ class TranscribeRecording implements ShouldQueue
*/
public function failed(?Throwable $e): void
{
$this->recording->forceFill([
'transcription_status' => 'failed',
'transcription_progress' => 'Transcription failed',
'transcription_error' => $e?->getMessage() ?: 'Transcription stopped unexpectedly.',
])->save();
$recording = Recording::query()->find($this->recording->id);
if ($recording === null || ! $recording->ownsTranscriptionRun($this->runStartedAt)) {
return;
}
$recording->markTranscriptionFailed(
$e?->getMessage() ?: 'Transcription stopped unexpectedly.',
);
}
private function reportIfOwned(string $message, int $percent): void
{
if (! $this->recording->ownsTranscriptionRun($this->runStartedAt)) {
return;
}
$this->recording->reportProgress($message, $percent);
}
}
+236
View File
@@ -2,9 +2,17 @@
namespace App\Models;
use App\Jobs\TranscribeRecording;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Throwable;
class Recording extends Model
{
@@ -86,6 +94,233 @@ class Recording extends Model
return in_array($this->transcription_status, ['pending', 'processing'], true);
}
/**
* Search title, metadata, and stored transcript text.
*/
#[Scope]
protected function search(Builder $query, string $term): void
{
$like = '%'.$term.'%';
$query->where(function (Builder $builder) use ($like): void {
$builder->where('title', 'like', $like)
->orWhere('artist', 'like', $like)
->orWhere('album', 'like', $like)
->orWhere('original_filename', 'like', $like)
->orWhere('transcript', 'like', $like);
});
}
/**
* Short transcript excerpt, optionally centered on a search hit.
*/
public function transcriptSnippet(?string $term = null, int $radius = 80): ?string
{
if (! filled($this->transcript)) {
return null;
}
$transcript = preg_replace('/\s+/', ' ', $this->transcript) ?? $this->transcript;
if ($term === null || $term === '') {
return Str::limit($transcript, $radius * 2);
}
$position = mb_stripos($transcript, $term);
if ($position === false) {
return Str::limit($transcript, $radius * 2);
}
$start = max(0, $position - $radius);
$excerpt = mb_substr($transcript, $start, ($radius * 2) + mb_strlen($term));
return ($start > 0 ? '…' : '').$excerpt.(mb_strlen($transcript) > $start + mb_strlen($excerpt) ? '…' : '');
}
/**
* Whether a TranscribeRecording job for this recording is still on the queue.
*/
public function hasActiveTranscriptionJob(): bool
{
return DB::table('jobs')
->pluck('payload')
->contains(function (string $payload): bool {
if (! str_contains($payload, TranscribeRecording::class)) {
return false;
}
$data = json_decode($payload, true);
$command = $data['data']['command'] ?? null;
if (! is_string($command)) {
return false;
}
try {
$job = unserialize($command);
} catch (Throwable) {
return (bool) preg_match('/id";i:'.$this->id.';/', $payload);
}
return $job instanceof TranscribeRecording
&& (int) $job->recording->getKey() === (int) $this->id;
});
}
/**
* Processing/pending with no worker job left (crashed worker, bad retry_after, etc.).
*/
public function isOrphanedTranscription(): bool
{
if (! $this->isTranscribing()) {
return false;
}
if ($this->hasActiveTranscriptionJob()) {
return false;
}
$reference = $this->transcription_started_at ?? $this->updated_at;
// Allow a short window after dispatch before the row appears / worker claims it.
if ($reference !== null && $reference->gt(now()->subSeconds(15))) {
return false;
}
return true;
}
/**
* Whether this recording still expects results for the given run.
*/
public function ownsTranscriptionRun(?string $runStartedAt): bool
{
$this->refresh();
if (! $this->isTranscribing()) {
return false;
}
if ($runStartedAt === null || $this->transcription_started_at === null) {
return false;
}
return $this->transcription_started_at->getTimestamp() === Carbon::parse($runStartedAt)->getTimestamp();
}
/**
* Remove queued TranscribeRecording jobs for this recording.
*/
public function discardQueuedTranscriptionJobs(): int
{
$deleted = 0;
DB::table('jobs')
->orderBy('id')
->get()
->each(function (object $job) use (&$deleted): void {
$payload = (string) $job->payload;
if (! str_contains($payload, TranscribeRecording::class)) {
return;
}
$data = json_decode($payload, true);
$command = $data['data']['command'] ?? null;
if (! is_string($command)) {
return;
}
try {
$queued = unserialize($command);
} catch (Throwable) {
if (! preg_match('/id";i:'.$this->id.';/', $payload)) {
return;
}
DB::table('jobs')->where('id', $job->id)->delete();
$deleted++;
return;
}
if ($queued instanceof TranscribeRecording && (int) $queued->recording->getKey() === (int) $this->id) {
DB::table('jobs')->where('id', $job->id)->delete();
$deleted++;
}
});
$this->releaseTranscriptionUniqueLock();
return $deleted;
}
/**
* Stop transcription: drop queued jobs and mark the run cancelled.
*
* @param bool $silent When true, skip status update (used before starting a replacement run).
*/
public function cancelTranscription(bool $silent = false): void
{
$this->discardQueuedTranscriptionJobs();
if ($silent) {
return;
}
$this->forceFill([
'transcription_status' => 'cancelled',
'transcription_progress' => 'Stopped by user',
'transcription_percent' => $this->transcription_percent ?: 0,
'transcription_error' => 'Stopped by user',
])->save();
}
/**
* Release a leftover ShouldBeUnique lock from earlier job versions.
*/
public function releaseTranscriptionUniqueLock(): void
{
Cache::lock(
'laravel_unique_job:'.TranscribeRecording::class.'transcribe-recording:'.$this->id
)->forceRelease();
}
/**
* Mark transcription as failed and unblock the UI.
*/
public function markTranscriptionFailed(string $message): void
{
if (! $this->isTranscribing()) {
return;
}
$this->forceFill([
'transcription_status' => 'failed',
'transcription_progress' => 'Transcription failed',
'transcription_percent' => $this->transcription_percent ?: 0,
'transcription_error' => $message,
])->save();
}
/**
* Recover a stuck transcription if the queue job is gone.
*/
public function recoverOrphanedTranscription(): bool
{
if (! $this->isOrphanedTranscription()) {
return false;
}
$this->markTranscriptionFailed(
'Transcription worker stopped before finishing. Start transcription again.',
);
return true;
}
/**
* Update the live progress fields shown in the UI.
*/
@@ -141,6 +376,7 @@ class Recording extends Model
'duration_seconds' => $this->duration_seconds,
'is_active' => $this->isTranscribing(),
'has_transcript' => filled($this->transcript),
'transcript' => $this->transcript,
'transcribed_at' => $this->transcribed_at?->toIso8601String(),
];
}