Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
Reprocess: resolve settings before clearing, and never leave QUEUED without a job
jjsmackay9995382990a4
diff --git a/src/services/reprocessing.py b/src/services/reprocessing.pyindex 9bb80fb0..dc24f316 100644--- a/src/services/reprocessing.py+++ b/src/services/reprocessing.py@@ -41,16 +41,30 @@ def queue_transcription_reprocess(recording, user, overrides=None):if recording.status in IN_PROGRESS:raise ReprocessError('Recording is already being processed', 400)+ # Settings first: a lookup that raises after the clearing below has+ # committed leaves a QUEUED recording with no job and no transcript,+ # which nothing can reprocess ("already being processed") or reset.+ params = resolve_transcription_params(recording, overrides or {})+ params['user_id'] = user.id+recording.transcription = Nonerecording.summary = Nonerecording.status = 'QUEUED'Event.query.filter_by(recording_id=recording.id).delete()db.session.commit()- params = resolve_transcription_params(recording, overrides or {})- params['user_id'] = user.id- return job_queue.enqueue(user_id=user.id, recording_id=recording.id,- job_type='reprocess_transcription', params=params)+ try:+ return job_queue.enqueue(user_id=user.id, recording_id=recording.id,+ job_type='reprocess_transcription', params=params)+ except Exception as e:+ # Same trap from the other side: the results are gone, so leave the+ # recording FAILED, which the reprocess and reset routes accept.+ db.session.rollback()+ recording = db.session.get(type(recording), recording.id)+ recording.status = 'FAILED'+ recording.error_message = f'Could not queue reprocessing: {e}'+ db.session.commit()+ raise ReprocessError('Could not queue reprocessing; please try again', 503) from edef queue_summary_reprocess(recording, user, custom_prompt=None, prompt_mode='replace',diff --git a/tests/test_reprocessing_service.py b/tests/test_reprocessing_service.pyindex 9ae69c3b..673ed709 100644--- a/tests/test_reprocessing_service.py+++ b/tests/test_reprocessing_service.py@@ -130,3 +130,44 @@ def test_v1_batch_uses_the_same_checks(world):assert resp.get_json()["results"][0] == {"id": ids["rec"], "success": False,"error": "Recording is already being processed"}enq.assert_not_called()+++# --- Nothing is cleared until the job can be queued ---+# A reprocess that raised after wiping the transcript (settings lookup, a+# busy database on enqueue) left the recording QUEUED with no job: the UI+# blocked every way out as "already being processed".++def _call(ids, overrides=None):+ from src.services.reprocessing import queue_transcription_reprocess+ r = db.session.get(Recording, ids["rec"])+ u = db.session.get(User, ids["user"])+ return queue_transcription_reprocess(r, u, overrides or {})+++def test_transcription_reprocess_keeps_results_when_settings_lookup_fails(world):+ ids, _ = world+ with app.app_context(), _storage(), \+ patch("src.services.transcription_defaults.resolve_transcription_params",+ side_effect=RuntimeError("settings unavailable")), \+ patch.object(job_queue, "enqueue") as enq:+ with pytest.raises(RuntimeError):+ _call(ids)+ enq.assert_not_called()+ with app.app_context():+ r = db.session.get(Recording, ids["rec"])+ assert (r.transcription, r.summary, r.status) == (TRANSCRIPT, "Old summary", "COMPLETED")+ assert Event.query.filter_by(recording_id=ids["rec"]).count() == 1+++def test_transcription_reprocess_is_not_left_queued_when_enqueue_fails(world):+ from src.services.reprocessing import ReprocessError+ ids, _ = world+ with app.app_context(), _storage(), \+ patch.object(job_queue, "enqueue", side_effect=RuntimeError("database is locked")):+ with pytest.raises(ReprocessError) as exc:+ _call(ids)+ assert exc.value.status == 503+ with app.app_context():+ r = db.session.get(Recording, ids["rec"])+ assert r.status == "FAILED"+ assert "database is locked" in (r.error_message or "")