cutter: Add database manager and connections

One connection each for transcode checker and cutter.
We don't need more than one each since both workers only ever do one thing at once.
pull/47/head
Mike Lang 5 years ago committed by Mike Lang
parent fdd245a6d9
commit 3ce8360a1e

@ -8,16 +8,19 @@ import gevent.event
import prometheus_client as prom import prometheus_client as prom
import common import common
from common.database import DBManager, query
from .youtube import Youtube from .youtube import Youtube
class Cutter(object): class Cutter(object):
def __init__(self, youtube, stop): def __init__(self, youtube, conn, stop):
"""youtube is an authenticated and initialized youtube api client. """youtube is an authenticated and initialized youtube api client.
Conn is a database connection.
Stop is an Event triggering graceful shutdown when set. Stop is an Event triggering graceful shutdown when set.
""" """
self.youtube = youtube self.youtube = youtube
self.conn = conn
self.stop = stop self.stop = stop
self.logger = logging.getLogger(type(self).__name__) self.logger = logging.getLogger(type(self).__name__)
@ -28,12 +31,14 @@ class Cutter(object):
class TranscodeChecker(object): class TranscodeChecker(object):
def __init__(self, youtube, stop): def __init__(self, youtube, conn, stop):
""" """
youtube is an authenticated and initialized youtube api client. youtube is an authenticated and initialized youtube api client.
Conn is a database connection.
Stop is an Event triggering graceful shutdown when set. Stop is an Event triggering graceful shutdown when set.
""" """
self.youtube = youtube self.youtube = youtube
self.conn = conn
self.stop = stop self.stop = stop
self.logger = logging.getLogger(type(self).__name__) self.logger = logging.getLogger(type(self).__name__)
@ -42,7 +47,7 @@ class TranscodeChecker(object):
pass pass
def main(youtube_creds_file, metrics_port=8003, backdoor_port=0): def main(dbconnect, youtube_creds_file, metrics_port=8003, backdoor_port=0):
""" """
youtube_creds_file should be a json file containing keys 'client_id', 'client_secret' and 'refresh_token'. youtube_creds_file should be a json file containing keys 'client_id', 'client_secret' and 'refresh_token'.
""" """
@ -61,14 +66,15 @@ def main(youtube_creds_file, metrics_port=8003, backdoor_port=0):
# We have two independent jobs to do - to perform cut jobs (cutter), # We have two independent jobs to do - to perform cut jobs (cutter),
# and to check the status of transcoding videos to see if they're done (transcode checker). # and to check the status of transcoding videos to see if they're done (transcode checker).
# We want to error if either errors, and shut down if either exits. # We want to error if either errors, and shut down if either exits.
dbmanager = DBManager(dsn=dbconnect)
youtube_creds = json.load(open(youtube_creds_file)) youtube_creds = json.load(open(youtube_creds_file))
youtube = Youtube( youtube = Youtube(
client_id=youtube_creds['client_id'], client_id=youtube_creds['client_id'],
client_secret=youtube_creds['client_secret'], client_secret=youtube_creds['client_secret'],
refresh_token=youtube_creds['refresh_token'], refresh_token=youtube_creds['refresh_token'],
) )
cutter = Cutter(youtube, stop) cutter = Cutter(youtube, dbmanager.get_conn(), stop)
transcode_checker = TranscodeChecker(youtube, stop) transcode_checker = TranscodeChecker(youtube, dbmanager.get_conn(), stop)
jobs = [ jobs = [
gevent.spawn(cutter.run), gevent.spawn(cutter.run),
gevent.spawn(transcode_checker.run), gevent.spawn(transcode_checker.run),

Loading…
Cancel
Save