Separate try-except for the get_video loop and mark loop.

This will help prevent inconsistent state where some videos get marked,
then one raises, and the db rolls back but the other objects still have
their state attribute set.
This commit is contained in:
voussoir 2021-10-26 12:23:45 -07:00
parent 413f9af568
commit c0235901c6
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB

View file

@ -1,6 +1,7 @@
import flask; from flask import request import flask; from flask import request
from voussoirkit import flasktools from voussoirkit import flasktools
from voussoirkit import stringtools
import ycdl import ycdl
@ -12,18 +13,18 @@ site = common.site
@site.route('/mark_video_state', methods=['POST']) @site.route('/mark_video_state', methods=['POST'])
def post_mark_video_state(): def post_mark_video_state():
video_ids = request.form['video_ids'] video_ids = request.form['video_ids']
video_ids = stringtools.comma_space_split(video_ids)
state = request.form['state'] state = request.form['state']
try:
video_ids = video_ids.split(',')
for video_id in video_ids:
video = common.ycdldb.get_video(video_id)
video.mark_state(state, commit=False)
common.ycdldb.commit()
try:
videos = [common.ycdldb.get_video(id) for id in video_ids]
except ycdl.exceptions.NoSuchVideo as exc: except ycdl.exceptions.NoSuchVideo as exc:
common.ycdldb.rollback()
return flasktools.json_response(exc.jsonify(), status=404) return flasktools.json_response(exc.jsonify(), status=404)
try:
for video in videos:
video.mark_state(state, commit=False)
common.ycdldb.commit()
except ycdl.exceptions.InvalidVideoState as exc: except ycdl.exceptions.InvalidVideoState as exc:
common.ycdldb.rollback() common.ycdldb.rollback()
return flasktools.json_response(exc.jsonify(), status=400) return flasktools.json_response(exc.jsonify(), status=400)
@ -34,14 +35,15 @@ def post_mark_video_state():
@site.route('/start_download', methods=['POST']) @site.route('/start_download', methods=['POST'])
def post_start_download(): def post_start_download():
video_ids = request.form['video_ids'] video_ids = request.form['video_ids']
try: video_ids = stringtools.comma_space_split(video_ids)
video_ids = video_ids.split(',')
for video_id in video_ids:
common.ycdldb.download_video(video_id, commit=False)
common.ycdldb.commit()
except ycdl.ytapi.VideoNotFound: try:
common.ycdldb.rollback() videos = [common.ycdldb.get_video(id) for id in video_ids]
flask.abort(404) except ycdl.exceptions.NoSuchVideo as exc:
return flasktools.json_response(exc.jsonify(), status=404)
for video in videos:
common.ycdldb.download_video(video, commit=False)
common.ycdldb.commit()
return flasktools.json_response({'video_ids': video_ids, 'state': 'downloaded'}) return flasktools.json_response({'video_ids': video_ids, 'state': 'downloaded'})