summaryrefslogtreecommitdiff
path: root/loader/lib/varnish.py
blob: 2b2bf898c443da610916ce5be454af7408567da6 (plain)
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
import requests

from lib.log import log


class VarnishPurger(object):
    def __init__(self, cfg):
        self.cfg = cfg

    def purge(self, purges):
        if not len(purges):
            return

        if not self.cfg.has_option('varnish', 'purgeurl'):
            return

        purgeurl = self.cfg.get('varnish', 'purgeurl')
        exprlist = []
        for p in purges:
            if isinstance(p, tuple):
                # Purging a list
                exprlist.append('obj.http.x-pglm ~ :%s/%s/%s:' % p)
            else:
                # Purging individual thread
                exprlist.append('obj.http.x-pgthread ~ :%s:' % p)
        purgedict = dict(list(zip(['p%s' % n for n in range(0, len(exprlist))], exprlist)))
        purgedict['n'] = len(exprlist)
        r = requests.post(purgeurl, data=purgedict, headers={
            'Content-type': 'application/x-www-form-urlencoded',
            'Host': 'www.postgresql.org',
        })
        if r.status_code != 200:
            log.error("Failed to send purge request!")