fix scheduler not running, template rendering route implementation, implement file change modifed checks

This commit is contained in:
Xevion
2020-06-19 17:35:23 -05:00
parent 4bc5d7e72e
commit 1a4779d8d5
5 changed files with 40 additions and 24 deletions

View File

@@ -1 +1,2 @@
Flask~=1.1.2
Flask~=1.1.2
APScheduler~=3.6.3

View File

@@ -15,7 +15,7 @@ from trivia import routes, api, utils
# Setup a scheduler for automatically refreshing data
scheduler = BackgroundScheduler()
scheduler.start()
scheduler.add_job(func=utils.refreshScores, trigger="interval", seconds=5)
utils.refreshScores()

View File

@@ -3,6 +3,7 @@ routes.py
Handles user frontend routes.
"""
from flask import render_template
from trivia import app
@@ -14,4 +15,6 @@ def index():
:return:
"""
return 'index'
from trivia.utils import teams
scoreCount = max([len(team.scores) for team in teams]) if len(teams) > 0 else 0
return render_template('index.html', scoreCount=scoreCount, teams=teams)

View File

@@ -5,6 +5,7 @@ Stores important backend application functionality.
"""
import json
import os
from typing import List
from trivia import Team
@@ -12,8 +13,9 @@ from trivia import Team
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
DATA_DIR = os.path.join(BASE_DIR, 'data')
# data: List[Team] = []
teams = []
# Initialize global data/tracking vars
lastChange: int = -1
teams: List[Team] = []
def refreshScores() -> None:
@@ -23,22 +25,32 @@ def refreshScores() -> None:
:return:
"""
try:
print('Attempting to load and parse scores file.')
with open(os.path.join(DATA_DIR, 'scores.json')) as file:
temp = json.load(file)
global lastChange
filepath = os.path.join(DATA_DIR, 'scores.json')
curChange = os.stat(filepath).st_mtime
# Place all values into Team object for jinja
temp = [
Team(
id=team['teamno'],
name=team['teamname'],
scores=team['scores']
) for team in temp
]
print(f'Successfully loaded ({len(temp)} teams).')
global teams
teams = temp
# If invalid or inaccessible, simply do nothing.
except json.JSONDecodeError:
print('Scores file could not be opened or parsed.')
if lastChange < curChange:
try:
# Update tracking var
lastChange = curChange
print('Attempting to load and parse scores file.')
with open(filepath) as file:
temp = json.load(file)
# Place all values into Team object for jinja
temp = [
Team(
id=team['teamno'],
name=team['teamname'],
scores=team['scores']
) for team in temp
]
print(f'Successfully loaded ({len(temp)} teams).')
global teams
teams = temp
# If invalid or inaccessible, simply do nothing.
except json.JSONDecodeError:
print('Scores file could not be opened or parsed.')

View File

@@ -7,4 +7,4 @@ Simple launcher file for project.
from trivia import app
if __name__ == "__main__":
app.run(host="0.0.0.0")
app.run(host="0.0.0.0", use_reloader=False)