changeset 3235:d6029dacbcc4 beta

API invalidate_cache function ref #733
author Marcin Kuzminski <marcin@python-works.com>
date Sat, 26 Jan 2013 23:12:50 +0100
parents 21cccfea18bf
children 48b176df890c
files docs/api/api.rst rhodecode/controllers/api/api.py rhodecode/tests/api/api_base.py
diffstat 3 files changed, 67 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/docs/api/api.rst	Sat Jan 26 23:11:40 2013 +0100
+++ b/docs/api/api.rst	Sat Jan 26 23:12:50 2013 +0100
@@ -152,6 +152,28 @@
     error :  null
 
 
+invalidate_cache
+----------------
+
+Invalidate cache for repository.
+This command can be executed only using api_key belonging to user with admin
+rights or regular user that have write or admin or write access to repository.
+
+INPUT::
+
+    id : <id_for_response>
+    api_key : "<api_key>"
+    method :  "invalidate_cache"
+    args :    {
+                "repoid" : "<reponame or repo_id>"
+              }
+
+OUTPUT::
+
+    id : <id_given_in_input>
+    result : "Cache for repository `<reponame>` was invalidated: invalidated cache keys: <list_of_cache_keys>"
+    error :  null
+
 lock
 ----
 
--- a/rhodecode/controllers/api/api.py	Sat Jan 26 23:11:40 2013 +0100
+++ b/rhodecode/controllers/api/api.py	Sat Jan 26 23:12:50 2013 +0100
@@ -202,6 +202,32 @@
                 'Error occurred during rescan repositories action'
             )
 
+    def invalidate_cache(self, apiuser, repoid):
+        """
+        Dispatch cache invalidation action on given repo
+
+        :param apiuser:
+        :param repoid:
+        """
+        repo = get_repo_or_error(repoid)
+        if HasPermissionAnyApi('hg.admin')(user=apiuser) is False:
+            # check if we have admin permission for this repo !
+            if HasRepoPermissionAnyApi('repository.admin',
+                                       'repository.write')(user=apiuser,
+                                            repo_name=repo.repo_name) is False:
+                raise JSONRPCError('repository `%s` does not exist' % (repoid))
+
+        try:
+            invalidated_keys = ScmModel().mark_for_invalidation(repo.repo_name)
+            Session().commit()
+            return ('Cache for repository `%s` was invalidated: '
+                    'invalidated cache keys: %s' % (repoid, invalidated_keys))
+        except Exception:
+            log.error(traceback.format_exc())
+            raise JSONRPCError(
+                'Error occurred during cache invalidation action'
+            )
+
     def lock(self, apiuser, repoid, locked, userid=Optional(OAttr('apiuser'))):
         """
         Set locking state on particular repository by given user, if
--- a/rhodecode/tests/api/api_base.py	Sat Jan 26 23:11:40 2013 +0100
+++ b/rhodecode/tests/api/api_base.py	Sat Jan 26 23:12:50 2013 +0100
@@ -286,6 +286,25 @@
         expected = 'Error occurred during rescan repositories action'
         self._compare_error(id_, expected, given=response.body)
 
+    def test_api_invalidate_cache(self):
+        id_, params = _build_data(self.apikey, 'invalidate_cache',
+                                  repoid=self.REPO)
+        response = api_call(self, params)
+
+        expected = ("Cache for repository `%s` was invalidated: "
+                    "invalidated cache keys: %s" % (self.REPO,
+                                                    [unicode(self.REPO)]))
+        self._compare_ok(id_, expected, given=response.body)
+
+    @mock.patch.object(ScmModel, 'mark_for_invalidation', crash)
+    def test_api_invalidate_cache_error(self):
+        id_, params = _build_data(self.apikey, 'invalidate_cache',
+                                  repoid=self.REPO)
+        response = api_call(self, params)
+
+        expected = 'Error occurred during cache invalidation action'
+        self._compare_error(id_, expected, given=response.body)
+
     def test_api_lock_repo_lock_aquire(self):
         id_, params = _build_data(self.apikey, 'lock',
                                   userid=TEST_USER_ADMIN_LOGIN,