~launchpad-pqm/launchpad/devel

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
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/python -S
#
# Copyright 2010 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Calculate database replication lag and cache it."""

__metaclass__ = type
__all__ = []

import _pythonpath

import sys
import time

import psycopg2

from lp.scripts.helpers import LPOptionParser
from lp.services.database.sqlbase import (
    connect,
    ISOLATION_LEVEL_AUTOCOMMIT,
    )
from lp.services.scripts import (
    db_options,
    logger,
    )


def main(args=None):
    parser = LPOptionParser()
    db_options(parser)
    parser.add_option(
        "-s", "--sleep", dest="sleep", type="int", default=5,
        metavar="SECS", help="Wait SECS seconds between refreshes.")

    (options, args) = parser.parse_args(args)
    if len(args) != 0:
        parser.error("Too many arguments.")

    log = logger(options)

    while True:
        try:
            con = connect(user="lagmon", isolation=ISOLATION_LEVEL_AUTOCOMMIT)
            cur = con.cursor()
            while True:
                cur.execute("SELECT update_replication_lag_cache()")
                if cur.fetchone()[0]:
                    log.info("Updated.")
                else:
                    log.error("update_replication_lag_cache() failed.")
                time.sleep(options.sleep)
        except psycopg2.Error, x:
            log.error("%s. Retrying.", str(x).strip())
            time.sleep(options.sleep)


if __name__ == '__main__':
    sys.exit(main())