Skip to content

Commit

Permalink
Merge pull request #1755 from UlrichB22/before_wiki
Browse files Browse the repository at this point in the history
app.py: skip before_wiki and teardown_wiki for static content
  • Loading branch information
RogerHaase authored Sep 10, 2024
2 parents 7899ba7 + 781c8e1 commit 90436ac
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
38 changes: 32 additions & 6 deletions src/moin/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import os
from os import path
import re
import sys

from flask import Flask, request, session
Expand Down Expand Up @@ -292,6 +293,10 @@ def before_wiki():
"""
Setup environment for wiki requests, start timers.
"""
if request and is_static_content(request.path):
logging.debug(f"skipping before_wiki for {request.path}")
return

logging.debug("running before_wiki")
flaskg.clock = Clock()
flaskg.clock.start("total")
Expand Down Expand Up @@ -323,19 +328,22 @@ def teardown_wiki(response):
"""
Teardown environment of wiki requests, stop timers.
"""
if request:
request_path = request.path
if is_static_content(request_path):
return response
else:
request_path = ""

logging.debug("running teardown_wiki")
try:
flaskg.clock.stop("total")
del flaskg.clock
except AttributeError:
# can happen if teardown_wiki() is called twice, e.g. by unit tests.
pass

if hasattr(flaskg, "edit_utils"):
# if transaction fails with sql file locked, we try to free it here
try:
flaskg.edit_utils.conn.close()
except AttributeError:
pass

try:
# whoosh cache performance
for cache in (
Expand All @@ -353,4 +361,22 @@ def teardown_wiki(response):
# moin commands may not have flaskg.storage
pass

try:
flaskg.clock.stop("total", comment=request_path)
del flaskg.clock
except AttributeError:
# can happen if teardown_wiki() is called twice, e.g. by unit tests.
pass

return response


def is_static_content(request_path):
"""
Check if content is static and does not need usual wiki handling
"""

if request_path.startswith(("/static/", "/+serve/", "/+template/")) or re.match(r"/_themes/\w+/css/", request_path):
return True
else:
return False
12 changes: 7 additions & 5 deletions src/moin/utils/clock.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright: 2001-2003 Juergen Hermann <[email protected]>
# Copyright: 2003-2006 MoinMoin:ThomasWaldmann
# Copyright: 2024 MoinMoin:UlrichB
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

"""
Expand All @@ -22,11 +23,12 @@ class Clock:
Helper class for measuring the time needed to run code.
Usage:
flaskg.clock.start('mytimer')
flaskg.clock.start("mytimer")
# do something
flaskg.clock.stop('mytimer')
flaskg.clock.stop("mytimer", comment="add this to the log message")
# the optional comment field is added to the log message
# or if you want to use its value later
timerval = flaskg.clock.stop('mytimer')
timerval = flaskg.clock.stop("mytimer")
Starting a timer multiple times is supported but the
one started last has to be stopped first.
Expand All @@ -40,10 +42,10 @@ def start(self, timer):
self.timers[timer] = []
self.timers[timer].append(time.time())

def stop(self, timer):
def stop(self, timer, comment=""):
if timer in self.timers:
value = time.time() - self.timers[timer].pop()
logging.debug(f"timer {timer}({len(self.timers[timer])}): {value * 1000:.2f}ms")
logging.debug(f"timer {timer}({len(self.timers[timer])}): {value * 1000:.2f}ms {comment}")
if not self.timers[timer]:
del self.timers[timer]
return value
Expand Down

0 comments on commit 90436ac

Please sign in to comment.