~launchpad-pqm/launchpad/devel

10637.3.1 by Guilherme Salgado
Use the default python version instead of a hard-coded version
1
#!/usr/bin/python -S
9795.4.27 by Stuart Bishop
Cache database replication lag information as querying the live Slony tables can be slow
2
#
3
# Copyright 2010 Canonical Ltd.  This software is licensed under the
4
# GNU Affero General Public License version 3 (see the file LICENSE).
5
6
"""Calculate database replication lag and cache it."""
7
8
__metaclass__ = type
9
__all__ = []
10
11
import _pythonpath
12
13
import sys
14
import time
15
16
import psycopg2
17
18
from canonical.database.sqlbase import connect, ISOLATION_LEVEL_AUTOCOMMIT
19
from canonical.launchpad.scripts import db_options, logger
20
from lp.scripts.helpers import LPOptionParser
21
22
23
def main(args=None):
24
    parser = LPOptionParser()
25
    db_options(parser)
26
    parser.add_option(
27
        "-s", "--sleep", dest="sleep", type="int", default=5,
28
        metavar="SECS", help="Wait SECS seconds between refreshes.")
29
30
    (options, args) = parser.parse_args(args)
31
    if len(args) != 0:
32
        parser.error("Too many arguments.")
33
34
    log = logger(options)
35
36
    while True:
37
        try:
38
            con = connect(user="lagmon", isolation=ISOLATION_LEVEL_AUTOCOMMIT)
39
            cur = con.cursor()
40
            while True:
41
                cur.execute("SELECT update_replication_lag_cache()")
42
                if cur.fetchone()[0]:
43
                    log.info("Updated.")
44
                else:
45
                    log.error("update_replication_lag_cache() failed.")
46
                time.sleep(options.sleep)
47
        except psycopg2.Error, x:
48
            log.error("%s. Retrying.", str(x).strip())
49
            time.sleep(options.sleep)
50
51
52
if __name__ == '__main__':
53
    sys.exit(main())