~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 lp.scripts.helpers import LPOptionParser
14606.3.4 by William Grant
Replace canonical.database usage everywhere, and format-imports.
19
from lp.services.database.sqlbase import (
20
    connect,
21
    ISOLATION_LEVEL_AUTOCOMMIT,
22
    )
23
from lp.services.scripts import (
24
    db_options,
25
    logger,
26
    )
9795.4.27 by Stuart Bishop
Cache database replication lag information as querying the live Slony tables can be slow
27
28
29
def main(args=None):
30
    parser = LPOptionParser()
31
    db_options(parser)
32
    parser.add_option(
33
        "-s", "--sleep", dest="sleep", type="int", default=5,
34
        metavar="SECS", help="Wait SECS seconds between refreshes.")
35
36
    (options, args) = parser.parse_args(args)
37
    if len(args) != 0:
38
        parser.error("Too many arguments.")
39
40
    log = logger(options)
41
42
    while True:
43
        try:
44
            con = connect(user="lagmon", isolation=ISOLATION_LEVEL_AUTOCOMMIT)
45
            cur = con.cursor()
46
            while True:
47
                cur.execute("SELECT update_replication_lag_cache()")
48
                if cur.fetchone()[0]:
49
                    log.info("Updated.")
50
                else:
51
                    log.error("update_replication_lag_cache() failed.")
52
                time.sleep(options.sleep)
53
        except psycopg2.Error, x:
54
            log.error("%s. Retrying.", str(x).strip())
55
            time.sleep(options.sleep)
56
57
58
if __name__ == '__main__':
59
    sys.exit(main())