Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
refactor: simplify the ingest heartbeat and name what a takeover costs
François Catuhec5a359eed6c8
diff --git a/src/api/recording_sessions.py b/src/api/recording_sessions.pyindex 11718228..8d9ba91d 100644--- a/src/api/recording_sessions.py+++ b/src/api/recording_sessions.py@@ -182,9 +182,18 @@ def _write_session_manifest(session):def _touch_session(app, session_id):- """Stamp a session as still being worked on. Best-effort by design:- a missed stamp costs a takeover, a raised exception would cost the- ingest it is reporting on."""+ """Stamp a session as still being worked on.++ Best-effort by design: a raised exception here would cost the ingest+ it reports on. Three stamps missed in a row hand a live ingest's+ session to a takeover, and since the completion path publishes+ unconditionally, the two ingests then produce two recordings for the+ one upload, only the later of which the session points at. It takes a+ database unreachable for the whole of three intervals to get there.++ Only a ``finalizing`` row is stamped, so a beat that arrives after+ the ingest published cannot revive a finished session.+ """try:with app.app_context():db.session.query(RecordingSession).filter(@@ -203,12 +212,9 @@ def _beat_until_stopped(app, session_id, stop, interval):returndef _ingest_heartbeat(session_id):- """Report an ingest alive until the returned callable is invoked.-- Used as a context manager so the stamping stops whichever way the- ingest ends, including an exception on the way out.- """+ """Report an ingest alive for as long as the block runs, however it ends."""app = current_app._get_current_object()stop = threading.Event()beater = threading.Thread(@@ -218,17 +224,11 @@ def _ingest_heartbeat(session_id):daemon=True,)beater.start()- return contextlib.closing(_Heartbeat(stop, beater))---class _Heartbeat:- def __init__(self, stop, beater):- self._stop = stop- self._beater = beater-- def close(self):- self._stop.set()- self._beater.join(timeout=5)+ try:+ yield+ finally:+ stop.set()+ beater.join(timeout=5)def _remove_session_dir(session_id, app=None):diff --git a/tests/test_sliced_file_upload.py b/tests/test_sliced_file_upload.pyindex 0170dcc0..892be322 100644--- a/tests/test_sliced_file_upload.py+++ b/tests/test_sliced_file_upload.py@@ -32,6 +32,7 @@ from src.models import Recording, RecordingSessionfrom src.api.recording_sessions import (_beat_until_stopped,_ingest_heartbeat_seconds,+ _touch_session,cleanup_expired_sessions,)@@ -438,6 +439,34 @@ def test_an_ingest_stamps_the_session_it_is_working_on():shutil.rmtree(upload_folder, ignore_errors=True)+def test_a_stamp_that_arrives_late_cannot_revive_a_finished_session():+ """The heartbeat is stopped and joined with a timeout, so a beat can+ still be in flight when the ingest publishes. Stamping only a+ 'finalizing' row is what keeps that beat from resurrecting a session+ the sweep and the status endpoint have both moved on from."""+ upload_folder = _tmp_upload_folder()+ with app.app_context():+ app.config["UPLOAD_FOLDER"] = upload_folder+ user = _mk_user("slice_latebeat")+ client = app.test_client()+ _login(client, user)+ session_id = _open_session(client, filename="sample.mp3", mime_type="audio/mpeg")["session_id"]++ session = db.session.get(RecordingSession, session_id)+ session.status = "finalized"+ published_at = datetime.utcnow() - timedelta(minutes=5)+ session.last_seen_at = published_at+ db.session.commit()++ _touch_session(app, session_id)++ db.session.expire_all()+ assert db.session.get(RecordingSession, session_id).last_seen_at == published_at++ _cleanup(user)+ shutil.rmtree(upload_folder, ignore_errors=True)++def test_a_finalize_abandoned_by_a_dead_worker_can_be_taken_over():"""Recreating the container mid-ingest leaves the session finalizingwith nobody ingesting. Without a takeover the user's retry waits for