Commit f9e4c279 authored by Landon DeCoito's avatar Landon DeCoito

refactored appmethods, redisactions. Moved redisdb creation to redisactions

parent f5408b5b
......@@ -4,10 +4,8 @@ from flask import Response
from flask import render_template
# app imports
from parscript import load_data
from getconnectedscript import load_getconn_data
from redisactions import *
from appmethods import *
from appmethods import updatebothdbs
from redisactions import redisdb
# python imports
import json
......@@ -20,9 +18,6 @@ import time
# setting up flask instance
app = Flask(__name__)
# setting up redis database
redisdb = redis.from_url("redis://localhost:6379/0", db=0)
# setting up cacheing
schedule.every().day.at("02:00").do(updatebothdbs)
# schedule.every(5).seconds.do(updatebothdbs)
......
......@@ -5,15 +5,24 @@ import redisactions as f
# python imports
import json
import datetime
# other imports
import redis
# attempts to update both dbs and returns true if successful
# attempts to update both dbs and logs the result
def updatebothdbs():
livesuccess = f.livedbfill(json.dumps(load_data(), ensure_ascii=False))
gcsuccess = f.gcdbfill(json.dumps(load_getconn_data(), ensure_ascii=False))
print "DBs updated.\n25Live: " + str(livesuccess) \
+ "\nGC: " + str(gcsuccess)
return livesuccess and gcsuccess
successLog = str(datetime.datetime.now()) \
+ "\n\nAttempted to update cache." \
+ "\n25Live: " + str(livesuccess) \
+ "\nGC: " + str(gcsuccess)
f.appendtoupdatelog(successLog)
return successLog
def testprint():
......
# app imports
from __init__ import redisdb
# python imports
import datetime
......@@ -18,37 +15,36 @@ import redis
# function. and every time we find an error we want to run a dberrorfill()
# function.
# setting up redis database
redisdb = redis.from_url("redis://localhost:6379/0", db=0)
# this will update the live dictlist and the cachedate
# returns true if the dictlist is not empty, false otherwise
def gcdbfill(dictlist):
try:
redisdb.set("gcdict", dictlist)
setlastcachedate("gccachedate", str(datetime.datetime.now()))
except e:
return False
success = redisdb.set("gcdict", dictlist)
log = str(datetime.datetime.now())
redisdb.set("gccachedate", log)
success = redisdb.get("gccachedate") == log and success
return redisdb.get("gcdict") is not None
return redisdb.get("gcdict") is not None and success
# saves new dictlist in place of previous 25Live dictlist
# returns true if the dictlist is not empty, false otherwise
def livedbfill(dictlist):
try:
redisdb.set("livedict", dictlist)
setlastcachedate("livecachedate", str(datetime.datetime.now()))
except e:
return False
success = redisdb.set("livedict", dictlist)
log = str(datetime.datetime.now())
redisdb.set("livecachedate", log)
success = redisdb.get("livecachedate") == log and success
return redisdb.get("livedict") is not None
return redisdb.get("livedict") is not None and success
# saves the last time the cache was updated
# return true if the cachedate is not empty, false otherwise
def setlastcachedate(cache, date):
try:
redisdb.set(cache, date)
except e:
return False
# appends the log string to the head of our update long
# returns true if the head is the newest update
def appendtoupdatelog(logstring):
redisdb.lpush("dbupdatelog", logstring)
return redisdb.get(cache) is not None
return redisdb.lrange("dbupdatelog", 0, 0) == logstring
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment