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 ...@@ -4,10 +4,8 @@ from flask import Response
from flask import render_template from flask import render_template
# app imports # app imports
from parscript import load_data from appmethods import updatebothdbs
from getconnectedscript import load_getconn_data from redisactions import redisdb
from redisactions import *
from appmethods import *
# python imports # python imports
import json import json
...@@ -20,9 +18,6 @@ import time ...@@ -20,9 +18,6 @@ import time
# setting up flask instance # setting up flask instance
app = Flask(__name__) app = Flask(__name__)
# setting up redis database
redisdb = redis.from_url("redis://localhost:6379/0", db=0)
# setting up cacheing # setting up cacheing
schedule.every().day.at("02:00").do(updatebothdbs) schedule.every().day.at("02:00").do(updatebothdbs)
# schedule.every(5).seconds.do(updatebothdbs) # schedule.every(5).seconds.do(updatebothdbs)
......
...@@ -5,15 +5,24 @@ import redisactions as f ...@@ -5,15 +5,24 @@ import redisactions as f
# python imports # python imports
import json 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(): def updatebothdbs():
livesuccess = f.livedbfill(json.dumps(load_data(), ensure_ascii=False)) livesuccess = f.livedbfill(json.dumps(load_data(), ensure_ascii=False))
gcsuccess = f.gcdbfill(json.dumps(load_getconn_data(), ensure_ascii=False)) gcsuccess = f.gcdbfill(json.dumps(load_getconn_data(), ensure_ascii=False))
print "DBs updated.\n25Live: " + str(livesuccess) \ successLog = str(datetime.datetime.now()) \
+ "\nGC: " + str(gcsuccess) + "\n\nAttempted to update cache." \
return livesuccess and gcsuccess + "\n25Live: " + str(livesuccess) \
+ "\nGC: " + str(gcsuccess)
f.appendtoupdatelog(successLog)
return successLog
def testprint(): def testprint():
......
# app imports
from __init__ import redisdb
# python imports # python imports
import datetime import datetime
...@@ -18,37 +15,36 @@ import redis ...@@ -18,37 +15,36 @@ import redis
# function. and every time we find an error we want to run a dberrorfill() # function. and every time we find an error we want to run a dberrorfill()
# function. # function.
# setting up redis database
redisdb = redis.from_url("redis://localhost:6379/0", db=0)
# this will update the live dictlist and the cachedate # this will update the live dictlist and the cachedate
# returns true if the dictlist is not empty, false otherwise # returns true if the dictlist is not empty, false otherwise
def gcdbfill(dictlist): def gcdbfill(dictlist):
try: success = redisdb.set("gcdict", dictlist)
redisdb.set("gcdict", dictlist)
setlastcachedate("gccachedate", str(datetime.datetime.now())) log = str(datetime.datetime.now())
except e: redisdb.set("gccachedate", log)
return False 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 # saves new dictlist in place of previous 25Live dictlist
# returns true if the dictlist is not empty, false otherwise # returns true if the dictlist is not empty, false otherwise
def livedbfill(dictlist): def livedbfill(dictlist):
try: success = redisdb.set("livedict", dictlist)
redisdb.set("livedict", dictlist)
setlastcachedate("livecachedate", str(datetime.datetime.now())) log = str(datetime.datetime.now())
except e: redisdb.set("livecachedate", log)
return False 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 # appends the log string to the head of our update long
# return true if the cachedate is not empty, false otherwise # returns true if the head is the newest update
def setlastcachedate(cache, date): def appendtoupdatelog(logstring):
try: redisdb.lpush("dbupdatelog", logstring)
redisdb.set(cache, date)
except e:
return False
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