Teach varnish purging code about xkey purges
authorMagnus Hagander <magnus@hagander.net>
Fri, 6 Jul 2018 13:32:14 +0000 (15:32 +0200)
committerMagnus Hagander <magnus@hagander.net>
Fri, 6 Jul 2018 13:32:14 +0000 (15:32 +0200)
It's just another  type of purge, so it's added as an extra option with
'K' as the key (as 'X' was already taken).

pgweb/core/views.py
pgweb/util/misc.py
sql/varnish.sql
sql/varnish_local.sql
tools/varnishqueue/varnish_queue.py

index dc6db30e4a1e136db33b11a948cb82548f980b60..daaa86c26f7c7acc9eec6d03e2784b4172182ebe 100644 (file)
@@ -21,7 +21,7 @@ from pgweb.util.decorators import cache, nocache
 from pgweb.util.contexts import render_pgweb, get_nav_menu, PGWebContextProcessor
 from pgweb.util.helpers import simple_form, PgXmlHelper, HttpServerError
 from pgweb.util.moderation import get_all_pending_moderations
-from pgweb.util.misc import get_client_ip, varnish_purge
+from pgweb.util.misc import get_client_ip, varnish_purge, varnish_purge_expr, varnish_purge_xkey
 from pgweb.util.sitestruct import get_all_pages_struct
 
 # models needed for the pieces on the frontpage
@@ -277,10 +277,24 @@ def admin_pending(request):
 def admin_purge(request):
        if request.method == 'POST':
                url = request.POST['url']
-               if url == '':
+               expr = request.POST['expr']
+               xkey = request.POST['xkey']
+               l = len(filter(None, [url, expr, xkey]))
+               if l == 0:
+                       # Nothing specified
                        return HttpResponseRedirect('.')
-               varnish_purge(url)
-               messages.info(request, "Purge completed: '^%s'" % url)
+               elif l > 1:
+                       messages.error(request, "Can only specify one of url, expression and xkey!")
+                       return HttpResponseRedirect('.')
+
+               if url:
+                       varnish_purge(url)
+               elif expr:
+                       varnish_purge_expr(expr)
+               else:
+                       varnish_purge_xkey(xkey)
+
+               messages.info(request, "Purge added.")
                return HttpResponseRedirect('.')
 
        # Fetch list of latest purges
index 364af5f4fc4802b71cc8a0d1eacf07a59836b20f..20bf822e2918219eeec3e0d7a7bca16d8ca0c071 100644 (file)
@@ -34,6 +34,12 @@ def get_client_ip(request):
        return request.META['REMOTE_ADDR']
 
 
+def varnish_purge_xkey(xkey):
+       """
+       Purge the specified xkey from Varnish.
+       """
+       connection.cursor().execute("SELECT varnish_purge_xkey(%s)", (xkey, ))
+
 def varnish_purge(url):
        """
        Purge the specified URL from Varnish. Will add initial anchor to the URL,
@@ -42,6 +48,13 @@ def varnish_purge(url):
        url = '^%s' % url
        connection.cursor().execute("SELECT varnish_purge(%s)", (url, ))
 
+def varnish_purge_expr(expr):
+       """
+       Purge the specified expression from Varnish. Does not modify the expression
+       at all, so be very careful!
+       """
+       connection.cursor().execute("SELECT varnish_purge_expr(%s)", (expr, ))
+
 def version_sort(l):
        """
        map a directory name to a format that will show up sensibly in an ascii sort
index 1079208801db20b2eee99c9fd1f9b5d6cb9320cd..d78138fd42c4d59a786f6a1043d9529ff87531ef 100644 (file)
@@ -26,4 +26,12 @@ AS $$
   INSERT INTO varnishqueue.queue (mode, consumerid, expr) SELECT 'X', consumerid, $1 FROM varnishqueue.consumers;
   NOTIFY varnishqueue;
 $$ LANGUAGE 'sql';
+
+DROP FUNCTION IF EXISTS varnish_purge_xkey(key text);
+CREATE OR REPLACE FUNCTION varnish_purge_xkey(key text)
+RETURNS void
+AS $$
+  INSERT INTO varnishqueue.queue (mode, consumerid, expr) SELECT 'K', consumerid, $1 FROM varnishqueue.consumers;
+  NOTIFY varnishqueue;
+$$ LANGUAGE 'sql';
 COMMIT;
index 30fe7360d2aeaf7c8467446df8afaba62bbdb06e..926eb14269326d6914bb024c61599074eaf5ba41 100644 (file)
@@ -16,4 +16,9 @@ RETURNS void
 AS $$
 $$ LANGUAGE 'sql';
 
-COMMIT;
\ No newline at end of file
+CREATE OR REPLACE FUNCTION varnish_purge_xkey(key text)
+RETURNS void
+AS $$
+$$ LANGUAGE 'sql';
+
+COMMIT;
index afbe767918e0619676654e1de09fc5ae4c15142f..1e66ea57884027330d5125d426626adf33a018c0 100755 (executable)
@@ -63,6 +63,11 @@ def worker(consumerid, consumername, dsn):
                                        if not do_purge(consumername, {'X-Purge-Expr': r[2]}):
                                                failed = True
                                                continue
+                               elif r[1] == 'K':
+                                       logging.info("Purging xkey %s on %s" % (r[2], consumername))
+                                       if not do_purge(consumername, {'X-Purge-Xkey': r[2]}):
+                                               failed = True
+                                               continue
                                else:
                                        logging.warning("Unknown purge type %s on %s, ignoring." % (r[1], consumername))