From 2f8bbc40dd7e80b3d9ee2aa1ac7ee9c5bcfff777 Mon Sep 17 00:00:00 2001 From: Magnus Hagander Date: Wed, 27 Apr 2016 13:17:32 +0200 Subject: Replace pgq with trivial local queue implementation The queue used for varnish purges has so few entries that it's really not worth paying the management overhead for skytools/pgq. Instead we can use a very simple local deamon using LISTEN/NOTIFY to fire them off. Now include a proper nagios plugin in this package, so we can get rid of the not-very-nice munin plugin currently used in the deployment. --- pgweb/core/views.py | 11 ++- pgweb/settings.py | 4 +- sql/varnish.sql | 20 +++-- templates/core/admin_purge.html | 17 ++-- tools/pgq/varnish_consumer.py | 173 ------------------------------------ tools/pgq/varnish_pgq.ini | 20 ----- tools/varnishqueue/nagios_check.py | 39 ++++++++ tools/varnishqueue/varnish_queue.py | 145 ++++++++++++++++++++++++++++++ 8 files changed, 214 insertions(+), 215 deletions(-) delete mode 100755 tools/pgq/varnish_consumer.py delete mode 100644 tools/pgq/varnish_pgq.ini create mode 100755 tools/varnishqueue/nagios_check.py create mode 100755 tools/varnishqueue/varnish_queue.py diff --git a/pgweb/core/views.py b/pgweb/core/views.py index 6b400bcd..2317a206 100644 --- a/pgweb/core/views.py +++ b/pgweb/core/views.py @@ -3,6 +3,7 @@ from django.http import HttpResponse, Http404, HttpResponseRedirect from django.http import HttpResponseNotModified from django.template import TemplateDoesNotExist, loader from django.contrib.auth.decorators import login_required, user_passes_test +from django.contrib import messages from django.views.decorators.csrf import csrf_exempt from django.db.models import Count from django.db import connection, transaction @@ -266,17 +267,15 @@ def admin_purge(request): return HttpResponseRedirect('.') varnish_purge(url) transaction.commit_unless_managed() - completed = '^%s' % url - else: - completed = None + messages.info(request, "Purge completed: '^%s'" % url) + return HttpResponseRedirect('.') # Fetch list of latest purges curs = connection.cursor() - curs.execute("SELECT ev_time, ev_type, ev_data FROM pgq.event_%s WHERE ev_type IN ('P', 'X') ORDER BY ev_time DESC LIMIT 20" % settings.VARNISH_QUEUE_ID) - latest = [{'t': r[0], 'ty': r[1], 'u': r[2]} for r in curs.fetchall()] + curs.execute("SELECT added, completed, consumer, mode, expr FROM varnishqueue.queue q LEFT JOIN varnishqueue.consumers c ON c.consumerid=q.consumerid ORDER BY added DESC") + latest = curs.fetchall() return render_to_response('core/admin_purge.html', { - 'purge_completed': completed, 'latest_purges': latest, }, RequestContext(request)) diff --git a/pgweb/settings.py b/pgweb/settings.py index 624c920d..67da6e70 100644 --- a/pgweb/settings.py +++ b/pgweb/settings.py @@ -135,6 +135,9 @@ PASSWORD_HASHERS = ( 'django.contrib.auth.hashers.CryptPasswordHasher', ) +# Default format for date/time (as it changes between machines) +DATETIME_FORMAT="Y-m-d H:i:s" + # Configure recaptcha. Most details contain keys and are thus handled # in settings_local.py. Override NOCAPTCHA to actually use them. NOCAPTCHA=True @@ -162,7 +165,6 @@ FRONTEND_SERVERS=() # A tuple containing the FTP_MASTERS=() # A tuple containing the *IP addresses* of all machines # trusted to upload ftp structure data VARNISH_PURGERS=() # Extra servers that can do varnish purges through our queue -VARNISH_QUEUE_ID=1 # pgq queue id used for varnish purging ARCHIVES_SEARCH_SERVER="archives.postgresql.org" # Where to post REST request for archives search FRONTEND_SMTP_RELAY="magus.postgresql.org" # Where to relay user generated email SITE_UPDATE_TRIGGER_FILE='/tmp/pgweb.update_trigger' # Where to drop update trigger file diff --git a/sql/varnish.sql b/sql/varnish.sql index bfbdb743..10792088 100644 --- a/sql/varnish.sql +++ b/sql/varnish.sql @@ -2,20 +2,28 @@ BEGIN; -- -- Create a function to purge from varnish cache --- By default this adds the object to a pgq queue, +-- By default this adds the object to a local queue, -- but this function can be replaced with a void one -- when running a development version. -- +CREATE SCHEMA IF NOT EXISTS varnishqueue; +CREATE TABLE IF NOT EXISTS varnishqueue.queue (id bigserial primary key, mode char NOT NULL, consumerid int NOT NULL, expr text NOT NULL, added timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP, completed timestamptz NULL); +CREATE TABLE IF NOT EXISTS varnishqueue.consumers (consumerid serial PRIMARY KEY, consumer text NOT NULL); + +DROP FUNCTION IF EXISTS varnish_purge(url text); CREATE OR REPLACE FUNCTION varnish_purge(url text) -RETURNS bigint +RETURNS void AS $$ - SELECT pgq.insert_event('varnish', 'P', $1); + INSERT INTO varnishqueue.queue (mode, consumerid, expr) SELECT 'P', consumerid, $1 FROM varnishqueue.consumers; + NOTIFY varnishqueue; $$ LANGUAGE 'sql'; +DROP FUNCTION IF EXISTS varnish_purge_expr(expr text); CREATE OR REPLACE FUNCTION varnish_purge_expr(expr text) -RETURNS bigint +RETURNS void AS $$ - SELECT pgq.insert_event('varnish', 'X', $1); + INSERT INTO varnishqueue.queue (mode, consumerid, expr) SELECT 'X', consumerid, $1 FROM varnishqueue.consumers; + NOTIFY varnishqueue; $$ LANGUAGE 'sql'; -COMMIT; \ No newline at end of file +COMMIT; diff --git a/templates/core/admin_purge.html b/templates/core/admin_purge.html index 16df1ef0..27bc077e 100644 --- a/templates/core/admin_purge.html +++ b/templates/core/admin_purge.html @@ -11,12 +11,6 @@