DailyMotion/consume_video_records.py

44 lines
1.0 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
import traceback
from DB import DBVidcon
def main():
db = DBVidcon()
try:
while True:
tasks = db.fetch_records(100)
if not tasks:
time.sleep(2)
continue
raws_to_rollback = []
for raw, data in tasks:
try:
db.upsert_video(data)
except Exception:
traceback.print_exc()
raws_to_rollback.append(raw)
try:
db.flush()
except Exception:
traceback.print_exc()
db.rollback_records([r for r, _ in tasks])
else:
if raws_to_rollback:
db.rollback_records(raws_to_rollback)
except KeyboardInterrupt:
print("Interrupted, exiting.")
except Exception:
traceback.print_exc()
finally:
db.close()
if __name__ == "__main__":
main()