Add last_refresh so the bg thread knows about manual refreshes.

This commit is contained in:
voussoir 2021-11-10 23:40:11 -08:00
parent 8376a0d4f2
commit 4279d39958
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB
2 changed files with 18 additions and 1 deletions

View file

@ -43,6 +43,15 @@ jinja_filters.register_all(site)
site.debug = True site.debug = True
site.localhost_only = False site.localhost_only = False
# This timestamp indicates the last time that all channels got a refresh.
# If the user clicks the "refresh all channels" button, we can update this
# timestamp so that the background refresher thread knows that it can wait
# a little longer.
# I chose the initial value as time.time() instead of 0 because when I'm
# testing the server and restarting it often, I don't want it making a bunch of
# network requests and/or burning API calls every time.
last_refresh = time.time()
# Request decorators ############################################################################### # Request decorators ###############################################################################
@site.before_request @site.before_request
@ -65,8 +74,13 @@ def init_ycdldb(*args, **kwargs):
ycdldb = ycdl.ycdldb.YCDLDB.closest_ycdldb(*args, **kwargs) ycdldb = ycdl.ycdldb.YCDLDB.closest_ycdldb(*args, **kwargs)
def refresher_thread(rate): def refresher_thread(rate):
global last_refresh
while True: while True:
time.sleep(rate) next_refresh = last_refresh + rate
wait = next_refresh - time.time()
if wait > 0:
time.sleep(wait)
continue
log.info('Starting refresh job.') log.info('Starting refresh job.')
thread_kwargs = {'force': False, 'skip_failures': True} thread_kwargs = {'force': False, 'skip_failures': True}
refresh_job = threading.Thread( refresh_job = threading.Thread(
@ -75,6 +89,7 @@ def refresher_thread(rate):
daemon=True, daemon=True,
) )
refresh_job.start() refresh_job.start()
last_refresh = time.time()
def start_refresher_thread(rate): def start_refresher_thread(rate):
log.info('Starting refresher thread, once per %d seconds.', rate) log.info('Starting refresher thread, once per %d seconds.', rate)

View file

@ -1,5 +1,6 @@
import flask; from flask import request import flask; from flask import request
import itertools import itertools
import time
from voussoirkit import flasktools from voussoirkit import flasktools
from voussoirkit import pathclass from voussoirkit import pathclass
@ -144,6 +145,7 @@ def post_refresh_all_channels():
force = request.form.get('force', False) force = request.form.get('force', False)
force = stringtools.truthystring(force, False) force = stringtools.truthystring(force, False)
common.ycdldb.refresh_all_channels(force=force, skip_failures=True, commit=True) common.ycdldb.refresh_all_channels(force=force, skip_failures=True, commit=True)
common.last_refresh = time.time()
return flasktools.json_response({}) return flasktools.json_response({})
@flasktools.required_fields(['state'], forbid_whitespace=True) @flasktools.required_fields(['state'], forbid_whitespace=True)