changeset 7359:0ebcc88f1280

cache: move cache invalidation from web handler to post push hook We need the post push hook anyway ... and having the cache invalidation here will also work for ssh pushes in the future. The name log_push_action is thus no longer spot-on. That might change later, but requires some care as it also is used directly as hook name. Note that having cache invalidation in the hook will do that debug logging no longer will appear in the server log. Based on a patch by Dominik Ruf.
author Mads Kiilerich <mads@kiilerich.com>
date Wed, 08 Aug 2018 02:23:11 +0200
parents 89c30b145bb8
children 03afcfc0d17f
files kallithea/lib/base.py kallithea/lib/hooks.py kallithea/lib/middleware/simplegit.py kallithea/lib/middleware/simplehg.py
diffstat 4 files changed, 14 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/lib/base.py	Wed Aug 08 02:23:11 2018 +0200
+++ b/kallithea/lib/base.py	Wed Aug 08 02:23:11 2018 +0200
@@ -323,14 +323,6 @@
 
         return '/'.join(data)
 
-    def _invalidate_cache(self, repo_name):
-        """
-        Sets cache for this repository for invalidation on next access
-
-        :param repo_name: full repo name, also a cache key
-        """
-        ScmModel().mark_for_invalidation(repo_name)
-
     def _check_permission(self, action, user, repo_name, ip_addr=None):
         """
         Checks permissions using action (push/pull) user and repository
@@ -630,23 +622,6 @@
             raise webob.exc.HTTPBadRequest()
 
 
-class WSGIResultCloseCallback(object):
-    """Wrap a WSGI result and let close call close after calling the
-    close method on the result.
-    """
-    def __init__(self, result, close):
-        self._result = result
-        self._close = close
-
-    def __iter__(self):
-        return iter(self._result)
-
-    def close(self):
-        if hasattr(self._result, 'close'):
-            self._result.close()
-        self._close()
-
-
 @decorator.decorator
 def jsonify(func, *args, **kwargs):
     """Action decorator that formats output for JSON
--- a/kallithea/lib/hooks.py	Wed Aug 08 02:23:11 2018 +0200
+++ b/kallithea/lib/hooks.py	Wed Aug 08 02:23:11 2018 +0200
@@ -148,9 +148,14 @@
 
 def log_push_action(ui, repo, **kwargs):
     """
-    Register that changes have been pushed.
+    Register that changes have been pushed - log it *and* invalidate caches.
+    Note: It is not only logging, but also the side effect invalidating cahes!
+    The function should perhaps be renamed.
 
-    Called as Mercurial hook changegroup.push_logger or from the Git post-receive hook calling handle_git_post_receive ... or from scm _handle_push
+    Called as Mercurial hook changegroup.push_logger or from the Git
+    post-receive hook calling handle_git_post_receive ... or from scm _handle_push.
+
+    Revisions are passed in different hack-ish ways.
     """
     ex = _extract_extras()
 
@@ -180,6 +185,9 @@
     action = action_tmpl % ','.join(revs)
     action_logger(ex.username, action, ex.repository, ex.ip, commit=True)
 
+    from kallithea.model.scm import ScmModel
+    ScmModel().mark_for_invalidation(ex.repository)
+
     # extension hook call
     from kallithea import EXTENSIONS
     callback = getattr(EXTENSIONS, 'PUSH_HOOK', None)
--- a/kallithea/lib/middleware/simplegit.py	Wed Aug 08 02:23:11 2018 +0200
+++ b/kallithea/lib/middleware/simplegit.py	Wed Aug 08 02:23:11 2018 +0200
@@ -40,7 +40,7 @@
 
 from kallithea.lib.utils2 import safe_str, safe_unicode, fix_PATH, get_server_url, \
     _set_extras
-from kallithea.lib.base import BaseVCSController, WSGIResultCloseCallback, check_locking_state
+from kallithea.lib.base import BaseVCSController, check_locking_state
 from kallithea.lib.utils import make_ui, is_valid_repo
 from kallithea.lib.exceptions import HTTPLockedRC
 from kallithea.lib.hooks import pull_lock_handling
@@ -139,11 +139,7 @@
             log.info('%s action on Git repo "%s" by "%s" from %s',
                      action, str_repo_name, safe_str(user.username), ip_addr)
             app = self.__make_app(repo_name, repo_path, extras)
-            result = app(environ, start_response)
-            if action == 'push':
-                result = WSGIResultCloseCallback(result,
-                    lambda: self._invalidate_cache(repo_name))
-            return result
+            return app(environ, start_response)
         except HTTPLockedRC as e:
             log.debug('Locked, response %s: %s', e.code, e.title)
             return e(environ, start_response)
--- a/kallithea/lib/middleware/simplehg.py	Wed Aug 08 02:23:11 2018 +0200
+++ b/kallithea/lib/middleware/simplehg.py	Wed Aug 08 02:23:11 2018 +0200
@@ -37,7 +37,7 @@
 
 from kallithea.lib.utils2 import safe_str, safe_unicode, fix_PATH, get_server_url, \
     _set_extras
-from kallithea.lib.base import BaseVCSController, WSGIResultCloseCallback, check_locking_state
+from kallithea.lib.base import BaseVCSController, check_locking_state
 from kallithea.lib.utils import make_ui, is_valid_repo, ui_sections
 from kallithea.lib.vcs.utils.hgcompat import RepoError, hgweb_mod
 from kallithea.lib.exceptions import HTTPLockedRC
@@ -149,11 +149,7 @@
             log.info('%s action on Mercurial repo "%s" by "%s" from %s',
                      action, str_repo_name, safe_str(user.username), ip_addr)
             app = self.__make_app(repo_path, baseui, extras)
-            result = app(environ, start_response)
-            if action == 'push':
-                result = WSGIResultCloseCallback(result,
-                    lambda: self._invalidate_cache(repo_name))
-            return result
+            return app(environ, start_response)
         except RepoError as e:
             if str(e).find('not found') != -1:
                 return HTTPNotFound()(environ, start_response)