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
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
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,
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
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;
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;
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))