Cache locking, markdown wrapping, referer fixing
This commit is contained in:
parent
ca241f2bcf
commit
845c47403e
|
@ -42,13 +42,20 @@ def get(document, cache=True):
|
||||||
else:
|
else:
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
# Get a lock for reading.
|
||||||
|
lock = lock_cache(document)
|
||||||
|
|
||||||
# Get the JSON data.
|
# Get the JSON data.
|
||||||
data = read_json(path)
|
data = read_json(path)
|
||||||
|
|
||||||
|
# Unlock!
|
||||||
|
unlock_cache(lock)
|
||||||
|
|
||||||
# Cache and return it.
|
# Cache and return it.
|
||||||
if cache:
|
if cache:
|
||||||
set_cache(document, data, expires=cache_lifetime)
|
set_cache(document, data, expires=cache_lifetime)
|
||||||
set_cache(document+"_mtime", stat.st_mtime, expires=cache_lifetime)
|
set_cache(document+"_mtime", stat.st_mtime, expires=cache_lifetime)
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
@ -56,8 +63,7 @@ def commit(document, data, cache=True):
|
||||||
"""Insert/update a document in the DB."""
|
"""Insert/update a document in the DB."""
|
||||||
|
|
||||||
# Only allow one commit at a time.
|
# Only allow one commit at a time.
|
||||||
if not lock_cache(document):
|
lock = lock_cache(document)
|
||||||
return
|
|
||||||
|
|
||||||
# Need to create the file?
|
# Need to create the file?
|
||||||
path = mkpath(document)
|
path = mkpath(document)
|
||||||
|
@ -83,7 +89,7 @@ def commit(document, data, cache=True):
|
||||||
set_cache(document+"_mtime", time.time(), expires=cache_lifetime)
|
set_cache(document+"_mtime", time.time(), expires=cache_lifetime)
|
||||||
|
|
||||||
# Release the lock.
|
# Release the lock.
|
||||||
unlock_cache(document)
|
unlock_cache(lock)
|
||||||
|
|
||||||
|
|
||||||
def delete(document):
|
def delete(document):
|
||||||
|
@ -166,15 +172,10 @@ def write_json(path, data):
|
||||||
logger.debug("JsonDB: WRITE > {}".format(path))
|
logger.debug("JsonDB: WRITE > {}".format(path))
|
||||||
|
|
||||||
# Open and lock the file.
|
# Open and lock the file.
|
||||||
fh = None
|
fh = codecs.open(path, 'w', 'utf-8')
|
||||||
if os.path.isfile(path):
|
|
||||||
fh = codecs.open(path, 'r+', 'utf-8')
|
|
||||||
else:
|
|
||||||
fh = codecs.open(path, 'w', 'utf-8')
|
|
||||||
flock(fh, LOCK_EX)
|
flock(fh, LOCK_EX)
|
||||||
|
|
||||||
# Write it.
|
# Write it.
|
||||||
fh.truncate(0)
|
|
||||||
fh.write(json.dumps(data, sort_keys=True, indent=4, separators=(',', ': ')))
|
fh.write(json.dumps(data, sort_keys=True, indent=4, separators=(',', ': ')))
|
||||||
|
|
||||||
# Unlock and close.
|
# Unlock and close.
|
||||||
|
@ -243,25 +244,16 @@ def lock_cache(key, timeout=5, expire=20):
|
||||||
|
|
||||||
Returns True on success, None on failure to acquire lock."""
|
Returns True on success, None on failure to acquire lock."""
|
||||||
lock_key = key + "_lock"
|
lock_key = key + "_lock"
|
||||||
begin = int(time.time())
|
client = get_redis()
|
||||||
|
|
||||||
lock = get_cache(lock_key)
|
|
||||||
while lock:
|
|
||||||
time.sleep(0.2)
|
|
||||||
lock = get_cache(lock_key)
|
|
||||||
if int(time.time()) - begin >= timeout:
|
|
||||||
handle_exception(
|
|
||||||
Exception("Redis key lock timed out waiting for {}".format(key))
|
|
||||||
)
|
|
||||||
return None
|
|
||||||
|
|
||||||
# Take the lock.
|
# Take the lock.
|
||||||
set_cache(lock_key, time.time(), expire)
|
lock = client.lock(key, timeout=expire)
|
||||||
|
lock.acquire()
|
||||||
logger.debug("Cache lock acquired: {}, expires in {}s".format(key, expire))
|
logger.debug("Cache lock acquired: {}, expires in {}s".format(key, expire))
|
||||||
return True
|
return lock
|
||||||
|
|
||||||
|
|
||||||
def unlock_cache(key):
|
def unlock_cache(lock):
|
||||||
"""Release the lock on a cache key."""
|
"""Release the lock on a cache key."""
|
||||||
del_cache(key + "_lock")
|
lock.release()
|
||||||
logger.debug("Cache lock released: {}".format(key))
|
logger.debug("Cache lock released")
|
||||||
|
|
|
@ -97,6 +97,15 @@ def log_referrer(request, link):
|
||||||
timeout=5,
|
timeout=5,
|
||||||
verify=False, # Don't do SSL verification
|
verify=False, # Don't do SSL verification
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Make sure the request didn't just redirect back to our main site
|
||||||
|
# (e.g. http://whatever.example.com wildcard may redirect back to
|
||||||
|
# http://example.com, and if that's us, don't log that!
|
||||||
|
if r.url.startswith("http://{}".format(hostname)) or \
|
||||||
|
r.url.startswith("https://{}".format(hostname)):
|
||||||
|
return None
|
||||||
|
|
||||||
|
# Look for our hostname in their page.
|
||||||
if hostname in r.text:
|
if hostname in r.text:
|
||||||
# Log it.
|
# Log it.
|
||||||
db = list()
|
db = list()
|
||||||
|
@ -106,9 +115,8 @@ def log_referrer(request, link):
|
||||||
db.append(link)
|
db.append(link)
|
||||||
JsonDB.commit("traffic/referrers", db, cache=False)
|
JsonDB.commit("traffic/referrers", db, cache=False)
|
||||||
return link
|
return link
|
||||||
except Exception as e:
|
except:
|
||||||
handle_exception(e)
|
pass
|
||||||
return None
|
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -206,5 +214,6 @@ def get_referrers(recent=25):
|
||||||
|
|
||||||
recent = 0 - recent
|
recent = 0 - recent
|
||||||
result["recent"] = db[recent:]
|
result["recent"] = db[recent:]
|
||||||
|
result["recent"].reverse()
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
|
@ -163,7 +163,9 @@ def render_markdown(body, html_escape=True, extensions=None, blacklist=None):
|
||||||
for ext in blacklist:
|
for ext in blacklist:
|
||||||
args["extensions"].remove(str(ext))
|
args["extensions"].remove(str(ext))
|
||||||
|
|
||||||
return markdown.markdown(body, **args)
|
return '<div class="markdown">{}</div>'.format(
|
||||||
|
markdown.markdown(body, **args)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def send_email(to, subject, message, sender=None, reply_to=None):
|
def send_email(to, subject, message, sender=None, reply_to=None):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user