Graceful stop for transcription

borken-flask
HeNine 3 years ago
parent 88147e75b6
commit fdb3f034cc

@ -3,6 +3,7 @@ import logging
import subprocess
from datetime import timedelta, datetime
from gevent.event import Event
from psycopg2._psycopg import cursor
from buscribe.recognizer import BuscribeRecognizer
@ -13,7 +14,7 @@ class HitMissingSegment(Exception):
def transcribe_segments(segments: list, sample_rate: int, recognizer: BuscribeRecognizer, start_of_transcript: datetime,
db_cursor: cursor):
db_cursor: cursor, stopping: Event):
"""Starts transcribing from a list of segments.
Only starts committing new lines to the database after reaching start_of_transcript.
@ -55,6 +56,9 @@ def transcribe_segments(segments: list, sample_rate: int, recognizer: BuscribeRe
if line_start_time > start_of_transcript:
write_line(result_json, line_start_time, line_end_time, db_cursor)
if stopping.is_set():
return segments_end_time
return segments_end_time

@ -5,8 +5,10 @@ from time import sleep
import argh
import common
import gevent
from common import dateutil
from common.database import DBManager
from gevent import signal
from buscribe.buscribe import get_end_of_transcript, transcribe_segments, finish_off_recognizer
from buscribe.recognizer import BuscribeRecognizer
@ -63,6 +65,15 @@ def main(database="", base_dir=".",
# Start priming the recognizer if possible
start_time -= timedelta(minutes=2)
stopping = gevent.event.Event()
def stop():
logging.info("Shutting down")
stopping.set()
gevent.signal_handler(signal.SIGTERM, stop)
while True:
# If end time isn't given, use current time (plus fudge) to get a "live" segment list
segments = common.get_best_segments(segments_dir,
@ -75,9 +86,10 @@ def main(database="", base_dir=".",
if recognizer.segments_start_time is None:
recognizer.segments_start_time = segments[0].start
segments_end_time = transcribe_segments(segments, SAMPLE_RATE, recognizer, start_time, db_cursor)
segments_end_time = transcribe_segments(segments, SAMPLE_RATE, recognizer, start_time, db_cursor, stopping)
if end_time is not None and segments_end_time >= end_time:
if end_time is not None and segments_end_time >= end_time \
or stopping.is_set():
# Work's done!
finish_off_recognizer(recognizer, db_cursor)
db_conn.close()

@ -7,6 +7,7 @@ setup(
install_requires = [
"argh",
"psycopg2",
"gevent==1.5a2",
"greenlet==0.4.16",
"psycogreen",
"wubloader-common",

Loading…
Cancel
Save