- Author:
- Digitalxero
- Posted:
- October 23, 2008
- Language:
- Python
- Version:
- 1.0
- Score:
- 3 (after 3 ratings)
Request-phase cache middleware that checks to make sure the Cache server is running. Starting it if it is not. This is run for every request, it checks to see if it can get a defined item out of the cache, if that fails it tries to set it. Failing that it decides the server is probably crashed, so goes though and attempts to connect to the server. Failing a connection it will launch a new server.
This is probably not useful on large scale multi server deployments as they likely have their own testing for when services crash, but I am using it in a shared hosting environment where I have to run my own copy of memcache manually and cannot setup proper services testing, so I use this to just make sure the cache server is still running.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | import socket
import subprocess
from django.conf import settings
from django.core.cache import cache
class CheckCacheServer(object):
"""
Request-phase cache middleware that checks to make sure the Cache server is running.
Starting it if it is not
Must be first in the middleware stack so it can check before anything else runs
"""
def __init__(self):
self.cache_backend = settings.CACHE_BACKEND
self.servers = self.cache_backend[self.cache_backend.index('//')+2 : self.cache_backend.rindex('/')].split(';')
def process_request(self, request):
"""
This is run for every request, it checks to see if it can get a
defined item out of the cache, if that fails it tries to set it
Failing that it decides the server is probably crashed, but
goes though and attempts to connect to the server. Failing a connection
It will launch a new server.
"""
if not self.cache_backend.startswith('memcached'):
return None
alive = cache.get("test-connection")
if not alive and not cache.set("test-connection", 1, 604800):
try:
memcached = settings.CACHE_SERVER_STRING
except AttributeError:
memcached = "memcached -l %s -p %d -c 10 -d"
for server in self.servers:
ip, port = server.split(':')
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((ip, int(port)))
except socket.error, e:
# not running, start it
subprocess.Popen(memcached % (ip, int(port)), shell=True)
finally:
s.close()
return None
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.